fix: report type mismatch on identifier in destructuring assignments
This commit is contained in:
parent
afdbd6cce2
commit
649e1f54cf
|
@ -911,7 +911,15 @@ impl<'a> InferenceContext<'a> {
|
|||
let lhs_ty = self.insert_type_vars_shallow(lhs_ty);
|
||||
let ty = match self.coerce(None, &rhs_ty, &lhs_ty) {
|
||||
Ok(ty) => ty,
|
||||
Err(_) => self.err_ty(),
|
||||
Err(_) => {
|
||||
self.result.type_mismatches.insert(
|
||||
lhs.into(),
|
||||
TypeMismatch { expected: rhs_ty.clone(), actual: lhs_ty.clone() },
|
||||
);
|
||||
// `rhs_ty` is returned so no further type mismatches are
|
||||
// reported because of this mismatch.
|
||||
rhs_ty
|
||||
}
|
||||
};
|
||||
self.write_expr_ty(lhs, ty.clone());
|
||||
return ty;
|
||||
|
|
|
@ -3043,3 +3043,30 @@ fn main() {
|
|||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn destructuring_assignment_type_mismatch_on_identifier() {
|
||||
check(
|
||||
r#"
|
||||
struct S { v: i64 }
|
||||
struct TS(i64);
|
||||
fn main() {
|
||||
let mut a: usize = 0;
|
||||
(a,) = (0i64,);
|
||||
//^expected i64, got usize
|
||||
|
||||
let mut a: usize = 0;
|
||||
[a,] = [0i64,];
|
||||
//^expected i64, got usize
|
||||
|
||||
let mut a: usize = 0;
|
||||
S { v: a } = S { v: 0 };
|
||||
//^expected i64, got usize
|
||||
|
||||
let mut a: usize = 0;
|
||||
TS(a) = TS(0);
|
||||
//^expected i64, got usize
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue