Inline `const` expression inside `vec!`/macros should suggest adding parentheses under edition <2024
Open
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?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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