-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Better handle type errors involving Self
literals
#117959
Conversation
r? @TaKO8Ki (rustbot has picked a reviewer for you, use r? to override) |
This comment was marked as resolved.
This comment was marked as resolved.
38faceb
to
c26b5c6
Compare
When encountering a type error involving a `Self` literal, point at the self type of the enclosing `impl`. CC rust-lang#76086.
When encountering a type error caused by the use of `Self`, suggest using the actual type name instead. ``` error[E0308]: mismatched types --> $DIR/struct-path-self-type-mismatch.rs:13:9 | LL | impl<T> Foo<T> { | - ------ this is the type of the `Self` literal | | | found type parameter LL | fn new<U>(u: U) -> Foo<U> { | - ------ expected `Foo<U>` because of return type | | | expected type parameter LL | / Self { LL | | LL | | inner: u LL | | LL | | } | |_________^ expected `Foo<U>`, found `Foo<T>` | = note: expected struct `Foo<U>` found struct `Foo<T>` = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters help: use the type name directly | LL | Foo::<U> { | ~~~~~~~~ ``` Fix rust-lang#76086.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me after moving the call
@@ -1738,6 +1740,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> { | |||
// label pointing out the cause for the type coercion will be wrong | |||
// as prior return coercions would not be relevant (#57664). | |||
let fn_decl = if let (Some(expr), Some(blk_id)) = (expression, blk_id) { | |||
self.explain_self_literal(fcx, &mut err, expr, expected, found); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like a better location for this is with the rest of the mismatch suggestions in emit_type_mismatch_suggestions
.
c26b5c6
to
1e8c095
Compare
@bors r=compiler-errors |
…iaskrgr Rollup of 5 pull requests Successful merges: - rust-lang#117892 (Detect more `=>` typos) - rust-lang#117959 (Better handle type errors involving `Self` literals) - rust-lang#117980 (fix: Update CONTRIBUTING.md recommend -> recommended) - rust-lang#117982 (bootstrap: only show PGO warnings when verbose) - rust-lang#117990 (Tweak error and move tests) Failed merges: - rust-lang#117944 (some additional region refactorings) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#117959 - estebank:issue-76086, r=compiler-errors Better handle type errors involving `Self` literals When encountering a type error involving a `Self` literal, point at the self type of the enclosing `impl` and suggest using the actual type name instead. ``` error[E0308]: mismatched types --> $DIR/struct-path-self-type-mismatch.rs:13:9 | LL | impl<T> Foo<T> { | - ------ this is the type of the `Self` literal | | | found type parameter LL | fn new<U>(u: U) -> Foo<U> { | - ------ expected `Foo<U>` because of return type | | | expected type parameter LL | / Self { LL | | LL | | inner: u LL | | LL | | } | |_________^ expected `Foo<U>`, found `Foo<T>` | = note: expected struct `Foo<U>` found struct `Foo<T>` = note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters help: use the type name directly | LL | Foo::<U> { | ~~~~~~~~ ``` Fix rust-lang#76086.
When encountering a type error involving a
Self
literal, point at the self type of the enclosingimpl
and suggest using the actual type name instead.Fix #76086.