Skip to content

Commit 2fc8823

Browse files
authored
Rollup merge of rust-lang#137631 - TaKO8Ki:issue-137508, r=compiler-errors
Avoid collecting associated types for undefined trait Fixes rust-lang#137508 Fixes rust-lang#137554
2 parents c5d5727 + b7a5497 commit 2fc8823

5 files changed

+78
-1
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,11 @@ 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.def_id();
792+
793+
let Res::Def(DefKind::Trait, trait_def) = path.res else {
794+
return None;
795+
};
796+
793797
let assoc_item = tcx.associated_items(trait_def).find_by_name_and_kind(
794798
tcx,
795799
ident,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/137554>.
2+
3+
fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
4+
//~^ ERROR `?Trait` is not permitted in trait object types
5+
//~| ERROR expected trait, found associated function `Iterator::advance_by`
6+
//~| ERROR the value of the associated type `Item` in `Iterator` must be specified
7+
todo!()
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0658]: `?Trait` is not permitted in trait object types
2+
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:29
3+
|
4+
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
10+
error[E0404]: expected trait, found associated function `Iterator::advance_by`
11+
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:30
12+
|
13+
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a trait
15+
16+
error[E0191]: the value of the associated type `Item` in `Iterator` must be specified
17+
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:18
18+
|
19+
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
20+
| ^^^^^^^^ help: specify the associated type: `Iterator<Item = Type>`
21+
22+
error: aborting due to 3 previous errors
23+
24+
Some errors have detailed explanations: E0191, E0404, E0658.
25+
For more information about an error, try `rustc --explain E0191`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Fix for <https://github.com/rust-lang/rust/issues/137508>.
2+
3+
trait Tr {
4+
type Item;
5+
}
6+
7+
fn main() {
8+
let _: dyn Tr + ?Foo<Assoc = ()>;
9+
//~^ ERROR: `?Trait` is not permitted in trait object types
10+
//~| ERROR: cannot find trait `Foo` in this scope
11+
//~| ERROR: the value of the associated type `Item` in `Tr` must be specified
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0658]: `?Trait` is not permitted in trait object types
2+
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:21
3+
|
4+
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
8+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
9+
10+
error[E0405]: cannot find trait `Foo` in this scope
11+
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:22
12+
|
13+
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
14+
| ^^^ not found in this scope
15+
16+
error[E0191]: the value of the associated type `Item` in `Tr` must be specified
17+
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:16
18+
|
19+
LL | type Item;
20+
| --------- `Item` defined here
21+
...
22+
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
23+
| ^^ help: specify the associated type: `Tr<Item = Type>`
24+
25+
error: aborting due to 3 previous errors
26+
27+
Some errors have detailed explanations: E0191, E0405, E0658.
28+
For more information about an error, try `rustc --explain E0191`.

0 commit comments

Comments
 (0)