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

Forgetting to call a variant constructor causes a confusing error message #35241

Closed
arielb1 opened this issue Aug 3, 2016 · 7 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics

Comments

@arielb1
Copy link
Contributor

arielb1 commented Aug 3, 2016

STR

struct Foo(u32);

fn test() -> Foo { Foo }

fn main() {
}

Confusing result

error[E0308]: mismatched types
 --> <anon>:3:20
  |
3 | fn test() -> Foo { Foo }
  |                    ^^^ expected struct `Foo`, found fn item
  |
  = note: expected type `Foo`
  = note:    found type `fn(u32) -> Foo {Foo::{{constructor}}}`
@arielb1 arielb1 added the A-diagnostics Area: Messages for errors, warnings, and lints label Aug 3, 2016
@arielb1
Copy link
Contributor Author

arielb1 commented Aug 3, 2016

The error message mentions function items, which is confusing to new users.

bors added a commit that referenced this issue Aug 4, 2016
bors added a commit that referenced this issue Aug 4, 2016
@arielb1 arielb1 closed this as completed Aug 20, 2016
@arielb1 arielb1 reopened this Aug 20, 2016
@steveklabnik steveklabnik removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@estebank
Copy link
Contributor

non-fn-like structs have better output, we should perform the same for fn-like structs:

error[E0423]: expected value, found struct `Bar`
 --> src/main.rs:9:21
  |
9 | fn test2() -> Bar { Bar }
  |                     ^^^ did you mean `Bar { /* fields */ }`?

@estebank estebank added the WG-diagnostics Working group: Diagnostics label Sep 30, 2017
@estebank
Copy link
Contributor

Mentoring instructions:

In note_type_err, check wether values can be decomposed into Some(ValuePairs::Types(exp_found). If it can, check wether exp_found.found is a TypeVariant::TyFnPtr(_) with an FnSig.output() == exp_found.expected. If that is the case, add a note to the diag like for E-423.

@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. labels Sep 30, 2017
@colinmarsh19
Copy link
Contributor

Has anyone worked on this recently? I might take a crack at it. New to Rust but I'd like to learn, and start getting involved with OSS. Let me know if there are any conflicts, or if anyone is able to give me some guidance. Thanks! :)

@estebank
Copy link
Contributor

@colinmarsh19 go ahead!

The terse explanation above should give enough context for someone slightly familiar with the compiler, but if you need more help you can always reach out with a comment in this ticket, on gitter or by creating a PR for us to comment on your code directly.

@joshleeb
Copy link

@colinmarsh19 are you currently working on this? If not I would like to give it a go :)

bors added a commit that referenced this issue Nov 2, 2017
Improve display of error E0308

Ref. Forgetting to call a variant constructor causes a confusing error message #35241.

This PR modifies [`note_type_err`](https://github.com/rust-lang/rust/blob/b7041bfab3a83702a8026fb7a18d8ea7d54cc648/src/librustc/infer/error_reporting/mod.rs#L669-L674) to display a `help` message when a `TyFnPtr` or `TyFnDef` are found and the return type, of the function or function pointer, is the same as the type that is expected.

The output of compiling

```rust
struct Foo(u32);

fn test() -> Foo { Foo }

fn main() {}
```

is now

```bash
$ rustc src/test/ui/issue-35241.rs
error[E0308]: mismatched types
  --> src/test/ui/issue-35241.rs:13:20
   |
13 | fn test() -> Foo { Foo }
   |              ---   ^^^ expected struct `Foo`, found fn item
   |              |
   |              expected `Foo` because of return type
   |
   = help: did you mean `Foo { /* fields */ }`?
   = note: expected type `Foo`
              found type `fn(u32) -> Foo {Foo::{{constructor}}}`

error: aborting due to previous error
```
@dsheets
Copy link

dsheets commented Nov 17, 2017

I believe this issue has been fixed in #45630.

Centril added a commit to Centril/rust that referenced this issue Aug 9, 2019
Tweak mismatched types error

- Change expected/found for type mismatches in `break`
- Be more accurate when talking about diverging match arms
- Tweak wording of function without a return value
- Suggest calling bare functions when their return value can be coerced to the expected type
- Give more parsing errors when encountering `foo(_, _, _)`

Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241.
Centril added a commit to Centril/rust that referenced this issue Aug 9, 2019
Tweak mismatched types error

- Change expected/found for type mismatches in `break`
- Be more accurate when talking about diverging match arms
- Tweak wording of function without a return value
- Suggest calling bare functions when their return value can be coerced to the expected type
- Give more parsing errors when encountering `foo(_, _, _)`

Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241.
Centril added a commit to Centril/rust that referenced this issue Aug 9, 2019
Tweak mismatched types error

- Change expected/found for type mismatches in `break`
- Be more accurate when talking about diverging match arms
- Tweak wording of function without a return value
- Suggest calling bare functions when their return value can be coerced to the expected type
- Give more parsing errors when encountering `foo(_, _, _)`

Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241, cc rust-lang#51669.
Centril added a commit to Centril/rust that referenced this issue Aug 10, 2019
Tweak mismatched types error

- Change expected/found for type mismatches in `break`
- Be more accurate when talking about diverging match arms
- Tweak wording of function without a return value
- Suggest calling bare functions when their return value can be coerced to the expected type
- Give more parsing errors when encountering `foo(_, _, _)`

Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241, cc rust-lang#51669.
Centril added a commit to Centril/rust that referenced this issue Aug 10, 2019
Tweak mismatched types error

- Change expected/found for type mismatches in `break`
- Be more accurate when talking about diverging match arms
- Tweak wording of function without a return value
- Suggest calling bare functions when their return value can be coerced to the expected type
- Give more parsing errors when encountering `foo(_, _, _)`

Fix rust-lang#51767, fix rust-lang#62677, fix rust-lang#63136, cc rust-lang#37384, cc rust-lang#35241, cc rust-lang#51669.
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 C-enhancement Category: An issue proposing an enhancement or a PR with one. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

7 participants