diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs index 96d00dc05fdb6..fc16edc6d1309 100644 --- a/compiler/rustc_ty_utils/src/opaque_types.rs +++ b/compiler/rustc_ty_utils/src/opaque_types.rs @@ -240,6 +240,10 @@ impl<'tcx> TypeVisitor> for OpaqueTypeCollector<'tcx> { continue; } + if !self.seen.insert(assoc.def_id.expect_local()) { + return; + } + let impl_args = alias_ty.args.rebase_onto( self.tcx, impl_trait_ref.def_id, diff --git a/tests/ui/impl-trait/associated-type-cycle.rs b/tests/ui/impl-trait/associated-type-cycle.rs new file mode 100644 index 0000000000000..4c1fc1a0fa612 --- /dev/null +++ b/tests/ui/impl-trait/associated-type-cycle.rs @@ -0,0 +1,14 @@ +trait Foo { + type Bar; + fn foo(self) -> Self::Bar; +} + +impl Foo for Box { + //~^ ERROR: the value of the associated type `Bar` in `Foo` must be specified + type Bar = ::Bar; + fn foo(self) -> ::Bar { + (*self).foo() + } +} + +fn main() {} diff --git a/tests/ui/impl-trait/associated-type-cycle.stderr b/tests/ui/impl-trait/associated-type-cycle.stderr new file mode 100644 index 0000000000000..7eef8d1e33893 --- /dev/null +++ b/tests/ui/impl-trait/associated-type-cycle.stderr @@ -0,0 +1,12 @@ +error[E0191]: the value of the associated type `Bar` in `Foo` must be specified + --> $DIR/associated-type-cycle.rs:6:22 + | +LL | type Bar; + | -------- `Bar` defined here +... +LL | impl Foo for Box { + | ^^^ help: specify the associated type: `Foo` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0191`.