Skip to content

Commit 33aea4c

Browse files
Rollup merge of rust-lang#152792 - ShoyuVanilla:issue-152663, r=petrochenkov
Fix an ICE while checking param env shadowing on an erroneous trait impl Fixes rust-lang#152663
2 parents b62c6d0 + fb94e58 commit 33aea4c

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,12 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
299299
let trait_def_id = alias.trait_def_id(tcx);
300300
let rebased_args = alias.args.rebase_onto(tcx, trait_def_id, impl_substs);
301301

302+
// The impl is erroneous missing a definition for the associated type.
303+
// Skipping it since calling `TyCtxt::type_of` on its assoc ty will trigger an ICE.
304+
if !leaf_def.item.defaultness(tcx).has_value() {
305+
return false;
306+
}
307+
302308
let impl_item_def_id = leaf_def.item.def_id;
303309
if !tcx.check_args_compatible(impl_item_def_id, rebased_args) {
304310
return false;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// A regression test for https://github.com/rust-lang/rust/issues/152663
2+
// Previously triggered an ICE when checking whether the param-env
3+
// shadows a global impl. The crash occurred due to calling
4+
// `TyCtxt::type_of` on an erroneous associated type in a trait impl
5+
// that had no corresponding value.
6+
7+
trait Iterable {
8+
type Iter;
9+
}
10+
11+
impl<T> Iterable for [T] {
12+
//~^ ERROR: not all trait items implemented
13+
fn iter() -> Self::Iter {}
14+
//~^ ERROR: method `iter` is not a member of trait `Iterable`
15+
//~| ERROR: mismatched types
16+
}
17+
18+
fn main() {}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0407]: method `iter` is not a member of trait `Iterable`
2+
--> $DIR/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs:13:5
3+
|
4+
LL | fn iter() -> Self::Iter {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not a member of trait `Iterable`
6+
7+
error[E0046]: not all trait items implemented, missing: `Iter`
8+
--> $DIR/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs:11:1
9+
|
10+
LL | type Iter;
11+
| --------- `Iter` from trait
12+
...
13+
LL | impl<T> Iterable for [T] {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^ missing `Iter` in implementation
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/param-env-shadowing-suggestion-no-ice-on-missing-assoc-value.rs:13:18
18+
|
19+
LL | fn iter() -> Self::Iter {}
20+
| ---- ^^^^^^^^^^ expected associated type, found `()`
21+
| |
22+
| implicitly returns `()` as its body has no tail or `return` expression
23+
|
24+
= note: expected associated type `<[T] as Iterable>::Iter`
25+
found unit type `()`
26+
= help: consider constraining the associated type `<[T] as Iterable>::Iter` to `()` or calling a method that returns `<[T] as Iterable>::Iter`
27+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
28+
29+
error: aborting due to 3 previous errors
30+
31+
Some errors have detailed explanations: E0046, E0308, E0407.
32+
For more information about an error, try `rustc --explain E0046`.

0 commit comments

Comments
 (0)