Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid collecting associated types for undefined trait #137631

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {

Some(args.constraints.iter().filter_map(|constraint| {
let ident = constraint.ident;
let trait_def = path.res.def_id();

let Res::Def(DefKind::Trait, trait_def) = path.res else {
return None;
};

let assoc_item = tcx.associated_items(trait_def).find_by_name_and_kind(
tcx,
ident,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Regression test for <https://github.com/rust-lang/rust/issues/137554>.

fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
//~^ ERROR `?Trait` is not permitted in trait object types
//~| ERROR expected trait, found associated function `Iterator::advance_by`
//~| ERROR the value of the associated type `Item` in `Iterator` must be specified
todo!()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
error[E0658]: `?Trait` is not permitted in trait object types
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:29
|
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

error[E0404]: expected trait, found associated function `Iterator::advance_by`
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:30
|
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a trait

error[E0191]: the value of the associated type `Item` in `Iterator` must be specified
--> $DIR/missing-associated_item_or_field_def_ids.rs:3:18
|
LL | fn main() -> dyn Iterator + ?Iterator::advance_by(usize) {
| ^^^^^^^^ help: specify the associated type: `Iterator<Item = Type>`

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0191, E0404, E0658.
For more information about an error, try `rustc --explain E0191`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Fix for <https://github.com/rust-lang/rust/issues/137508>.

trait Tr {
type Item;
}

fn main() {
let _: dyn Tr + ?Foo<Assoc = ()>;
//~^ ERROR: `?Trait` is not permitted in trait object types
//~| ERROR: cannot find trait `Foo` in this scope
//~| ERROR: the value of the associated type `Item` in `Tr` must be specified
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0658]: `?Trait` is not permitted in trait object types
--> $DIR/avoid-getting-associated-items-of-undefined-trait.rs:8:21
|
LL | let _: dyn Tr + ?Foo<Assoc = ()>;
| ^^^^^^^^^^^^^^^^
|
= help: add `#![feature(more_maybe_bounds)]` to the crate attributes to enable
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date

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

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

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0191, E0405, E0658.
For more information about an error, try `rustc --explain E0191`.
Loading