Always coerce in a cast, even when there are unknown types

This cause the relationships between inference vars to get recorded.
This commit is contained in:
Chayim Refael Friedman 2025-09-10 04:51:33 +03:00
parent 98d863c8b1
commit b2b33f9faa
2 changed files with 25 additions and 6 deletions

View file

@ -106,6 +106,13 @@ impl CastCheck {
self.expr_ty = table.eagerly_normalize_and_resolve_shallow_in(self.expr_ty.clone());
self.cast_ty = table.eagerly_normalize_and_resolve_shallow_in(self.cast_ty.clone());
// This should always come first so that we apply the coercion, which impacts infer vars.
if let Ok((adj, _)) = table.coerce(&self.expr_ty, &self.cast_ty, CoerceNever::Yes) {
apply_adjustments(self.source_expr, adj);
set_coercion_cast(self.source_expr);
return Ok(());
}
if self.expr_ty.contains_unknown() || self.cast_ty.contains_unknown() {
return Ok(());
}
@ -126,12 +133,6 @@ impl CastCheck {
return Ok(());
}
if let Ok((adj, _)) = table.coerce(&self.expr_ty, &self.cast_ty, CoerceNever::Yes) {
apply_adjustments(self.source_expr, adj);
set_coercion_cast(self.source_expr);
return Ok(());
}
self.do_check(table, apply_adjustments)
.map_err(|e| e.into_diagnostic(self.expr, self.expr_ty.clone(), self.cast_ty.clone()))
}

View file

@ -98,3 +98,21 @@ fn main() {
"#,
);
}
#[test]
fn cast_error_type() {
check_infer(
r#"
fn main() {
let foo: [_; _] = [false] as _;
}
"#,
expect![[r#"
10..47 '{ le...s _; }': ()
18..21 'foo': [bool; 1]
32..39 '[false]': [bool; 1]
32..44 '[false] as _': [bool; 1]
33..38 'false': bool
"#]],
);
}