Skip to content

Commit 649e1f5

Browse files
committed
fix: report type mismatch on identifier in destructuring assignments
1 parent afdbd6c commit 649e1f5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,15 @@ impl<'a> InferenceContext<'a> {
911911
let lhs_ty = self.insert_type_vars_shallow(lhs_ty);
912912
let ty = match self.coerce(None, &rhs_ty, &lhs_ty) {
913913
Ok(ty) => ty,
914-
Err(_) => self.err_ty(),
914+
Err(_) => {
915+
self.result.type_mismatches.insert(
916+
lhs.into(),
917+
TypeMismatch { expected: rhs_ty.clone(), actual: lhs_ty.clone() },
918+
);
919+
// `rhs_ty` is returned so no further type mismatches are
920+
// reported because of this mismatch.
921+
rhs_ty
922+
}
915923
};
916924
self.write_expr_ty(lhs, ty.clone());
917925
return ty;

crates/hir-ty/src/tests/simple.rs

+27
Original file line numberDiff line numberDiff line change
@@ -3043,3 +3043,30 @@ fn main() {
30433043
"#,
30443044
);
30453045
}
3046+
3047+
#[test]
3048+
fn destructuring_assignment_type_mismatch_on_identifier() {
3049+
check(
3050+
r#"
3051+
struct S { v: i64 }
3052+
struct TS(i64);
3053+
fn main() {
3054+
let mut a: usize = 0;
3055+
(a,) = (0i64,);
3056+
//^expected i64, got usize
3057+
3058+
let mut a: usize = 0;
3059+
[a,] = [0i64,];
3060+
//^expected i64, got usize
3061+
3062+
let mut a: usize = 0;
3063+
S { v: a } = S { v: 0 };
3064+
//^expected i64, got usize
3065+
3066+
let mut a: usize = 0;
3067+
TS(a) = TS(0);
3068+
//^expected i64, got usize
3069+
}
3070+
"#,
3071+
);
3072+
}

0 commit comments

Comments
 (0)