Unnamed repository; edit this file 'description' to name the repository.
Always coerce in a cast, even when there are unknown types
This cause the relationships between inference vars to get recorded.
Chayim Refael Friedman 7 months ago
parent 8b2671e · commit 577203a
-rw-r--r--crates/hir-ty/src/infer/cast.rs13
-rw-r--r--crates/hir-ty/src/tests/regression/new_solver.rs18
2 files changed, 25 insertions, 6 deletions
diff --git a/crates/hir-ty/src/infer/cast.rs b/crates/hir-ty/src/infer/cast.rs
index 43364963eb..bc3ee3c4c5 100644
--- a/crates/hir-ty/src/infer/cast.rs
+++ b/crates/hir-ty/src/infer/cast.rs
@@ -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()))
}
diff --git a/crates/hir-ty/src/tests/regression/new_solver.rs b/crates/hir-ty/src/tests/regression/new_solver.rs
index 6d6c56696a..4df788638a 100644
--- a/crates/hir-ty/src/tests/regression/new_solver.rs
+++ b/crates/hir-ty/src/tests/regression/new_solver.rs
@@ -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
+ "#]],
+ );
+}