diff --git a/crates/hir-ty/src/infer/expr.rs b/crates/hir-ty/src/infer/expr.rs index db3303607bad..50bc70f4ee50 100644 --- a/crates/hir-ty/src/infer/expr.rs +++ b/crates/hir-ty/src/infer/expr.rs @@ -1336,14 +1336,18 @@ impl<'db> InferenceContext<'_, 'db> { ExprIsRead::Yes, ); let usize = self.types.usize; - match self.body[repeat] { + let len = match self.body[repeat] { Expr::Underscore => { self.write_expr_ty(repeat, usize); + self.table.next_const_var() } - _ => _ = self.infer_expr(repeat, &Expectation::HasType(usize), ExprIsRead::Yes), - } + _ => { + self.infer_expr(repeat, &Expectation::HasType(usize), ExprIsRead::Yes); + consteval::eval_to_const(repeat, self) + } + }; - (elem_ty, consteval::eval_to_const(repeat, self)) + (elem_ty, len) } }; // Try to evaluate unevaluated constant, and insert variable if is not possible. diff --git a/crates/hir-ty/src/tests/simple.rs b/crates/hir-ty/src/tests/simple.rs index 478411f5a84f..2e107b2c5941 100644 --- a/crates/hir-ty/src/tests/simple.rs +++ b/crates/hir-ty/src/tests/simple.rs @@ -3943,3 +3943,16 @@ fn foo() { "#]], ); } + +#[test] +fn infer_array_size() { + check_no_mismatches( + r#" +fn foo(a: [u8; 3]) {} + +fn bar() { + foo([0; _]); +} + "#, + ); +}