about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeón Orell Valerian Liehr <me@fmease.dev>2025-02-26 19:03:56 +0100
committerGitHub <noreply@github.com>2025-02-26 19:03:56 +0100
commit2fc88233cd927c55196844e671a9c6aaca5b043d (patch)
tree612d9e4907fcc8a2fb1b3f1f0eb2ff6bbbb34f88
parentc5d57274c03072941f4b3c579498c2c11a3ceff2 (diff)
parentb7a549725c98de36144e1257b5fbca5372d82a51 (diff)
downloadrust-2fc88233cd927c55196844e671a9c6aaca5b043d.tar.gz
rust-2fc88233cd927c55196844e671a9c6aaca5b043d.zip
Rollup merge of #137631 - TaKO8Ki:issue-137508, r=compiler-errors
Avoid collecting associated types for undefined trait

Fixes #137508
Fixes #137554
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs6
-rw-r--r--tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs8
-rw-r--r--tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr25
-rw-r--r--tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs12
-rw-r--r--tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr28
5 files changed, 78 insertions, 1 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
index d644a1f224c..922578792ba 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
@@ -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,
diff --git a/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs
new file mode 100644
index 00000000000..b90bb9ea4dd
--- /dev/null
+++ b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs
@@ -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!()
+}
diff --git a/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr
new file mode 100644
index 00000000000..7f0fbc800ed
--- /dev/null
+++ b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr
@@ -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`.
diff --git a/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs
new file mode 100644
index 00000000000..f6b749a5100
--- /dev/null
+++ b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs
@@ -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
+}
diff --git a/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr
new file mode 100644
index 00000000000..f31a1de76a7
--- /dev/null
+++ b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr
@@ -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`.