Skip to content

Better diagnostic for using struct fields in consts #92882

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ impl<'a> Parser<'a> {
err.emit();
None
} else {
if self.check(&token::Dot) {
err.help("if this was intended as a const argument, wrap it in curly braces: `{ ... }`");
}
// Rewind to before attempting to parse the type and continue parsing.
let parser_snapshot_after_type =
mem::replace(self, parser_snapshot_before_type);
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/issues/issue-92776.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct Example {
foo: usize
}

struct Example2(u32);

const EXAMPLE: Example = Example { foo: 42 };
const EXAMPLE_2: Example2 = Example2(42);

struct Wow<const N: usize> {
field: [u8; N]
}


fn a() {
let _a: Wow<EXAMPLE.foo> = Wow::new();
//~^ ERROR expected one of
}

fn main() {
let _b: Wow<EXAMPLE_2.0> = Wow::new();
//~^ ERROR expected one of
}
22 changes: 22 additions & 0 deletions src/test/ui/issues/issue-92776.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: expected one of `!`, `(`, `+`, `,`, `::`, `:`, `<`, `=`, or `>`, found `.`
--> $DIR/issue-92776.rs:16:24
|
LL | let _a: Wow<EXAMPLE.foo> = Wow::new();
| -- ^ expected one of 9 possible tokens
| |
| while parsing the type for `_a`
|
= help: if this was intended as a const argument, wrap it in curly braces: `{ ... }`

error: expected one of `!`, `(`, `+`, `,`, `::`, `:`, `<`, `=`, or `>`, found `.`
--> $DIR/issue-92776.rs:21:26
|
LL | let _b: Wow<EXAMPLE_2.0> = Wow::new();
| -- ^ expected one of 9 possible tokens
| |
| while parsing the type for `_b`
|
= help: if this was intended as a const argument, wrap it in curly braces: `{ ... }`

error: aborting due to 2 previous errors