rust/compiler/rustc_parse/src/parser
Esteban Küber a16722d221 Handle attempts to have multiple cfgd 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.
2023-11-16 21:21:26 +00:00
..
attr_wrapper.rs Adjust to_attr_token_stream. 2023-09-06 17:12:07 +10:00
attr.rs Avoid too many expected symbols and reduce Nones 2023-08-03 08:56:31 +00:00
diagnostics.rs Handle attempts to have multiple cfgd tail expressions 2023-11-16 21:21:26 +00:00
expr.rs Suggest fix for ; within let-chains 2023-11-09 00:31:42 -08:00
generics.rs fix couple of clippy findings: 2023-07-23 10:50:14 +02:00
item.rs Rollup merge of #117282 - clubby789:recover-wrong-function-header, r=TaKO8Ki 2023-11-08 17:14:36 +01:00
mod.rs Re-format code with new rustfmt 2023-11-15 21:45:48 -05:00
nonterminal.rs Format all the let chains in compiler 2023-10-13 08:59:36 +00:00
pat.rs Minimize pub usage in source_map.rs. 2023-11-02 19:35:00 +11:00
path.rs Minimize pub usage in source_map.rs. 2023-11-02 19:35:00 +11:00
stmt.rs Handle attempts to have multiple cfgd tail expressions 2023-11-16 21:21:26 +00:00
ty.rs Fix some typos 2023-11-14 23:06:50 +08:00