Skip to content

Commit bae3fbb

Browse files
Do not ICE on generic type mismatch in anonymous const
1 parent 15d1f81 commit bae3fbb

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1660,10 +1660,19 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16601660
// it's a actual definition. According to the comments (e.g. in
16611661
// librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter
16621662
// is relied upon by some other code. This might (or might not) need cleanup.
1663-
let body_owner_def_id =
1663+
let mut body_owner_def_id =
16641664
self.tcx.hir().opt_local_def_id(cause.body_id).unwrap_or_else(|| {
16651665
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
16661666
});
1667+
1668+
if let hir::def::DefKind::AnonConst = self.tcx.hir().def_kind(body_owner_def_id) {
1669+
// #80062: we need to pass the containing function to access the generics properly
1670+
body_owner_def_id = self
1671+
.tcx
1672+
.hir()
1673+
.get_parent_did(self.tcx.hir().local_def_id_to_hir_id(body_owner_def_id));
1674+
}
1675+
16671676
self.check_and_note_conflicting_crates(diag, terr);
16681677
self.tcx.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id.to_def_id());
16691678

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Regression test for issue #80062
2+
3+
fn foo<Foo>() -> Foo { todo!() }
4+
5+
fn bar<T>() {
6+
let _: [u8; foo::<T>()];
7+
//~^ ERROR the size for values of type `T` cannot be known at compilation time
8+
//~^^ ERROR mismatched types
9+
//~^^^ ERROR the size for values of type `T` cannot be known at compilation time
10+
}
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error[E0277]: the size for values of type `T` cannot be known at compilation time
2+
--> $DIR/issue-80062-mismatched-types-anon-const.rs:6:23
3+
|
4+
LL | fn foo<Foo>() -> Foo { todo!() }
5+
| --- required by this bound in `foo`
6+
LL |
7+
LL | fn bar<T>() {
8+
| - this type parameter needs to be `Sized`
9+
LL | let _: [u8; foo::<T>()];
10+
| ^ doesn't have a size known at compile-time
11+
|
12+
help: consider relaxing the implicit `Sized` restriction
13+
|
14+
LL | fn foo<Foo: ?Sized>() -> Foo { todo!() }
15+
| ^^^^^^^^
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/issue-80062-mismatched-types-anon-const.rs:6:17
19+
|
20+
LL | fn bar<T>() {
21+
| - this type parameter
22+
LL | let _: [u8; foo::<T>()];
23+
| ^^^^^^^^^^ expected `usize`, found type parameter `T`
24+
|
25+
= note: expected type `usize`
26+
found type parameter `T`
27+
28+
error[E0277]: the size for values of type `T` cannot be known at compilation time
29+
--> $DIR/issue-80062-mismatched-types-anon-const.rs:6:17
30+
|
31+
LL | fn bar<T>() {
32+
| - this type parameter needs to be `Sized`
33+
LL | let _: [u8; foo::<T>()];
34+
| ^^^^^^^^ doesn't have a size known at compile-time
35+
|
36+
= note: the return type of a function must have a statically known size
37+
38+
error: aborting due to 3 previous errors
39+
40+
Some errors have detailed explanations: E0277, E0308.
41+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)