Skip to content

Provide more context when an expected type was a type parameter - #160180

Open
estebank wants to merge 7 commits into
rust-lang:mainfrom
estebank:issue-67991
Open

estebank wants to merge 7 commits into
rust-lang:mainfrom
estebank:issue-67991

Conversation

@estebank

@estebank estebank commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

This becomes particularly confusing when a type parameter has a default.

error[E0053]: method `foo` has an incompatible type for trait
  --> $DIR/default-type-parameter.rs:8:17
   |
LL | impl Trait for S {
LL |     fn foo() -> u32 { 0 }
   |                 ^^^ expected `()`, found `u32`
   |
note: type in trait
  --> $DIR/default-type-parameter.rs:2:17
   |
LL | trait Trait<T = ()> {
   |             ------ expected to match the default of type parameter `T`
LL |     fn foo() -> T;
   |                 ^
   = note: expected signature `fn() -> ()`
              found signature `fn() -> u32`
help: change the output type to match the trait
   |
LL -     fn foo() -> u32 { 0 }
LL +     fn foo() -> () { 0 }
   |

Fix #67991.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 29, 2026
@rust-bors

This comment has been minimized.

@estebank
estebank marked this pull request as ready for review October 5, 2026 20:01
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 5, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 5, 2026
@rustbot

rustbot commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

r? @tiif

rustbot has assigned @tiif.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 77 candidates
  • Random selection from 19 candidates

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@tiif tiif left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for working on this! I have some non-blocking style nits that I think might be good to have. I am currently reading the code closely, it might take a few days, so I am releasing these comments for earlier feedback.

View changes since this review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add the issue number here and comment on what this test is about?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is the link enough?

--> $DIR/defaults-specialization.rs:8:18
|
LL | trait Tr {
...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

non-blocking style nit: will it be possible to have...?

    LL | trait Tr {
       |  ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That change is something to bring up to rust-lang/annotate-snippets-rs. I don't think that rendering is necessarily ideal, because it would be hard to differentiate between the actual code being

trait Tr {
 ...

and

trait Tr {
// this like will be skipped by the diagnostic renderer and replaced with ` ...`

By having it on the other side of the gutter, it makes that a bit clearer.

Comment thread tests/ui/compare-method/bad-self-type.stderr
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 19, 2026
@rustbot

rustbot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

This becomes particularly confusing when a type parameter has a default.

```
  --> $DIR/default-type-parameter.rs:8:17
   |
LL |     fn foo() -> u32 { 0 }
   |                 ^^^ expected `()`, found `u32`
   |
note: type in trait
  --> $DIR/default-type-parameter.rs:2:17
   |
LL |     fn foo() -> T;
   |                 ^
   = note: expected signature `fn() -> ()`
              found signature `fn() -> u32`
note: expected to match this type parameter
  --> $DIR/default-type-parameter.rs:1:13
   |
LL | trait Trait<T = ()> {
   |             ^^^^^^
help: change the output type to match the trait
   |
LL -     fn foo() -> u32 { 0 }
LL +     fn foo() -> () { 0 }
```
…the trait

```
error[E0053]: method `foo` has an incompatible type for trait
  --> $DIR/async-example-desugared-boxed-in-trait.rs:11:28
   |
LL | impl MyTrait for i32 {
LL |     async fn foo(&self) -> i32 {
   |                            ^^^ expected `Pin<Box<dyn Future<Output = i32>>>`, found future
   |
note: type in trait
  --> $DIR/async-example-desugared-boxed-in-trait.rs:7:22
   |
LL | trait MyTrait {
LL |     fn foo(&self) -> Pin<Box<dyn Future<Output = i32> + '_>>;
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: expected signature `fn(&_) -> Pin<Box<dyn Future<Output = i32>>>`
              found signature `fn(&_) -> impl Future<Output = i32>`
```
…type in trait" note

```
error[E0053]: method `foo` has an incompatible type for trait
  --> $DIR/default-type-parameter.rs:8:17
   |
LL | impl Trait for S {
LL |     fn foo() -> u32 { 0 }
   |                 ^^^ expected `()`, found `u32`
   |
note: type in trait
  --> $DIR/default-type-parameter.rs:2:17
   |
LL | trait Trait<T = ()> {
   |             ------ expected to match the default of type parameter `T`
LL |     fn foo() -> T;
   |                 ^
   = note: expected signature `fn() -> ()`
              found signature `fn() -> u32`
help: change the output type to match the trait
   |
LL -     fn foo() -> u32 { 0 }
LL +     fn foo() -> () { 0 }
   |
```
@rustbot

rustbot commented Oct 20, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@estebank estebank 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 20, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incompatible type error should explain when types come from default type parameters

4 participants