Open
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?
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: Suggestions generated by the compiler applied by `cargo fix`Diagnostics: Confusing error or lint; hard to understand for new users.Inline constants (aka: const blocks, const expressions, anonymous constants)Status: A Minimal Complete and Verifiable Example has been found for this issueRelevant to the compiler team, which will review and decide on the PR/issue.