diff options
| author | Chayim Refael Friedman <chayimfr@gmail.com> | 2025-09-10 04:51:33 +0300 |
|---|---|---|
| committer | Chayim Refael Friedman <chayimfr@gmail.com> | 2025-09-10 04:57:10 +0300 |
| commit | b2b33f9faab81507abda08d025c0680eb8d4995d (patch) | |
| tree | 315336bef706eb9fed88b3800197f02730a9843a /src/tools | |
| parent | 98d863c8b140323b39d36e24fe0bc3e48e3797ef (diff) | |
| download | rust-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.rs | 13 | ||||
| -rw-r--r-- | src/tools/rust-analyzer/crates/hir-ty/src/tests/regression/new_solver.rs | 18 |
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 + "#]], + ); +} |
