From b7a549725c98de36144e1257b5fbca5372d82a51 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 26 Feb 2025 04:12:20 +0900 Subject: [PATCH] fix #137508 rename ui tests check if res is trait def fix typo regression test for #137554 --- .../src/hir_ty_lowering/errors.rs | 6 +++- ...issing-associated_item_or_field_def_ids.rs | 8 ++++++ ...ng-associated_item_or_field_def_ids.stderr | 25 +++++++++++++++++ ...ing-associated-items-of-undefined-trait.rs | 12 ++++++++ ...associated-items-of-undefined-trait.stderr | 28 +++++++++++++++++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs create mode 100644 tests/ui/associated-item/missing-associated_item_or_field_def_ids.stderr create mode 100644 tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs create mode 100644 tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.stderr 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 7eb982a31798c..1b1f4bf44188a 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 0000000000000..b90bb9ea4ddf6 --- /dev/null +++ b/tests/ui/associated-item/missing-associated_item_or_field_def_ids.rs @@ -0,0 +1,8 @@ +// Regression test for . + +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 0000000000000..7f0fbc800ed1c --- /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` + +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 0000000000000..f6b749a5100b9 --- /dev/null +++ b/tests/ui/associated-types/avoid-getting-associated-items-of-undefined-trait.rs @@ -0,0 +1,12 @@ +// Fix for . + +trait Tr { + type Item; +} + +fn main() { + let _: dyn Tr + ?Foo; + //~^ 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 0000000000000..f31a1de76a790 --- /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; + | ^^^^^^^^^^^^^^^^ + | + = 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; + | ^^^ 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; + | ^^ help: specify the associated type: `Tr` + +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`.