-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Open
Labels
A-type-systemArea: Type systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.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.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Description
I tried this code:
pub trait Super {
type X;
// Change return type to `()` and everything is fine.
fn method(&self) -> Self::X;
}
// Remove the `: Super` and everything is fine.
pub trait Helper: Super {}
impl<T: Helper> Super for T {
type X = ();
fn method(&self) {}
}
I expected to see this happen: The code compiles
Instead, this happened: The code fails to compile.
Maybe this is working as intended, but intuitively I wouldn't have expected the Helper: Super
to make a difference for the impl Super for T
.
The type error is a lot more confusing if the associated type is implicit, (i.e. -> impl Future
or async fn
), because that results in "expected signature fn(&_) -> X
, found signature fn(&_) -> X
"
error[E0053]: method `method` has an incompatible type for trait
--> src/lib.rs:14:21
|
14 | fn method(&self) {}
| ^ expected associated type, found `()`
|
note: type in trait
--> src/lib.rs:5:25
|
5 | fn method(&self) -> Self::X;
| ^^^^^^^
= note: expected signature `fn(&_) -> <T as Super>::X`
found signature `fn(&_) -> ()`
Meta
rustc --version --verbose
:
Build using the Nightly version: 1.89.0-nightly(2025-05-30 70b3f4666e24ce22fc32)
Metadata
Metadata
Assignees
Labels
A-type-systemArea: Type systemArea: Type systemC-discussionCategory: Discussion or questions that doesn't represent real issues.Category: Discussion or questions that doesn't represent real issues.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.T-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.