Skip to content

Inline const expression inside vec!/macros should suggest adding parentheses under edition <2024 #126459

Open
@Zalathar

Description

@Zalathar

Code

fn main() {
    // Works fine, no warning.
    let _: [Vec<String>; 10] = [const { vec![] }; 10];
    // Works fine, no warning.
    let _: Vec<Vec<String>> = vec![(const { vec![] })];

    // Works, but warns about unnecessary parentheses.
    let _: Vec<Vec<String>> = vec![(const { vec![] }); 10];

    // These fail on stable79/beta80/nightly81 with edition 2021.
    // They work on nightly81 with edition 2024.
    let _: Vec<Vec<String>> = vec![const { vec![] }];
    let _: Vec<Vec<String>> = vec![const { vec![] }; 10];
}

Current output

error: no rules expected the token `const`
  --> src/main.rs:12:36
   |
12 |     let _: Vec<Vec<String>> = vec![const { vec![] }];
   |                                    ^^^^^ no rules expected this token in macro call
   |
   = note: while trying to match end of macro

error: no rules expected the token `const`
  --> src/main.rs:13:36
   |
13 |     let _: Vec<Vec<String>> = vec![const { vec![] }; 10];
   |                                    ^^^^^ no rules expected this token in macro call
   |
   = note: while trying to match end of macro

Desired output

help: consider surrounding the const expression with parentheses
   |
12 -     let _: Vec<Vec<String>> = vec![const { vec![] }];
12 +     let _: Vec<Vec<String>> = vec![(const { vec![] })];
   |

Rationale and extra context

For compatibility reasons, macros under edition <2024 do not treat const as the start of an expr, so a layer of parentheses is needed to convince the macro to treat inline-const as an expression.

Other cases

No response

Rust Version

(Stable Rust 1.79.0 on playground.)

Anything else?

https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/Weird.20interaction.20between.20vec!.5B.5D.20macro.20and.20inline.20const

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions