Skip to content

Commit 3c8cf6d

Browse files
committed
Avoid ICEing when associated type bound trait is missing
1 parent 5b6f206 commit 3c8cf6d

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

compiler/rustc_typeck/src/collect.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -652,13 +652,21 @@ impl ItemCtxt<'tcx> {
652652
match b {
653653
hir::GenericBound::Trait(poly_trait_ref, _) => {
654654
let trait_ref = &poly_trait_ref.trait_ref;
655-
let trait_did = trait_ref.trait_def_id().unwrap();
656-
super_traits_of(self.tcx, trait_did).any(|trait_did| {
657-
self.tcx
658-
.associated_items(trait_did)
659-
.find_by_name_and_kind(self.tcx, assoc_name, ty::AssocKind::Type, trait_did)
660-
.is_some()
661-
})
655+
if let Some(trait_did) = trait_ref.trait_def_id() {
656+
super_traits_of(self.tcx, trait_did).any(|trait_did| {
657+
self.tcx
658+
.associated_items(trait_did)
659+
.find_by_name_and_kind(
660+
self.tcx,
661+
assoc_name,
662+
ty::AssocKind::Type,
663+
trait_did,
664+
)
665+
.is_some()
666+
})
667+
} else {
668+
false
669+
}
662670
}
663671
_ => false,
664672
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[allow(dead_code)]
2+
fn foo<M>(_m: M)
3+
where
4+
M::Item: Temp,
5+
//~^ ERROR cannot find trait `Temp` in this scope [E0405]
6+
//~| ERROR associated type `Item` not found for `M` [E0220]
7+
{
8+
}
9+
10+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0405]: cannot find trait `Temp` in this scope
2+
--> $DIR/missing-trait-bound-for-assoc-fails.rs:4:14
3+
|
4+
LL | M::Item: Temp,
5+
| ^^^^ not found in this scope
6+
7+
error[E0220]: associated type `Item` not found for `M`
8+
--> $DIR/missing-trait-bound-for-assoc-fails.rs:4:8
9+
|
10+
LL | M::Item: Temp,
11+
| ^^^^ associated type `Item` not found
12+
13+
error: aborting due to 2 previous errors
14+
15+
Some errors have detailed explanations: E0220, E0405.
16+
For more information about an error, try `rustc --explain E0220`.

0 commit comments

Comments
 (0)