-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Some Rust (playground):
struct Generic<T> {
inner: T,
}
impl<T> Generic<T> {
fn foo() -> Generic<()> {
Self { inner: () }
}
}
yields an error about the mismatched type of inner
:
error[E0308]: mismatched types
--> src/lib.rs:7:23
|
5 | impl<T> Generic<T> {
| - this type parameter
6 | fn foo() -> Generic<()> {
7 | Self { inner: () }
| ^^ expected type parameter `T`, found `()`
|
= note: expected type parameter `T`
found unit type `()`
but I know it should be ()
- the return type on foo
also expects T
to be ()
! The second error is somewhat more helpful:
error[E0308]: mismatched types
--> src/lib.rs:7:9
|
5 | impl<T> Generic<T> {
| - this type parameter
6 | fn foo() -> Generic<()> {
| ----------- expected `Generic<()>` because of return type
7 | Self { inner: () }
| ^^^^^^^^^^^^^^^^^^ expected `()`, found type parameter `T`
|
= note: expected struct `Generic<()>`
found struct `Generic<T>`
The actual issue I'd had writing this was Self { inner: () }
instead of Generic { inner: () }
. It would have been helpful if rustc could have concluded that the type I'd wanted matched the function's return type, and that the issue was Self
being Generic<T>
and causing the mismatch.
cc @estebank who'd asked me to write up an issue with a reproduction.
Apologies for the mushy title, I couldn't think of something more precise - very open to rewording if someone's got a better one-liner.
estebank and rrbutani
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.Diagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Point at impl self ty on type error involving `Self`
Suggest replacing `Self` with the right type on type error
Self
literals #117959Suggest replacing `Self` with the right type on type error
Point at impl self ty on type error involving `Self`
Suggest replacing `Self` with the right type on type error
Rollup merge of rust-lang#117959 - estebank:issue-76086, r=compiler-e…
Unrolled build for rust-lang#117959