From 24fddb15e86a46e6057c8c0f2f0fb18844f50049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 1 May 2019 14:57:24 -0700 Subject: [PATCH] Resolve match arm ty when arms diverge --- src/librustc/infer/error_reporting/mod.rs | 2 +- src/test/ui/match/match-type-err-first-arm.rs | 24 ++++++++----- .../ui/match/match-type-err-first-arm.stderr | 35 ++++++++++++++----- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index c20a08fc5aea6..95b566d4a1b69 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -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() { diff --git a/src/test/ui/match/match-type-err-first-arm.rs b/src/test/ui/match/match-type-err-first-arm.rs index b4b84ef8f1cec..8dfbf1019e9a4 100644 --- a/src/test/ui/match/match-type-err-first-arm.rs +++ b/src/test/ui/match/match-type-err-first-arm.rs @@ -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 @@ -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 @@ -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', @@ -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` + }; +} diff --git a/src/test/ui/match/match-type-err-first-arm.stderr b/src/test/ui/match/match-type-err-first-arm.stderr index a318e6cffb937..e0553fca683a5 100644 --- a/src/test/ui/match/match-type-err-first-arm.stderr +++ b/src/test/ui/match/match-type-err-first-arm.stderr @@ -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 @@ -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` @@ -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` +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`.