rust-lang / rust-lang/rust

Invalid help suggestion for specialization on associated type with `Self`

Open
#117,841 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics F-specialization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic using the Rust snippets in src/lib.rs, then compare the current and desired E0308 help suggestions, including the additional cases. Use rustc --explain E0308 for context; done means the suggestions use the MyTrait bound form shown in the desired output and no longer produce syntactically or logically incorrect bounds.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.