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

Provide context when type error happens due to different fn arguments with the same type parameter are called with different types #116615

Closed
estebank opened this issue Oct 10, 2023 · 3 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Oct 10, 2023

Code

fn main() {
    foo(1, 2.);
}
fn foo<T>(a: T, b: T) {}

Current output

error[E0308]: mismatched types
 --> src/main.rs:2:12
  |
2 |     foo(1, 2.);
  |     ---    ^^ expected integer, found floating-point number
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:4:4
  |
4 | fn foo<T>(a: T, b: T) {}
  |    ^^^          ----

Desired output

error[E0308]: mismatched types
 --> src/main.rs:2:12
  |
2 |     foo(1, 2.);
  |     --- -  ^^ expected integer, found floating-point number
  |     |   |
  |     |   expected because that argument needs to match the integer type of this parameter
  |     |
  |     arguments to this function are incorrect
  |
note: function defined here
 --> src/main.rs:4:4
  |
4 | fn foo<T>(a: T, b: T) {}
  |    ^^^ -  ----  ----
  |        |  |     |
  |        |  |     this parameter needs to match the type of `a`
  |        |  `b` needs to match the type of this parameter
  |        `a` and `b` both reference type parameter `T`

Rationale and extra context

No response

Other cases

No response

Anything else?

No response

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 10, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 10, 2023
@estebank estebank added A-typesystem Area: The type system D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 10, 2023
@chenyukang chenyukang assigned chenyukang and unassigned chenyukang Feb 13, 2024
@strottos
Copy link
Contributor

@rustbot claim

I think I can see how to implement this, will give it a go and unassign if it doesn't work.

@strottos
Copy link
Contributor

Spent the last week on this now and put a draft PR here for feedback at this stage, though not essential if I don't get it at this stage: #121595

It's mostly working now, though there are likely to be improvements yet and I'd like to do a review of it myself before pushing even which I'll probably do later next week.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 3, 2024
…errors

Better reporting on generic argument mismatchs

This allows better reporting as per issue rust-lang#116615 .

If you have a function:
```
fn foo(a: T, b: T) {}
```
and call it like so:
```
foo(1, 2.)
```
it'll give improved error reported similar to the following:
```
error[E0308]: mismatched types
 --> generic-mismatch-reporting-issue-116615.rs:6:12
  |
6 |     foo(1, 2.);
  |     --- -  ^^ expected integer, found floating-point number
  |     |   |
  |     |   expected argument `b` to be an integer because that argument needs to match the type of this parameter
  |     arguments to this function are incorrect
  |
note: function defined here
 --> generic-mismatch-reporting-issue-116615.rs:1:4
  |
1 | fn foo<T>(a: T, b: T) {}
  |    ^^^ -  ----  ----
  |        |  |     |
  |        |  |     this parameter needs to match the integer type of `a`
  |        |  `b` needs to match the type of this parameter
  |        `a` and `b` all reference this parameter T
```

Open question, do we need to worry about error message translation into other languages? Not sure what the status of that is in Rust.

NB: Needs some checking over and some tests have altered that need sanity checking, but overall this is starting to get somewhere now. Will take out of draft PR status when this has been done, raising now to allow feedback at this stage, probably 90% ready.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 3, 2024
Rollup merge of rust-lang#121595 - strottos:issue_116615, r=compiler-errors

Better reporting on generic argument mismatchs

This allows better reporting as per issue rust-lang#116615 .

If you have a function:
```
fn foo(a: T, b: T) {}
```
and call it like so:
```
foo(1, 2.)
```
it'll give improved error reported similar to the following:
```
error[E0308]: mismatched types
 --> generic-mismatch-reporting-issue-116615.rs:6:12
  |
6 |     foo(1, 2.);
  |     --- -  ^^ expected integer, found floating-point number
  |     |   |
  |     |   expected argument `b` to be an integer because that argument needs to match the type of this parameter
  |     arguments to this function are incorrect
  |
note: function defined here
 --> generic-mismatch-reporting-issue-116615.rs:1:4
  |
1 | fn foo<T>(a: T, b: T) {}
  |    ^^^ -  ----  ----
  |        |  |     |
  |        |  |     this parameter needs to match the integer type of `a`
  |        |  `b` needs to match the type of this parameter
  |        `a` and `b` all reference this parameter T
```

Open question, do we need to worry about error message translation into other languages? Not sure what the status of that is in Rust.

NB: Needs some checking over and some tests have altered that need sanity checking, but overall this is starting to get somewhere now. Will take out of draft PR status when this has been done, raising now to allow feedback at this stage, probably 90% ready.
@strottos
Copy link
Contributor

strottos commented Apr 4, 2024

This has been merged now, I believe the issue can be closed.

@estebank estebank closed this as completed Apr 5, 2024
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-typesystem Area: The type system D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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

4 participants