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
7 changes: 7 additions & 0 deletions compiler/rustc_expand/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2427,6 +2427,13 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
Some(sym::cfg) => {
let span = attr.span;
if self.expand_cfg_true(node, attr, pos).as_bool() {
match Node::KIND {
AstFragmentKind::Expr | AstFragmentKind::MethodReceiverExpr => {
self.cx.dcx().emit_err(RemoveExprNotSupported { span });
}
AstFragmentKind::Crate => {}
_ => unreachable!(),
}
Comment thread
petrochenkov marked this conversation as resolved.
continue;
}
Comment on lines 2429 to 2438

@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)

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 you change it so there's only one place we emit this error (whether the predicate is true or not)?

The false version is reported in expand_cfg_false.

This seems like an ok place to do this check for the true case, if it's moved to expand_cfg_true, the match #159580 (comment) will be much larger at least.

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.

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.

If expand_cfg_false is inlined and replaced with a similar match then it will be more clear.


Expand Down
27 changes: 27 additions & 0 deletions tests/ui/conditional-compilation/cfg-non-opt-expr.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
#![feature(stmt_expr_attributes)]
#![feature(custom_test_frameworks)]

#[derive(Clone)]
enum E {
V1 = #[cfg(true)] 1,
//~^ ERROR removing an expression is not supported in this position
//~| ERROR removing an expression is not supported in this position
V2 = #[cfg_attr(true, cfg(true))] 2,
//~^ ERROR removing an expression is not supported in this position
}

macro_rules! mac {
($expr:expr) => { $expr.clone() }
}

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;
//~^ ERROR removing an expression is not supported in this position
let _ = [1, 2, 3][#[cfg(false)] 1];
//~^ ERROR removing an expression is not supported in this position
let _ = mac!(#[cfg(true)] 10);
//~^ ERROR removing an expression is not supported in this position
let _ = #[cfg(true)] ();
//~^ ERROR removing an expression is not supported in this position
let _ = 1 + 2 + #[cfg(true)] 3;
//~^ ERROR removing an expression is not supported in this position
let _ = [1, 2, 3][#[cfg(true)] 1];
//~^ ERROR removing an expression is not supported in this position
}
70 changes: 66 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,82 @@
error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:5:13
--> $DIR/cfg-non-opt-expr.rs:18:17
|
LL | let _ = 1 + #[cfg(unix)] 2;
| ^^^^^^^^^^^^

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

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

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

error: aborting due to 3 previous errors
error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:32:13
|
LL | let _ = #[cfg(true)] ();
| ^^^^^^^^^^^^

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

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

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:6:10
|
LL | V1 = #[cfg(true)] 1,
| ^^^^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:6:10
|
LL | V1 = #[cfg(true)] 1,
| ^^^^^^^^^^^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:9:27
|
LL | V2 = #[cfg_attr(true, cfg(true))] 2,
| ^^^^^^^^^

error: removing an expression is not supported in this position
--> $DIR/cfg-non-opt-expr.rs:30:18
|
LL | let _ = mac!(#[cfg(true)] 10);
| ^^^^^^^^^^^^

error: aborting due to 13 previous errors

Loading