Skip to content
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

Placing an attribute on a generic argument confuses the parser #135017

Open
weiznich opened this issue Jan 2, 2025 · 0 comments
Open

Placing an attribute on a generic argument confuses the parser #135017

weiznich opened this issue Jan 2, 2025 · 0 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@weiznich
Copy link
Contributor

weiznich commented Jan 2, 2025

Code

struct Foo<T>(T);

fn main() {
    let foo: Foo<#[cfg(not(wrong))] String> = todo!();
}

Current output

error: invalid const generic expression
 --> /playground/src/main.rs:8:37
  |
8 |     let foo: Foo<#[cfg(not(wrong))] String> = todo!();
  |                                     ^^^^^^
  |
help: expressions must be enclosed in braces to be used as const generic arguments
  |
8 |     let foo: Foo<#[cfg(not(wrong))] { String }> = todo!();

Desired output

error: attributes are not allowed on generic arguments 
--> /playground/src/main.rs:8:37
  |
8 |     let foo: Foo<#[cfg(not(wrong))] String> = todo!();
  |                  ^^^^^^^^^^^^^^^^^^
  |                  attribute not allowed in this context
help: remove the attribute to allow the code to compile:
8 |     let foo: Foo<String> = todo!();

Rationale and extra context

Playground link: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=eaf1097ff4efa3734d3a5e3faa3cb10f

Other cases

If one applies the suggestion made in the current error, you get the "same" error message suggesting to add another level of brackets.

Rust Version

Build using the Nightly version: 1.85.0-nightly

(2025-01-01 45d11e51bb66c2deb63a)

Anything else?

No response

@weiznich weiznich 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 Jan 2, 2025
@jieyouxu jieyouxu added the A-parser Area: The parsing of Rust source code to an AST label Jan 2, 2025
@chenyukang chenyukang self-assigned this Jan 17, 2025
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-parser Area: The parsing of Rust source code to an AST 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

3 participants