Fix an ICE while checking param env shadowing on an erroneous trait impl

This commit is contained in:
Shoyu Vanilla 2026-02-18 19:12:34 +09:00
parent 8387095803
commit fb94e5883e
3 changed files with 56 additions and 0 deletions

View file

@ -299,6 +299,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let trait_def_id = alias.trait_def_id(tcx);
let rebased_args = alias.args.rebase_onto(tcx, trait_def_id, impl_substs);
// The impl is erroneous missing a definition for the associated type.
// Skipping it since calling `TyCtxt::type_of` on its assoc ty will trigger an ICE.
if !leaf_def.item.defaultness(tcx).has_value() {
return false;
}
let impl_item_def_id = leaf_def.item.def_id;
let impl_assoc_ty = tcx.type_of(impl_item_def_id).instantiate(tcx, rebased_args);

View file

@ -0,0 +1,18 @@
// A regression test for https://github.com/rust-lang/rust/issues/152663
// Previously triggered an ICE when checking whether the param-env
// shadows a global impl. The crash occurred due to calling
// `TyCtxt::type_of` on an erroneous associated type in a trait impl
// that had no corresponding value.
trait Iterable {
type Iter;
}
impl<T> Iterable for [T] {
//~^ ERROR: not all trait items implemented
fn iter() -> Self::Iter {}
//~^ ERROR: method `iter` is not a member of trait `Iterable`
//~| ERROR: mismatched types
}
fn main() {}

View file

@ -0,0 +1,32 @@
error[E0407]: method `iter` is not a member of trait `Iterable`
--> $DIR/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs:13:5
|
LL | fn iter() -> Self::Iter {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Iterable`
error[E0046]: not all trait items implemented, missing: `Iter`
--> $DIR/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs:11:1
|
LL | type Iter;
| --------- `Iter` from trait
...
LL | impl<T> Iterable for [T] {
| ^^^^^^^^^^^^^^^^^^^^^^^^ missing `Iter` in implementation
error[E0308]: mismatched types
--> $DIR/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs:13:18
|
LL | fn iter() -> Self::Iter {}
| ---- ^^^^^^^^^^ expected associated type, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
|
= note: expected associated type `<[T] as Iterable>::Iter`
found unit type `()`
= help: consider constraining the associated type `<[T] as Iterable>::Iter` to `()` or calling a method that returns `<[T] as Iterable>::Iter`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0046, E0308, E0407.
For more information about an error, try `rustc --explain E0046`.