9526: fix: coerce array elements to a common type r=flodiebold a=jonas-schievink



Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2021-07-07 22:13:35 +00:00 committed by GitHub
commit 85f0f9eb1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -729,7 +729,7 @@ impl<'a> InferenceContext<'a> {
TyKind::Tuple(tys.len(), Substitution::from_iter(&Interner, tys)).intern(&Interner) TyKind::Tuple(tys.len(), Substitution::from_iter(&Interner, tys)).intern(&Interner)
} }
Expr::Array(array) => { Expr::Array(array) => {
let elem_ty = let mut elem_ty =
match expected.to_option(&mut self.table).as_ref().map(|t| t.kind(&Interner)) { match expected.to_option(&mut self.table).as_ref().map(|t| t.kind(&Interner)) {
Some(TyKind::Array(st, _) | TyKind::Slice(st)) => st.clone(), Some(TyKind::Array(st, _) | TyKind::Slice(st)) => st.clone(),
_ => self.table.new_type_var(), _ => self.table.new_type_var(),
@ -738,8 +738,8 @@ impl<'a> InferenceContext<'a> {
let len = match array { let len = match array {
Array::ElementList(items) => { Array::ElementList(items) => {
for expr in items.iter() { for expr in items.iter() {
// FIXME: use CoerceMany (coerce_merge_branch) let cur_elem_ty = self.infer_expr_inner(*expr, expected);
self.infer_expr_coerce(*expr, &Expectation::has_type(elem_ty.clone())); elem_ty = self.coerce_merge_branch(Some(*expr), &elem_ty, &cur_elem_ty);
} }
Some(items.len() as u64) Some(items.len() as u64)
} }

View File

@ -507,3 +507,17 @@ fn main() {
"#, "#,
); );
} }
#[test]
fn coerce_array_elems_lub() {
check_no_mismatches(
r#"
fn f() {}
fn g() {}
fn test() {
[f, g];
}
"#,
);
}

View File

@ -117,19 +117,19 @@ fn recursive_vars_2() {
"#, "#,
expect![[r#" expect![[r#"
10..79 '{ ...x)]; }': () 10..79 '{ ...x)]; }': ()
20..21 'x': &{unknown} 20..21 'x': {unknown}
24..31 'unknown': &{unknown} 24..31 'unknown': {unknown}
41..42 'y': {unknown} 41..42 'y': {unknown}
45..52 'unknown': {unknown} 45..52 'unknown': {unknown}
58..76 '[(x, y..., &x)]': [(&{unknown}, {unknown}); 2] 58..76 '[(x, y..., &x)]': [({unknown}, {unknown}); 2]
59..65 '(x, y)': (&{unknown}, {unknown}) 59..65 '(x, y)': ({unknown}, {unknown})
60..61 'x': &{unknown} 60..61 'x': {unknown}
63..64 'y': {unknown} 63..64 'y': {unknown}
67..75 '(&y, &x)': (&{unknown}, {unknown}) 67..75 '(&y, &x)': (&{unknown}, &{unknown})
68..70 '&y': &{unknown} 68..70 '&y': &{unknown}
69..70 'y': {unknown} 69..70 'y': {unknown}
72..74 '&x': &&{unknown} 72..74 '&x': &{unknown}
73..74 'x': &{unknown} 73..74 'x': {unknown}
"#]], "#]],
); );
} }