diff --git a/compiler/rustc_hir_analysis/src/coherence/orphan.rs b/compiler/rustc_hir_analysis/src/coherence/orphan.rs index 8a1a887766c7..123c5d1d6a3f 100644 --- a/compiler/rustc_hir_analysis/src/coherence/orphan.rs +++ b/compiler/rustc_hir_analysis/src/coherence/orphan.rs @@ -554,12 +554,12 @@ impl<'cx, 'tcx> TypeFolder> for TyVarReplacer<'cx, 'tcx> { return ty.super_fold_with(self); }; let origin = self.infcx.type_var_origin(vid); - if let Some(def_id) = origin.param_def_id { + if let Some(def_id) = origin.param_def_id + && let Some(index) = self.generics.param_def_id_to_index.get(&def_id) + { // The generics of an `impl` don't have a parent, we can index directly. - let index = self.generics.param_def_id_to_index[&def_id]; - let name = self.generics.own_params[index as usize].name; - - Ty::new_param(self.infcx.tcx, index, name) + let name = self.generics.own_params[*index as usize].name; + Ty::new_param(self.infcx.tcx, *index, name) } else { ty } diff --git a/tests/ui/coherence/impl-for-assoc-with-ty-var.rs b/tests/ui/coherence/impl-for-assoc-with-ty-var.rs new file mode 100644 index 000000000000..c0df792a03c6 --- /dev/null +++ b/tests/ui/coherence/impl-for-assoc-with-ty-var.rs @@ -0,0 +1,21 @@ +//@ aux-build:coherence_lib.rs + +// issue#132826 + +extern crate coherence_lib; + +use coherence_lib::{Pair, Remote, Remote1}; + +trait MyTrait { + type Item; +} + +impl MyTrait for Pair { + type Item = Pair; +} + +impl Remote for as MyTrait>::Item {} +//~^ ERROR: the type parameter `K` is not constrained by the impl trait, self type, or predicates +//~| ERROR: only traits defined in the current crate can be implemented for arbitrary types + +fn main() {} diff --git a/tests/ui/coherence/impl-for-assoc-with-ty-var.stderr b/tests/ui/coherence/impl-for-assoc-with-ty-var.stderr new file mode 100644 index 000000000000..4cdf5e0d4816 --- /dev/null +++ b/tests/ui/coherence/impl-for-assoc-with-ty-var.stderr @@ -0,0 +1,22 @@ +error[E0207]: the type parameter `K` is not constrained by the impl trait, self type, or predicates + --> $DIR/impl-for-assoc-with-ty-var.rs:17:6 + | +LL | impl Remote for as MyTrait>::Item {} + | ^ unconstrained type parameter + +error[E0117]: only traits defined in the current crate can be implemented for arbitrary types + --> $DIR/impl-for-assoc-with-ty-var.rs:17:1 + | +LL | impl Remote for as MyTrait>::Item {} + | ^^^^^^^^^^^^^^^^^^^----------------------------- + | | + | `Pair` is not defined in the current crate + | + = note: impl doesn't have any local type before any uncovered type parameters + = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules + = note: define and implement a trait or new type instead + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0117, E0207. +For more information about an error, try `rustc --explain E0117`.