Skip to content

Commit dff53a0

Browse files
authored
Rollup merge of rust-lang#60455 - estebank:resolve-match-arm-ty, r=davidtwco
Resolve match arm ty when arms diverge Fix rust-lang#58695.
2 parents 57cfb40 + 24fddb1 commit dff53a0

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

src/librustc/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
644644
for sp in prior_arms {
645645
err.span_label(*sp, format!(
646646
"this is found to be of type `{}`",
647-
last_ty,
647+
self.resolve_type_vars_if_possible(&last_ty),
648648
));
649649
}
650650
} else if let Some(sp) = prior_arms.last() {

src/test/ui/match/match-type-err-first-arm.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ fn main() {
33
let _ = test_func2(1);
44
}
55

6-
fn test_func1(n: i32) -> i32 {
7-
//~^ NOTE expected `i32` because of return type
6+
fn test_func1(n: i32) -> i32 { //~ NOTE expected `i32` because of return type
87
match n {
98
12 => 'b',
109
//~^ ERROR mismatched types
@@ -14,10 +13,8 @@ fn test_func1(n: i32) -> i32 {
1413
}
1514

1615
fn test_func2(n: i32) -> i32 {
17-
let x = match n {
18-
//~^ NOTE `match` arms have incompatible types
19-
12 => 'b',
20-
//~^ NOTE this is found to be of type `char`
16+
let x = match n { //~ NOTE `match` arms have incompatible types
17+
12 => 'b', //~ NOTE this is found to be of type `char`
2118
_ => 42,
2219
//~^ ERROR match arms have incompatible types
2320
//~| NOTE expected char, found integer
@@ -27,8 +24,7 @@ fn test_func2(n: i32) -> i32 {
2724
}
2825

2926
fn test_func3(n: i32) -> i32 {
30-
let x = match n {
31-
//~^ NOTE `match` arms have incompatible types
27+
let x = match n { //~ NOTE `match` arms have incompatible types
3228
1 => 'b',
3329
2 => 'b',
3430
3 => 'b',
@@ -43,3 +39,15 @@ fn test_func3(n: i32) -> i32 {
4339
};
4440
x
4541
}
42+
43+
fn test_func4() {
44+
match Some(0u32) { //~ NOTE `match` arms have incompatible types
45+
Some(x) => {
46+
x //~ NOTE this is found to be of type `u32`
47+
},
48+
None => {}
49+
//~^ ERROR match arms have incompatible types
50+
//~| NOTE expected u32, found ()
51+
//~| NOTE expected type `u32`
52+
};
53+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
error[E0308]: mismatched types
2-
--> $DIR/match-type-err-first-arm.rs:9:15
2+
--> $DIR/match-type-err-first-arm.rs:8:15
33
|
44
LL | fn test_func1(n: i32) -> i32 {
55
| --- expected `i32` because of return type
6-
...
6+
LL | match n {
77
LL | 12 => 'b',
88
| ^^^ expected i32, found char
99

1010
error[E0308]: match arms have incompatible types
11-
--> $DIR/match-type-err-first-arm.rs:21:14
11+
--> $DIR/match-type-err-first-arm.rs:18:14
1212
|
1313
LL | let x = match n {
1414
| _____________-
15-
LL | |
1615
LL | | 12 => 'b',
1716
| | --- this is found to be of type `char`
18-
LL | |
1917
LL | | _ => 42,
2018
| | ^^ expected char, found integer
21-
... |
19+
LL | |
20+
LL | |
2221
LL | |
2322
LL | | };
2423
| |_____- `match` arms have incompatible types
@@ -27,13 +26,13 @@ LL | | };
2726
found type `{integer}`
2827

2928
error[E0308]: match arms have incompatible types
30-
--> $DIR/match-type-err-first-arm.rs:39:14
29+
--> $DIR/match-type-err-first-arm.rs:35:14
3130
|
3231
LL | let x = match n {
3332
| _____________-
34-
LL | |
3533
LL | | 1 => 'b',
3634
LL | | 2 => 'b',
35+
LL | | 3 => 'b',
3736
... |
3837
LL | | 6 => 'b',
3938
| | --- this and all prior arms are found to be of type `char`
@@ -48,6 +47,24 @@ LL | | };
4847
= note: expected type `char`
4948
found type `{integer}`
5049

51-
error: aborting due to 3 previous errors
50+
error[E0308]: match arms have incompatible types
51+
--> $DIR/match-type-err-first-arm.rs:48:17
52+
|
53+
LL | / match Some(0u32) {
54+
LL | | Some(x) => {
55+
LL | | x
56+
| | - this is found to be of type `u32`
57+
LL | | },
58+
LL | | None => {}
59+
| | ^^ expected u32, found ()
60+
... |
61+
LL | |
62+
LL | | };
63+
| |_____- `match` arms have incompatible types
64+
|
65+
= note: expected type `u32`
66+
found type `()`
67+
68+
error: aborting due to 4 previous errors
5269

5370
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)