rust-lang / rust-lang/rust

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

Open
#126,459 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros A-suggestion-diagnostics D-newcomer-roadblock F-inline_const S-has-mcve T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/main.rs, focusing on the vec! macro calls containing inline const expressions under edition 2021. Compare the failing cases with the parenthesized working case and the edition 2024 behavior. Done means the failing forms produce a help suggestion to surround the const expression with parentheses.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.