-
-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Reject cfg on expressions that cannot be safely removed #159580
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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!(), | ||
| } | ||
| continue; | ||
| } | ||
|
Comment on lines
2429
to
2438
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The This seems like an ok place to do this check for the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If |
||
|
|
||
|
|
||
| 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 | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this file have some |
| 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 | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.