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

Bad generic error for incorrectly placed where clause for tuple struct #100790

Closed
CAD97 opened this issue Aug 20, 2022 · 4 comments · Fixed by #106537
Closed

Bad generic error for incorrectly placed where clause for tuple struct #100790

CAD97 opened this issue Aug 20, 2022 · 4 comments · Fixed by #106537
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

@CAD97
Copy link
Contributor

CAD97 commented Aug 20, 2022

Given the following code: [playground]

struct Example
where
    (): Sized,
(usize);

The current output is:

error: expected one of `:`, `==`, or `=`, found `;`
 --> src/lib.rs:4:8
  |
4 | (usize);
  |        ^ expected one of `:`, `==`, or `=`

What appears to be happening is that we're trying to parse this as a unit struct (which can have a where clause), and thus trying to parse the struct tuple as a type to bound.

With a tuple visibility:

struct Example
where
    (): Sized,
(pub usize, usize);
error: expected type, found keyword `pub`
 --> src/lib.rs:4:2
  |
4 | (pub usize, usize);
  |  ^^^ expected type

Ideally,

  • In the former case (easier?) we can notice that we parsed a tuple type and then got ; instead of : (or a currently-just-for-diagnostics =) and then reinterpret the tuple type as a tuple struct tuple, printing a more useful error;
  • In the latter case (harder?) we would likely need to backtrack when seeing a pub visibility marker and retry to parse as a tuple struct definition tuple in order to provide the nicer error.
@CAD97 CAD97 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 Aug 20, 2022
@fmease
Copy link
Member

fmease commented Aug 20, 2022

printing a more useful error ("tuple structs cannot have a where clause")

Note that tuple structs can indeed have a where clause albeit after the fields not before them:

- struct Example
- where
-     (): Sized,
- (usize);
+ struct Example(usize)
+ where
+     (): Sized;

The proposed diagnostic should suggest to move the where clause.

@vincenzopalazzo
Copy link
Member

@rustbot claim

@CAD97 CAD97 changed the title Bad generic error for trying to use where clause for tuple struct Bad generic error for incorrectly placed where clause for tuple struct Aug 20, 2022
@estebank estebank added the A-parser Area: The parsing of Rust source code to an AST label Aug 22, 2022
@vincenzopalazzo vincenzopalazzo removed their assignment Oct 13, 2022
@fmease
Copy link
Member

fmease commented Jan 4, 2023

@rustbot claim

@fmease
Copy link
Member

fmease commented Jan 6, 2023

Fyi, PR is up: #106537. CC @gamma-delta.

@bors bors closed this as completed in d4203ed Jan 12, 2023
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

Successfully merging a pull request may close this issue.

4 participants