Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Zalathar opened this issue Jun 14, 2024 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Zalathar
Copy link
Contributor

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

@Zalathar Zalathar added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 14, 2024
@Zalathar
Copy link
Contributor Author

(Given that this works in 2024, I'm not sure whether it's worth the effort of a specific hint, but I figured I might as well file an issue for it.)

@Zalathar
Copy link
Contributor Author

See also #126457, where the compiler sometimes inappropriately warns about the necessary parens.

@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue labels Jun 14, 2024
@Neutron3529
Copy link
Contributor

Currently, even in edition 2024, vec![const { vec![] }]; failed to compile (at least for now)
playground

@ehuss
Copy link
Contributor

ehuss commented Sep 11, 2024

Currently, even in edition 2024, vec![const { vec![] }]; failed to compile (at least for now)

The relevant edition is the one used for building the standard library itself. So the relevant edition will be the one where the macro is defined, not where it is called.

@jendrikw
Copy link
Contributor

Can we add another macro pattern for edition 2021 to match this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. F-inline_const Inline constants (aka: const blocks, const expressions, anonymous constants) S-has-mcve Status: A Minimal Complete and Verifiable Example has been found for this issue T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants