Skip to content
Closed
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: 5 additions & 5 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,12 +554,12 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> 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
}
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/coherence/impl-for-assoc-with-ty-var.rs
Original file line number Diff line number Diff line change
@@ -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<M> MyTrait for Pair<M, M> {
type Item = Pair<M, M>;
}

impl<K> Remote for <Pair<K, K> 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() {}
22 changes: 22 additions & 0 deletions tests/ui/coherence/impl-for-assoc-with-ty-var.stderr
Original file line number Diff line number Diff line change
@@ -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<K> Remote for <Pair<K, K> 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<K> Remote for <Pair<K, K> 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`.
Loading