diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 4db9a4c2bcdd8..9b7fc9cf6af35 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -1522,7 +1522,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { // as potential fallback candidates (#142006). To temporarily mask that issue, let's not // select at all if there are no early inherent candidates. if candidates.is_empty() { - return Ok(None); + return match assoc_tag { + ty::AssocTag::Type => Ok(None), + ty::AssocTag::Const => Err(self.report_unresolved_inherent_assoc_item( + name, + self_ty, + candidates, + vec![], + span, + assoc_tag, + )), + ty::AssocTag::Fn => unreachable!(), + }; } let (applicable_candidates, fulfillment_errors) = diff --git a/tests/ui/associated-inherent-types/iat-const-resolution.rs b/tests/ui/associated-inherent-types/iat-const-resolution.rs new file mode 100644 index 0000000000000..269200a20c30b --- /dev/null +++ b/tests/ui/associated-inherent-types/iat-const-resolution.rs @@ -0,0 +1,18 @@ +//! regression test for #148949, #149809 +#![expect(incomplete_features)] +#![feature(min_generic_const_args)] +#![feature(inherent_associated_types)] +struct Foo<'a> { + x: &'a (), +} + +impl<'a> Foo<'a> { + fn bar(_: [u8; Foo::X]) {} + //~^ ERROR: associated constant `X` not found for `Foo<'_>` in the current scope + // #[type_const] + // const Y: usize = 10; + // fn foo(_: [u8; Foo::Y]) {} + // associated constant `Y` not found for `Foo<'_>` in the current scope +} + +fn main() {} diff --git a/tests/ui/associated-inherent-types/iat-const-resolution.stderr b/tests/ui/associated-inherent-types/iat-const-resolution.stderr new file mode 100644 index 0000000000000..9289c2f198999 --- /dev/null +++ b/tests/ui/associated-inherent-types/iat-const-resolution.stderr @@ -0,0 +1,15 @@ +error[E0220]: associated constant `X` not found for `Foo<'_>` in the current scope + --> $DIR/iat-const-resolution.rs:10:25 + | +LL | struct Foo<'a> { + | -------------- associated constant `X` not found for this struct +... +LL | fn bar(_: [u8; Foo::X]) {} + | ^ associated item not found in `Foo<'_>` + | + = note: the associated constant was found for + + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0220`.