Skip to content

Commit 79c665b

Browse files
Calculate ProjectionTy::trait_def_id correctly
1 parent 89e4e1f commit 79c665b

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

compiler/rustc_middle/src/ty/sty.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1133,9 +1133,13 @@ pub struct ProjectionTy<'tcx> {
11331133

11341134
impl<'tcx> ProjectionTy<'tcx> {
11351135
pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId {
1136-
let parent = tcx.parent(self.item_def_id);
1137-
assert_eq!(tcx.def_kind(parent), DefKind::Trait);
1138-
parent
1136+
match tcx.def_kind(self.item_def_id) {
1137+
DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.item_def_id),
1138+
DefKind::ImplTraitPlaceholder => {
1139+
tcx.parent(tcx.impl_trait_in_trait_parent(self.item_def_id))
1140+
}
1141+
kind => bug!("unexpected DefKind in ProjectionTy: {kind:?}"),
1142+
}
11391143
}
11401144

11411145
/// Extracts the underlying trait reference and own substs from this projection.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![feature(return_position_impl_trait_in_trait)]
2+
#![allow(incomplete_features)]
3+
4+
trait Marker {}
5+
impl Marker for u32 {}
6+
7+
trait MyTrait {
8+
fn foo(&self) -> impl Marker
9+
where
10+
Self: Sized;
11+
}
12+
13+
struct Outer;
14+
15+
impl MyTrait for Outer {
16+
fn foo(&self) -> impl Marker {
17+
42
18+
}
19+
}
20+
21+
impl dyn MyTrait {
22+
fn other(&self) -> impl Marker {
23+
MyTrait::foo(&self)
24+
//~^ ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
25+
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
26+
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
27+
}
28+
}
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
2+
--> $DIR/issue-102140.rs:23:22
3+
|
4+
LL | MyTrait::foo(&self)
5+
| ------------ -^^^^
6+
| | |
7+
| | the trait `MyTrait` is not implemented for `&dyn MyTrait`
8+
| | help: consider removing the leading `&`-reference
9+
| required by a bound introduced by this call
10+
11+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
12+
--> $DIR/issue-102140.rs:23:9
13+
|
14+
LL | MyTrait::foo(&self)
15+
| ^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
16+
|
17+
= help: the trait `MyTrait` is implemented for `Outer`
18+
19+
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
20+
--> $DIR/issue-102140.rs:23:9
21+
|
22+
LL | MyTrait::foo(&self)
23+
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
24+
|
25+
= help: the trait `MyTrait` is implemented for `Outer`
26+
27+
error: aborting due to 3 previous errors
28+
29+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)