-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)Inline constants (aka: const blocks, const expressions, anonymous constants)S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
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
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.F-inline_constInline constants (aka: const blocks, const expressions, anonymous constants)Inline constants (aka: const blocks, const expressions, anonymous constants)S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueStatus: 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.Relevant to the compiler team, which will review and decide on the PR/issue.