From fb94e5883ed1b648ae00fd3bc85e3dc002068a78 Mon Sep 17 00:00:00 2001 From: Shoyu Vanilla Date: Wed, 18 Feb 2026 19:12:34 +0900 Subject: [PATCH] Fix an ICE while checking param env shadowing on an erroneous trait impl --- .../src/error_reporting/infer/mod.rs | 6 ++++ ...uggestion-no-ice-on-missing-assoc-value.rs | 18 +++++++++++ ...stion-no-ice-on-missing-assoc-value.stderr | 32 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs create mode 100644 tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.stderr diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs index 6003461f35e8..2e1657a8dc2c 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs @@ -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); diff --git a/tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs b/tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs new file mode 100644 index 000000000000..6f0cd74d2ffe --- /dev/null +++ b/tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs @@ -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 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() {} diff --git a/tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.stderr b/tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.stderr new file mode 100644 index 000000000000..9d4f7c58bd7a --- /dev/null +++ b/tests/ui/associated-types/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.stderr @@ -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 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`.