Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2410,6 +2410,12 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
Some(sym::cfg) => {
let span = attr.span;
if self.expand_cfg_true(node, attr, pos).as_bool() {
if matches!(
Node::KIND,
AstFragmentKind::Expr | AstFragmentKind::MethodReceiverExpr
) {
self.cx.dcx().emit_err(RemoveExprNotSupported { span });
}
continue;
}
Comment on lines 2412 to 2420

@mejrs mejrs Jul 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What this code does it emits this error if the cfg predicate is true, then if it's false we call expand_cfg_false and (if it's not the crate root) that emits the same error. Not only isn't that immediately obvious, it also suggests that there are still places where #[cfg(true)] is allowed but #[cfg(false)] is not.

Can you change it so there's only one place we emit this error (whether the predicate is true or not)?

Can you also change the error name and message to say that cfg is not supported in these positions?

View changes since the review

@mejrs mejrs Jul 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this check could also be moved elsewhere, I'm not sure what the best place for it is)


Expand Down
7 changes: 7 additions & 0 deletions tests/ui/conditional-compilation/cfg-non-opt-expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
#![feature(custom_test_frameworks)]

fn main() {
let _ = 1 + #[cfg(unix)] 2;
//~^ ERROR removing an expression is not supported in this position
let _ = 1 + #[cfg(windows)] 2;
//~^ ERROR removing an expression is not supported in this position

let _ = 1 + #[cfg(all())] 2;
//~^ ERROR removing an expression is not supported in this position
let _ = #[cfg(false)] ();
//~^ ERROR removing an expression is not supported in this position
let _ = 1 + 2 + #[cfg(false)] 3;
Expand Down
26 changes: 22 additions & 4 deletions tests/ui/conditional-compilation/cfg-non-opt-expr.stderr

@mejrs mejrs Jul 20, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this file have some #[cfg(true)] as well?

View changes since the review

Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:5:13
--> $DIR/cfg-non-opt-expr.rs:5:17
|
LL | let _ = 1 + #[cfg(unix)] 2;
| ^^^^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:7:17
|
LL | let _ = 1 + #[cfg(windows)] 2;
| ^^^^^^^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:10:17
|
LL | let _ = 1 + #[cfg(all())] 2;
| ^^^^^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:12:13
|
LL | let _ = #[cfg(false)] ();
| ^^^^^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:7:21
--> $DIR/cfg-non-opt-expr.rs:14:21
|
LL | let _ = 1 + 2 + #[cfg(false)] 3;
| ^^^^^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:9:23
--> $DIR/cfg-non-opt-expr.rs:16:23
|
LL | let _ = [1, 2, 3][#[cfg(false)] 1];
| ^^^^^^^^^^^^^

error: aborting due to 3 previous errors
error: aborting due to 6 previous errors

Loading