Merge pull request #21061 from ChayimFriedman2/infer-array-size

fix: Allow inferring array sizes
This commit is contained in:
Lukas Wirth
2025-11-19 08:36:42 +00:00
committed by GitHub
2 changed files with 21 additions and 4 deletions
@@ -1336,14 +1336,18 @@ fn infer_expr_array(&mut self, array: &Array, expected: &Expectation<'db>) -> Ty
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.
@@ -3943,3 +3943,16 @@ fn foo() {
"#]],
);
}
#[test]
fn infer_array_size() {
check_no_mismatches(
r#"
fn foo(a: [u8; 3]) {}
fn bar() {
foo([0; _]);
}
"#,
);
}