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

Improve display of error E0308 #45630

Merged
merged 1 commit into from
Nov 2, 2017
Merged

Improve display of error E0308 #45630

merged 1 commit into from
Nov 2, 2017

Conversation

joshleeb
Copy link

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

This PR modifies note_type_err 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

struct Foo(u32);

fn test() -> Foo { Foo }

fn main() {}

is now

$ 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

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pnkfelix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@kennytm kennytm added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 30, 2017
@estebank
Copy link
Contributor

The output when Foo is defined as struct Foo {a: u32} is:

error[E0423]: expected value, found struct `Foo`
 --> src/main.rs:3:20
  |
3 | fn test() -> Foo { Foo }
  |                    ^^^ did you mean `Foo { /* fields */ }`?

The help did you mean 'Foo { /* fields */ }'? in this PR is incorrect as it should be Foo(/* fields */) instead.

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 31, 2017
"did you mean `{}(/* fields */)`?",
self.tcx.item_path_str(def_id)
);
diag.help(&message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing and this will be ready for merging (r=me), could you change diag.help(&message) here for span_label(cause.span, &message)?

That way the output will be:

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

Also, while you're at it, could you squash your PR into a single commit?

Improve the display of error E0308 for structs by adding a "did you
mean" span label.
@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Oct 31, 2017

📌 Commit 87c951d has been approved by estebank

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 1, 2017
@bors
Copy link
Contributor

bors commented Nov 2, 2017

⌛ Testing commit 87c951d with merge a7d98c7...

bors added a commit that referenced this pull request 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
```
@bors
Copy link
Contributor

bors commented Nov 2, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: estebank
Pushing a7d98c7 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants