Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate ProjectionTy::trait_def_id for return-position impl Trait in trait correctly #102152

Merged
merged 1 commit into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,9 +1133,13 @@ pub struct ProjectionTy<'tcx> {

impl<'tcx> ProjectionTy<'tcx> {
pub fn trait_def_id(&self, tcx: TyCtxt<'tcx>) -> DefId {
let parent = tcx.parent(self.item_def_id);
assert_eq!(tcx.def_kind(parent), DefKind::Trait);
parent
match tcx.def_kind(self.item_def_id) {
DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.item_def_id),
DefKind::ImplTraitPlaceholder => {
tcx.parent(tcx.impl_trait_in_trait_parent(self.item_def_id))
}
kind => bug!("unexpected DefKind in ProjectionTy: {kind:?}"),
}
}

/// Extracts the underlying trait reference and own substs from this projection.
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/impl-trait/in-trait/issue-102140.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]

trait Marker {}
impl Marker for u32 {}

trait MyTrait {
fn foo(&self) -> impl Marker
where
Self: Sized;
}

struct Outer;

impl MyTrait for Outer {
fn foo(&self) -> impl Marker {
42
}
}

impl dyn MyTrait {
fn other(&self) -> impl Marker {
MyTrait::foo(&self)
//~^ ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
//~| ERROR the trait bound `&dyn MyTrait: MyTrait` is not satisfied
}
}

fn main() {}
29 changes: 29 additions & 0 deletions src/test/ui/impl-trait/in-trait/issue-102140.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
--> $DIR/issue-102140.rs:23:22
|
LL | MyTrait::foo(&self)
| ------------ -^^^^
| | |
| | the trait `MyTrait` is not implemented for `&dyn MyTrait`
| | help: consider removing the leading `&`-reference
| required by a bound introduced by this call

error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
--> $DIR/issue-102140.rs:23:9
|
LL | MyTrait::foo(&self)
| ^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
|
= help: the trait `MyTrait` is implemented for `Outer`

error[E0277]: the trait bound `&dyn MyTrait: MyTrait` is not satisfied
--> $DIR/issue-102140.rs:23:9
|
LL | MyTrait::foo(&self)
| ^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `&dyn MyTrait`
|
= help: the trait `MyTrait` is implemented for `Outer`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0277`.