rust/compiler/rustc_parse/src/parser
Michael Goulet e6a3ca0c65
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`
2023-11-19 19:14:34 -08:00
..
attr.rs More detail when expecting expression but encountering bad macro argument 2023-11-16 16:19:04 +00:00
attr_wrapper.rs Adjust to_attr_token_stream. 2023-09-06 17:12:07 +10:00
diagnostics.rs Rollup merge of #117988 - estebank:issue-106020, r=cjgillot 2023-11-19 19:14:34 -08:00
expr.rs Auto merge of #114292 - estebank:issue-71039, r=b-naber 2023-11-17 20:57:12 +00:00
generics.rs fix couple of clippy findings: 2023-07-23 10:50:14 +02:00
item.rs More detail when expecting expression but encountering bad macro argument 2023-11-16 16:19:04 +00:00
mod.rs Auto merge of #114292 - estebank:issue-71039, r=b-naber 2023-11-17 20:57:12 +00:00
nonterminal.rs More detail when expecting expression but encountering bad macro argument 2023-11-16 16:19:04 +00:00
pat.rs Auto merge of #114292 - estebank:issue-71039, r=b-naber 2023-11-17 20:57:12 +00:00
path.rs More detail when expecting expression but encountering bad macro argument 2023-11-16 16:19:04 +00:00
stmt.rs Rollup merge of #117988 - estebank:issue-106020, r=cjgillot 2023-11-19 19:14:34 -08:00
ty.rs Rollup merge of #117891 - compiler-errors:recover-for-dyn, r=davidtwco 2023-11-19 19:14:33 -08:00