about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorChayim Refael Friedman <chayimfr@gmail.com>2025-09-10 04:51:33 +0300
committerChayim Refael Friedman <chayimfr@gmail.com>2025-09-10 04:57:10 +0300
commitb2b33f9faab81507abda08d025c0680eb8d4995d (patch)
tree315336bef706eb9fed88b3800197f02730a9843a /src/tools
parent98d863c8b140323b39d36e24fe0bc3e48e3797ef (diff)
downloadrust-b2b33f9faab81507abda08d025c0680eb8d4995d.tar.gz
rust-b2b33f9faab81507abda08d025c0680eb8d4995d.zip
Always coerce in a cast, even when there are unknown types
This cause the relationships between inference vars to get recorded.
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/infer/cast.rs13
-rw-r--r--src/tools/rust-analyzer/crates/hir-ty/src/tests/regression/new_solver.rs18
2 files changed, 25 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-ty/src/infer/cast.rs b/src/tools/rust-analyzer/crates/hir-ty/src/infer/cast.rs
index 43364963eb0..bc3ee3c4c54 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/infer/cast.rs
+++ b/src/tools/rust-analyzer/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/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression/new_solver.rs b/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression/new_solver.rs
index 6d6c56696a3..4df788638a3 100644
--- a/src/tools/rust-analyzer/crates/hir-ty/src/tests/regression/new_solver.rs
+++ b/src/tools/rust-analyzer/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
+        "#]],
+    );
+}