Skip to content

Commit 93caf60

Browse files
Always coerce in a cast, even when there are unknown types
This cause the relationships between inference vars to get recorded.
1 parent 412932e commit 93caf60

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
@@ -71,3 +71,21 @@ fn main() {
7171
}"#,
7272
);
7373
}
74+
75+
#[test]
76+
fn cast_error_type() {
77+
check_infer(
78+
r#"
79+
fn main() {
80+
let foo: [_; _] = [false] as _;
81+
}
82+
"#,
83+
expect![[r#"
84+
10..47 '{ le...s _; }': ()
85+
18..21 'foo': [bool; 1]
86+
32..39 '[false]': [bool; 1]
87+
32..44 '[false] as _': [bool; 1]
88+
33..38 'false': bool
89+
"#]],
90+
);
91+
}

0 commit comments

Comments
 (0)