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

Suggest tuple struct syntax when using struct incorrectly #57242

Closed
estebank opened this issue Dec 31, 2018 · 4 comments
Closed

Suggest tuple struct syntax when using struct incorrectly #57242

estebank opened this issue Dec 31, 2018 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. P-medium Medium priority

Comments

@estebank
Copy link
Contributor

As per #57047 (comment), when encountering a tuple struct being used with the wrong syntax, suggest an appropriate fix:

error[E0560]: struct `NonCopyable` has no field named `p`
  --> $DIR/issue-4736.rs:4:26
   |
LL |     let z = NonCopyable{ p: () }; //~ ERROR struct `NonCopyable` has no field named `p`
   |             -------------^-----
   |             |            |
   |             |            field does not exist
   |             help: `NonCopyable` is a tuple struct, use the appropriate syntax: `NonCopyable(())`

instead of

error[E0560]: struct `NonCopyable` has no field named `p`
  --> $DIR/issue-4736.rs:4:26
   |
LL |     let z = NonCopyable{ p: () }; //~ ERROR struct `NonCopyable` has no field named `p`
   |                          ^ help: a field with a similar name exists: `0`
@estebank estebank added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Dec 31, 2018
@estebank estebank added P-medium Medium priority A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. labels Jun 11, 2019
@Electron-libre
Copy link
Contributor

Hi,

I can give it a try. This does not seems hard to fix.

Probably from here:

fn report_unknown_field(

  • 1 Unit test

Should be enought

@estebank
Copy link
Contributor Author

estebank commented Jun 11, 2019

@Electron-libre that'd be great!

You'll have to look at the AdtDef's (only) variant in variants, which is of type VariantDef, and look at its ctor_kind field (of type CtorKind) to identify whether this is a struct or tuple style struct. From there it is a matter of using the appropriate spans for the suggestion and silencing the "similar name exists" suggestion.

To test your changes you can use ./x.py test src/test/ui --stage 1, and once you're happy with the results you can run the same command with --bless to update the stderr files.

@Electron-libre
Copy link
Contributor

Thanks for the tips.

Centril added a commit to Centril/rust that referenced this issue Jun 20, 2019
…_syntax, r=estebank

suggest tuple struct syntax

refs rust-lang#57242
@JohnTitor
Copy link
Member

@estebank this can be closed? It seems fixed in #61782.

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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. E-medium Call for participation: Medium difficulty. Experience needed to fix: Intermediate. P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

3 participants