Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve match arm ty when arms diverge #60455

Merged
merged 1 commit into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
for sp in prior_arms {
err.span_label(*sp, format!(
"this is found to be of type `{}`",
last_ty,
self.resolve_type_vars_if_possible(&last_ty),
));
}
} else if let Some(sp) = prior_arms.last() {
Expand Down
24 changes: 16 additions & 8 deletions src/test/ui/match/match-type-err-first-arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ fn main() {
let _ = test_func2(1);
}

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

fn test_func2(n: i32) -> i32 {
let x = match n {
//~^ NOTE `match` arms have incompatible types
12 => 'b',
//~^ NOTE this is found to be of type `char`
let x = match n { //~ NOTE `match` arms have incompatible types
12 => 'b', //~ NOTE this is found to be of type `char`
_ => 42,
//~^ ERROR match arms have incompatible types
//~| NOTE expected char, found integer
Expand All @@ -27,8 +24,7 @@ fn test_func2(n: i32) -> i32 {
}

fn test_func3(n: i32) -> i32 {
let x = match n {
//~^ NOTE `match` arms have incompatible types
let x = match n { //~ NOTE `match` arms have incompatible types
1 => 'b',
2 => 'b',
3 => 'b',
Expand All @@ -43,3 +39,15 @@ fn test_func3(n: i32) -> i32 {
};
x
}

fn test_func4() {
match Some(0u32) { //~ NOTE `match` arms have incompatible types
Some(x) => {
x //~ NOTE this is found to be of type `u32`
},
None => {}
//~^ ERROR match arms have incompatible types
//~| NOTE expected u32, found ()
//~| NOTE expected type `u32`
};
}
35 changes: 26 additions & 9 deletions src/test/ui/match/match-type-err-first-arm.stderr
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
error[E0308]: mismatched types
--> $DIR/match-type-err-first-arm.rs:9:15
--> $DIR/match-type-err-first-arm.rs:8:15
|
LL | fn test_func1(n: i32) -> i32 {
| --- expected `i32` because of return type
...
LL | match n {
LL | 12 => 'b',
| ^^^ expected i32, found char

error[E0308]: match arms have incompatible types
--> $DIR/match-type-err-first-arm.rs:21:14
--> $DIR/match-type-err-first-arm.rs:18:14
|
LL | let x = match n {
| _____________-
LL | |
LL | | 12 => 'b',
| | --- this is found to be of type `char`
LL | |
LL | | _ => 42,
| | ^^ expected char, found integer
... |
LL | |
LL | |
LL | |
LL | | };
| |_____- `match` arms have incompatible types
Expand All @@ -27,13 +26,13 @@ LL | | };
found type `{integer}`

error[E0308]: match arms have incompatible types
--> $DIR/match-type-err-first-arm.rs:39:14
--> $DIR/match-type-err-first-arm.rs:35:14
|
LL | let x = match n {
| _____________-
LL | |
LL | | 1 => 'b',
LL | | 2 => 'b',
LL | | 3 => 'b',
... |
LL | | 6 => 'b',
| | --- this and all prior arms are found to be of type `char`
Expand All @@ -48,6 +47,24 @@ LL | | };
= note: expected type `char`
found type `{integer}`

error: aborting due to 3 previous errors
error[E0308]: match arms have incompatible types
--> $DIR/match-type-err-first-arm.rs:48:17
|
LL | / match Some(0u32) {
LL | | Some(x) => {
LL | | x
| | - this is found to be of type `u32`
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
LL | | },
LL | | None => {}
| | ^^ expected u32, found ()
... |
LL | |
LL | | };
| |_____- `match` arms have incompatible types
|
= note: expected type `u32`
found type `()`

error: aborting due to 4 previous errors

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