Skip to content

Commit 4a77dae

Browse files
committed
check if res is trait def
1 parent 1ac2051 commit 4a77dae

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -789,15 +789,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
789789

790790
Some(args.constraints.iter().filter_map(|constraint| {
791791
let ident = constraint.ident;
792-
let trait_def = path.res.opt_def_id();
793-
let assoc_item = trait_def.and_then(|trait_def_id| {
794-
tcx.associated_items(trait_def_id).find_by_name_and_kind(
795-
tcx,
796-
ident,
797-
ty::AssocKind::Type,
798-
trait_def_id,
799-
)
800-
});
792+
793+
let Res::Def(DefKind::Trait, trait_def) = path.res else {
794+
return None;
795+
};
796+
797+
let assoc_item = tcx.associated_items(trait_def).find_by_name_and_kind(
798+
tcx,
799+
ident,
800+
ty::AssocKind::Type,
801+
trait_def,
802+
);
801803

802804
Some((ident.name, assoc_item?))
803805
}))

Diff for: tests/ui/associated-types/ice-issue-137508.rs renamed to tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Fix for https://github.com/rust-lang/rust/issues/137508>.
2+
13
trait Tr {
24
type Item;
35
}
46

57
fn main() {
6-
let _: dyn Tr + ?Foo();
8+
let _: dyn Tr + ?Foo<Assoc = ()>;
79
//~^ ERROR: `?Trait` is not permitted in trait object types
810
//~| ERROR: cannot find trait `Foo` in this scope
911
//~| ERROR: the value of the associated type `Item` in `Tr` must be specifi

Diff for: tests/ui/associated-types/ice-issue-137508.stderr renamed to tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error[E0658]: `?Trait` is not permitted in trait object types
2-
--> $DIR/issue-137508.rs:6:21
2+
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:21
33
|
4-
LL | let _: dyn Tr + ?Foo();
5-
| ^^^^^^
4+
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
5+
| ^^^^^^^^^^^^^^^^
66
|
77
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
88
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
99

1010
error[E0405]: cannot find trait `Foo` in this scope
11-
--> $DIR/issue-137508.rs:6:22
11+
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:22
1212
|
13-
LL | let _: dyn Tr + ?Foo();
13+
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
1414
| ^^^ not found in this scope
1515

1616
error[E0191]: the value of the associated type `Item` in `Tr` must be specified
17-
--> $DIR/issue-137508.rs:6:16
17+
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:16
1818
|
1919
LL | type Item;
2020
| --------- `Item` defined here
2121
...
22-
LL | let _: dyn Tr + ?Foo();
22+
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
2323
| ^^ help: specify the associated type: `Tr<Item = Type>`
2424

2525
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)