Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d048a47

Browse files
authored
Merge pull request #20649 from ChayimFriedman2/cast-unknown
fix: Always coerce in a cast, even when there are unknown types
2 parents cd529a4 + 577203a commit d048a47

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

crates/hir-ty/src/infer/cast.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ impl CastCheck {
106106
self.expr_ty = table.eagerly_normalize_and_resolve_shallow_in(self.expr_ty.clone());
107107
self.cast_ty = table.eagerly_normalize_and_resolve_shallow_in(self.cast_ty.clone());
108108

109+
// This should always come first so that we apply the coercion, which impacts infer vars.
110+
if let Ok((adj, _)) = table.coerce(&self.expr_ty, &self.cast_ty, CoerceNever::Yes) {
111+
apply_adjustments(self.source_expr, adj);
112+
set_coercion_cast(self.source_expr);
113+
return Ok(());
114+
}
115+
109116
if self.expr_ty.contains_unknown() || self.cast_ty.contains_unknown() {
110117
return Ok(());
111118
}
@@ -126,12 +133,6 @@ impl CastCheck {
126133
return Ok(());
127134
}
128135

129-
if let Ok((adj, _)) = table.coerce(&self.expr_ty, &self.cast_ty, CoerceNever::Yes) {
130-
apply_adjustments(self.source_expr, adj);
131-
set_coercion_cast(self.source_expr);
132-
return Ok(());
133-
}
134-
135136
self.do_check(table, apply_adjustments)
136137
.map_err(|e| e.into_diagnostic(self.expr, self.expr_ty.clone(), self.cast_ty.clone()))
137138
}

crates/hir-ty/src/tests/regression/new_solver.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,21 @@ fn main() {
9898
"#,
9999
);
100100
}
101+
102+
#[test]
103+
fn cast_error_type() {
104+
check_infer(
105+
r#"
106+
fn main() {
107+
let foo: [_; _] = [false] as _;
108+
}
109+
"#,
110+
expect![[r#"
111+
10..47 '{ le...s _; }': ()
112+
18..21 'foo': [bool; 1]
113+
32..39 '[false]': [bool; 1]
114+
32..44 '[false] as _': [bool; 1]
115+
33..38 'false': bool
116+
"#]],
117+
);
118+
}

0 commit comments

Comments
 (0)