Skip to content

Commit a7b44b9

Browse files
committed
make array coercion consistent with if and match
1 parent 6ab8f33 commit a7b44b9

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,15 +1825,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18251825
.filter(|t| !self.try_structurally_resolve_type(expr.span, *t).is_ty_var())
18261826
})
18271827
.unwrap_or_else(|| self.next_ty_var(expr.span));
1828+
let element_expected = Expectation::ExpectHasType(coerce_to)
1829+
.try_structurally_resolve_and_adjust_for_branches(self, expr.span);
18281830
let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
18291831
assert_eq!(self.diverges.get(), Diverges::Maybe);
18301832
for e in args {
1831-
// FIXME: the element expectation should use
1832-
// `try_structurally_resolve_and_adjust_for_branches` just like in `if` and `match`.
1833-
// While that fixes nested coercion, it will break [some
1834-
// code like this](https://github.com/rust-lang/rust/pull/140283#issuecomment-2958776528).
1835-
// If we find a way to support recursive tuple coercion, this break can be avoided.
1836-
let e_ty = self.check_expr_with_hint(e, coerce_to);
1833+
let e_ty = self.check_expr_with_expectation(e, element_expected);
18371834
let cause = self.misc(e.span);
18381835
coerce.coerce(self, &cause, e, e_ty);
18391836
}

tests/ui/coercion/coerce-many-with-ty-var.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fn main() {
1313
// Previously the element type's ty var will be unified with `foo`.
1414
let _: [_; 2] = [foo, bar];
1515
infer_array_element([foo, bar]);
16+
let _ = [foo, if false { bar } else { foo }];
1617

1718
let _ = if false {
1819
foo
@@ -24,6 +25,16 @@ fn main() {
2425
} else {
2526
bar
2627
});
28+
let _ = if false {
29+
foo
30+
} else {
31+
if false {
32+
bar
33+
} else {
34+
foo
35+
}
36+
};
37+
2738

2839
let _ = match false {
2940
true => foo,
@@ -33,6 +44,12 @@ fn main() {
3344
true => foo,
3445
false => bar,
3546
});
36-
37-
47+
let _ = match 1 {
48+
2 => foo,
49+
_ => if false {
50+
bar
51+
} else {
52+
foo
53+
},
54+
};
3855
}

tests/ui/coercion/coerce-unify.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,4 @@ pub fn main() {
6262
let a = [1, 2, 3];
6363
let v = vec![1, 2, 3];
6464
check2!(&a[..], &v);
65-
66-
// Make sure in-array coercion still works.
67-
let _ = [("a", Default::default()), (Default::default(), "b"), (&s, &s)];
6865
}

tests/ui/repeat-expr/typo-in-repeat-expr-issue-80173.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ error[E0308]: mismatched types
103103
--> $DIR/typo-in-repeat-expr-issue-80173.rs:57:23
104104
|
105105
LL | let k = vec!['c', 10];
106-
| ^^ expected `char`, found `u8`
106+
| ^^ expected `char`, found integer
107107
|
108108
help: replace the comma with a semicolon to create a vector
109109
|

0 commit comments

Comments
 (0)