Skip to content

Invalid help suggestion for specialization on associated type with Self  #117841

Open
@polina4096

Description

@polina4096

Code

#![feature(specialization)]

trait Other<A> {}

trait MyTrait {
	type T;
	fn foo(self);
}

impl<X: Other<()>> MyTrait for X {
	default type T = Self;
	fn foo(self) {
		let _: Self::T = self;
	}
}

Current output

warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![feature(specialization)]
  |            ^^^^^^^^^^^^^^
  |
  = note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
  = help: consider using `min_specialization` instead, which is more stable and complete
  = note: `#[warn(incomplete_features)]` on by default

error[E0308]: mismatched types
  --> src/lib.rs:13:20
   |
10 | impl<X: Other<()>> MyTrait for X {
   |      - found this type parameter
...
13 |         let _: Self::T = self;
   |                -------   ^^^^ expected associated type, found type parameter `X`
   |                |
   |                expected due to this
   |
   = note: expected associated type `<X as MyTrait>::T`
               found type parameter `X`
help: consider further restricting this bound
   |
10 | impl<X: Other<()><T = X>> MyTrait for X {
   |                  +++++++

For more information about this error, try `rustc --explain E0308`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to previous error; 1 warning emitted

Desired output

warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![feature(specialization)]
  |            ^^^^^^^^^^^^^^
  |
  = note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
  = help: consider using `min_specialization` instead, which is more stable and complete
  = note: `#[warn(incomplete_features)]` on by default

error[E0308]: mismatched types
  --> src/lib.rs:13:20
   |
10 | impl<X: Other<()>> MyTrait for X {
   |      - found this type parameter
...
13 |         let _: Self::T = self;
   |                -------   ^^^^ expected associated type, found type parameter `X`
   |                |
   |                expected due to this
   |
   = note: expected associated type `<X as MyTrait>::T`
               found type parameter `X`
help: consider further restricting this bound
   |
10 | impl<X: Other<()> + MyTrait<T = X>> MyTrait for X {
   |                  +++++++++++++++++

For more information about this error, try `rustc --explain E0308`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to previous error; 1 warning emitted

Rationale and extra context

The compiler incorrectly handles specialized associated type bounds in diagnostic messages, which leads to certain incorrect help suggestions.

Other cases

// suggests `<X: <T = X>>` which is syntactically incorrect
#![feature(specialization)]

trait MyTrait {
	type T;
	fn foo(self);
}

impl<X> MyTrait for X {
	default type T = Self;
	fn foo(self) {
		let _: Self::T = self;
	}
}

// suggests `<X: Other<T = X>>` which is logically incorrect
#![feature(specialization)]

trait Other {}

trait MyTrait {
	type T;
	fn foo(self);
}

impl<X: Other> MyTrait for X {
	default type T = Self;
	fn foo(self) {
		let _: Self::T = self;
	}
}

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsF-specialization`#![feature(specialization)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions