Skip to content

Commit f2bd627

Browse files
committed
Reject cfg on expressions that cannot be safely removed
Previously, expressions behind stmt_expr_attributes were only rejected when the cfg condition evaluated to false. If the condition was true, the code compiled successfully. This meant code like an attributed binary operand could compile on one platform but fail on another, even though removing the operand would leave an invalid expression. This change always rejects cfg in expression positions where removing the expression would produce invalid code. Expression positions where removal is safe continue to work as before. Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
1 parent 2f5253f commit f2bd627

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

compiler/rustc_expand/src/expand.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2410,6 +2410,12 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
24102410
Some(sym::cfg) => {
24112411
let span = attr.span;
24122412
if self.expand_cfg_true(node, attr, pos).as_bool() {
2413+
if matches!(
2414+
Node::KIND,
2415+
AstFragmentKind::Expr | AstFragmentKind::MethodReceiverExpr
2416+
) {
2417+
self.cx.dcx().emit_err(RemoveExprNotSupported { span });
2418+
}
24132419
continue;
24142420
}
24152421

tests/ui/conditional-compilation/cfg-non-opt-expr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
#![feature(custom_test_frameworks)]
33

44
fn main() {
5+
let _ = 1 + #[cfg(unix)] 2;
6+
//~^ ERROR removing an expression is not supported in this position
7+
let _ = 1 + #[cfg(windows)] 2;
8+
//~^ ERROR removing an expression is not supported in this position
9+
10+
let _ = 1 + #[cfg(all())] 2;
11+
//~^ ERROR removing an expression is not supported in this position
512
let _ = #[cfg(false)] ();
613
//~^ ERROR removing an expression is not supported in this position
714
let _ = 1 + 2 + #[cfg(false)] 3;
Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
error: removing an expression is not supported in this position
2-
--> $DIR/cfg-non-opt-expr.rs:5:13
2+
--> $DIR/cfg-non-opt-expr.rs:5:17
3+
|
4+
LL | let _ = 1 + #[cfg(unix)] 2;
5+
| ^^^^^^^^^^^^
6+
7+
error: removing an expression is not supported in this position
8+
--> $DIR/cfg-non-opt-expr.rs:7:17
9+
|
10+
LL | let _ = 1 + #[cfg(windows)] 2;
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: removing an expression is not supported in this position
14+
--> $DIR/cfg-non-opt-expr.rs:10:17
15+
|
16+
LL | let _ = 1 + #[cfg(all())] 2;
17+
| ^^^^^^^^^^^^^
18+
19+
error: removing an expression is not supported in this position
20+
--> $DIR/cfg-non-opt-expr.rs:12:13
321
|
422
LL | let _ = #[cfg(false)] ();
523
| ^^^^^^^^^^^^^
624

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

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

19-
error: aborting due to 3 previous errors
37+
error: aborting due to 6 previous errors
2038

0 commit comments

Comments
 (0)