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 code to use enum type in return type when accidentally using variant instance #57393

Open
estebank opened this issue Jan 7, 2019 · 3 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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 7, 2019

When encountering an enum variant instance in a return type, make a better effort to suggest appropriate code.

Right now, if you accidentally write Some(Span) when you meant Option<Span>, we mention the existence of the enum, but we don't suggest code:

error[E0573]: expected type, found variant `Some`
    --> src/librustc_typeck/check/mod.rs:5198:10
     |
5198 |     ) -> Some(Span) {
     |          ^^^^^^^^^^ not a type
     |
     = help: there is an enum variant `rustc::middle::cstore::LibSource::Some`, try using `rustc::middle::cstore::LibSource`?
     = help: there is an enum variant `rustc::session::config::Passes::Some`, try using `rustc::session::config::Passes`?
     = help: there is an enum variant `std::prelude::v1::Option::Some`, try using `std::prelude::v1::Option`?
     = help: there is an enum variant `std::prelude::v1::Some`, try using `std::prelude::v1`?

It'd be nice if we could suggest the appropriate code.

@estebank estebank added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 7, 2019
@zackmdavis
Copy link
Member

@estebank Try nightly? I thought I addressed this in #56188?

@estebank
Copy link
Contributor Author

estebank commented Jan 7, 2019

@zackmdavis you're right. rustc hasn't updated to a recent enough nightly, which is why I didn't have it.

Trying it out in the playground, it doesn't seem to account for type arguments, locally accessible path (part of a larger issue) nor for reexported variants:

error[E0573]: expected type, found variant `Some`
 --> src/main.rs:5:13
  |
5 | fn foo() -> Some(usize) {
  |             ^^^^^^^^^^^ not a type
help: try using the variant's enum
  |
5 | fn foo() -> core::prelude::v1::Option {
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^
5 | fn foo() -> core::prelude::v1 {
  |             ^^^^^^^^^^^^^^^^^
5 | fn foo() -> lzw::Bits {
  |             ^^^^^^^^^
5 | fn foo() -> serde::export::Option {
  |             ^^^^^^^^^^^^^^^^^^^^^
and 4 other candidates

@zackmdavis
Copy link
Member

it doesn't seem to account for type arguments

Oh, that's right. (We'd want the substitution to be Option<usize>, not Option.)

@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 18, 2019
@crlf0710 crlf0710 added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jun 11, 2020
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. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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