diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index 6b7edde9a67af..64eec8050b0d1 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -1660,10 +1660,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { // it's a actual definition. According to the comments (e.g. in // librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter // is relied upon by some other code. This might (or might not) need cleanup. - let body_owner_def_id = + let mut body_owner_def_id = self.tcx.hir().opt_local_def_id(cause.body_id).unwrap_or_else(|| { self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id }) }); + + if let hir::def::DefKind::AnonConst = self.tcx.hir().def_kind(body_owner_def_id) { + // #80062: we need to pass the containing function to access the generics properly + body_owner_def_id = self + .tcx + .hir() + .get_parent_did(self.tcx.hir().local_def_id_to_hir_id(body_owner_def_id)); + } + self.check_and_note_conflicting_crates(diag, terr); self.tcx.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id.to_def_id()); diff --git a/src/test/ui/typeck/issue-80062-mismatched-types-anon-const.rs b/src/test/ui/typeck/issue-80062-mismatched-types-anon-const.rs new file mode 100644 index 0000000000000..c256b8bf61d1e --- /dev/null +++ b/src/test/ui/typeck/issue-80062-mismatched-types-anon-const.rs @@ -0,0 +1,12 @@ +// Regression test for issue #80062 + +fn foo() -> Foo { todo!() } + +fn bar() { + let _: [u8; foo::()]; + //~^ ERROR the size for values of type `T` cannot be known at compilation time + //~^^ ERROR mismatched types + //~^^^ ERROR the size for values of type `T` cannot be known at compilation time +} + +fn main() {} diff --git a/src/test/ui/typeck/issue-80062-mismatched-types-anon-const.stderr b/src/test/ui/typeck/issue-80062-mismatched-types-anon-const.stderr new file mode 100644 index 0000000000000..2385e1f596d42 --- /dev/null +++ b/src/test/ui/typeck/issue-80062-mismatched-types-anon-const.stderr @@ -0,0 +1,41 @@ +error[E0277]: the size for values of type `T` cannot be known at compilation time + --> $DIR/issue-80062-mismatched-types-anon-const.rs:6:23 + | +LL | fn foo() -> Foo { todo!() } + | --- required by this bound in `foo` +LL | +LL | fn bar() { + | - this type parameter needs to be `Sized` +LL | let _: [u8; foo::()]; + | ^ doesn't have a size known at compile-time + | +help: consider relaxing the implicit `Sized` restriction + | +LL | fn foo() -> Foo { todo!() } + | ^^^^^^^^ + +error[E0308]: mismatched types + --> $DIR/issue-80062-mismatched-types-anon-const.rs:6:17 + | +LL | fn bar() { + | - this type parameter +LL | let _: [u8; foo::()]; + | ^^^^^^^^^^ expected `usize`, found type parameter `T` + | + = note: expected type `usize` + found type parameter `T` + +error[E0277]: the size for values of type `T` cannot be known at compilation time + --> $DIR/issue-80062-mismatched-types-anon-const.rs:6:17 + | +LL | fn bar() { + | - this type parameter needs to be `Sized` +LL | let _: [u8; foo::()]; + | ^^^^^^^^ doesn't have a size known at compile-time + | + = note: the return type of a function must have a statically known size + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0277, E0308. +For more information about an error, try `rustc --explain E0277`.