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 using the enum when a variant is used as a type #35675

Closed
Wilfred opened this issue Aug 15, 2016 · 4 comments
Closed

Suggest using the enum when a variant is used as a type #35675

Wilfred opened this issue Aug 15, 2016 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@Wilfred
Copy link
Contributor

Wilfred commented Aug 15, 2016

Consider the code:

enum Fruit {
    Apple(i64),
    Orange(i64),
}

fn should_return_fruit() -> Apple {
    Apple(5)
}

The current error wording is:

error[E0412]: type name `Apple` is undefined or not in scope
 --> <anon>:7:29
  |
7 | fn should_return_fruit() -> Apple {
  |                             ^^^^^ undefined or not in scope
  |
  = help: no candidates by the name of `Apple` found in your project; maybe you misspelled the name or forgot to import an external crate?

I periodically make this exact mistake. It would be really nice if rustc could spot this error and say did you mean Fruit?

@Aatch
Copy link
Contributor

Aatch commented Aug 15, 2016

I'm not sure about your exact case, but I think such a suggestion for this:

enum Fruit {
    Apple(i64),
    Orange(i64),
}

fn should_return_fruit() -> Fruit::Apple {
    Fruit::Apple(5)
}

Or, more likely, this:

use Fruit::*;
enum Fruit {
    Apple(i64),
    Orange(i64),
}

fn should_return_fruit() -> Apple {
    Apple(5)
}

Makes sense, and likely isn't too hard to implement.

@Aatch Aatch added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 15, 2016
@retep998
Copy link
Member

retep998 commented Aug 15, 2016

Or, more likely, this:

use Fruit::*;
enum Fruit {
    Apple(i64),
    Orange(i64),
}

fn should_return_fruit() -> Apple {
    Apple(5)
}

@Aatch I'm pretty sure that is invalid because Apple isn't a type and cannot be specified as the return type for should_return_fruit.

@Aatch
Copy link
Contributor

Aatch commented Aug 15, 2016

@retep998 that's the point. I meant that suggestion should work in those cases, but not the original example, as Apple isn't in scope.

By "more likely", I meant that it's more likely to come up than the first example.

@sanxiyn
Copy link
Member

sanxiyn commented Aug 29, 2016

@Aatch's example gives this error, which seems good to me:

error[E0248]: found value `Fruit::Apple` used as a type
 --> 35675.rs:8:29
  |
8 | fn should_return_fruit() -> Apple {
  |                             ^^^^^ value used as a type

That is, different errors are given depending on whether name is in scope.

@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
frewsxcv added a commit to frewsxcv/rust that referenced this issue Apr 5, 2017
…enkov

Suggest using enum when a variant is used as a type

Given a file:

```rust
enum Fruit {
    Apple(i64),
    Orange(i64),
}

fn should_return_fruit() -> Apple {
    Apple(5)
}
```

Provide the following output:

```rust
error[E0412]: cannot find type `Apple` in this scope
  --> file.rs:16:29
   |
16 | fn should_return_fruit() -> Apple {
   |                             ^^^^^ not found in this scope
   |
help: there is an enum variant `Fruit::Apple`, did you mean to use `Fruit`?
  --> file.rs:12:5
   |
12 |     Apple(i64),
   |     ^^^^^^^^^^

error[E0425]: cannot find function `Apple` in this scope
  --> file.rs:17:5
   |
17 |     Apple(5)
   |     ^^^^^ not found in this scope
   |
   = help: possible candidate is found in another module, you can import it into scope:
             `use Fruit::Apple;`
```

Fix rust-lang#35675.
bors added a commit that referenced this issue Apr 8, 2017
Suggest using enum when a variant is used as a type

Given a file:

```rust
enum Fruit {
    Apple(i64),
    Orange(i64),
}

fn should_return_fruit() -> Apple {
    Apple(5)
}
```

Provide the following output:

```rust
error[E0412]: cannot find type `Apple` in this scope
  --> file.rs:16:29
   |
16 | fn should_return_fruit() -> Apple {
   |                             ^^^^^ not found in this scope
   |
help: there is an enum variant `Fruit::Apple`, did you mean to use `Fruit`?
  --> file.rs:12:5
   |
12 |     Apple(i64),
   |     ^^^^^^^^^^

error[E0425]: cannot find function `Apple` in this scope
  --> file.rs:17:5
   |
17 |     Apple(5)
   |     ^^^^^ not found in this scope
   |
   = help: possible candidate is found in another module, you can import it into scope:
             `use Fruit::Apple;`
```

Fix #35675.
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
Projects
None yet
Development

No branches or pull requests

5 participants