-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #117988 - estebank:issue-106020, r=cjgillot
Handle attempts to have multiple `cfg`d tail expressions When encountering code that seems like it might be trying to have multiple tail expressions depending on `cfg` information, suggest alternatives that will success to parse. ```rust fn foo() -> String { #[cfg(feature = "validation")] [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() #[cfg(not(feature = "validation"))] String::new() } ``` ``` error: expected `;`, found `#` --> $DIR/multiple-tail-expr-behind-cfg.rs:5:64 | LL | #[cfg(feature = "validation")] | ------------------------------ only `;` terminated statements or tail expressions are allowed after this attribute LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() | ^ expected `;` here LL | #[cfg(not(feature = "validation"))] | - unexpected token | help: add `;` here | LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>(); | + help: alternatively, consider surrounding the expression with a block | LL | { [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() } | + + help: it seems like you are trying to provide different expressions depending on `cfg`, consider using `if cfg!(..)` | LL ~ if cfg!(feature = "validation") { LL ~ [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() LL ~ } else if cfg!(not(feature = "validation")) { LL ~ String::new() LL + } | ``` Fix #106020. r? `@oli-obk`
- Loading branch information
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
tests/ui/parser/attribute/multiple-tail-expr-behind-cfg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#![feature(stmt_expr_attributes)] | ||
|
||
fn foo() -> String { | ||
#[cfg(feature = "validation")] | ||
[1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() //~ ERROR expected `;`, found `#` | ||
#[cfg(not(feature = "validation"))] | ||
String::new() | ||
} | ||
|
||
fn bar() -> String { | ||
#[attr] | ||
[1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() //~ ERROR expected `;`, found `#` | ||
#[attr] //~ ERROR cannot find attribute `attr` in this scope | ||
String::new() | ||
} | ||
|
||
fn main() { | ||
println!("{}", foo()); | ||
} |
54 changes: 54 additions & 0 deletions
54
tests/ui/parser/attribute/multiple-tail-expr-behind-cfg.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
error: expected `;`, found `#` | ||
--> $DIR/multiple-tail-expr-behind-cfg.rs:5:64 | ||
| | ||
LL | #[cfg(feature = "validation")] | ||
| ------------------------------ only `;` terminated statements or tail expressions are allowed after this attribute | ||
LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() | ||
| ^ expected `;` here | ||
LL | #[cfg(not(feature = "validation"))] | ||
| - unexpected token | ||
| | ||
help: add `;` here | ||
| | ||
LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>(); | ||
| + | ||
help: alternatively, consider surrounding the expression with a block | ||
| | ||
LL | { [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() } | ||
| + + | ||
help: it seems like you are trying to provide different expressions depending on `cfg`, consider using `if cfg!(..)` | ||
| | ||
LL ~ if cfg!(feature = "validation") { | ||
LL ~ [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() | ||
LL ~ } else if cfg!(not(feature = "validation")) { | ||
LL ~ String::new() | ||
LL + } | ||
| | ||
|
||
error: expected `;`, found `#` | ||
--> $DIR/multiple-tail-expr-behind-cfg.rs:12:64 | ||
| | ||
LL | #[attr] | ||
| ------- only `;` terminated statements or tail expressions are allowed after this attribute | ||
LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() | ||
| ^ expected `;` here | ||
LL | #[attr] | ||
| - unexpected token | ||
| | ||
help: add `;` here | ||
| | ||
LL | [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>(); | ||
| + | ||
help: alternatively, consider surrounding the expression with a block | ||
| | ||
LL | { [1, 2, 3].iter().map(|c| c.to_string()).collect::<String>() } | ||
| + + | ||
|
||
error: cannot find attribute `attr` in this scope | ||
--> $DIR/multiple-tail-expr-behind-cfg.rs:13:7 | ||
| | ||
LL | #[attr] | ||
| ^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|