Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions crates/hir-ty/src/infer/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(());
}
Expand All @@ -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()))
}
Expand Down
18 changes: 18 additions & 0 deletions crates/hir-ty/src/tests/regression/new_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"#]],
);
}
Loading