From 334d4657322a95b4160e03fecc815fc38ac56e82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 1 Sep 2019 20:13:59 -0700 Subject: [PATCH 1/3] Point at appropriate arm on type error on if/else/match with one non-! arm --- src/librustc_typeck/check/mod.rs | 43 ++++++++++++++++--- ...-to-type-err-cause-on-impl-trait-return.rs | 2 +- ...type-err-cause-on-impl-trait-return.stderr | 20 ++++----- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index a80550486d627..4a0b3879cb991 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3687,6 +3687,40 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } + /// If `expr` is a `match` expression that has only one non-`!` arm, use that arm's tail + /// expression's `Span`, otherwise return `expr.span`. This is done to give bettern errors + /// when given code like the following: + /// ```text + /// if false { return 0i32; } else { 1u32 } + /// // ^^^^ point at this instead of the whole `if` expression + /// ``` + fn get_expr_coercion_span(&self, expr: &hir::Expr) -> syntax_pos::Span { + if let hir::ExprKind::Match(_, arms, _) = &expr.node { + let arm_spans: Vec = arms.iter().filter_map(|arm| { + self.in_progress_tables + .and_then(|tables| tables.borrow().node_type_opt(arm.body.hir_id)) + .and_then(|arm_ty| { + if arm_ty.is_never() { + None + } else { + Some(match &arm.body.node { + // Point at the tail expression when possible. + hir::ExprKind::Block(block, _) => block.expr + .as_ref() + .map(|e| e.span) + .unwrap_or(block.span), + _ => arm.body.span, + }) + } + }) + }).collect(); + if arm_spans.len() == 1 { + return arm_spans[0]; + } + } + expr.span + } + fn check_block_with_expected( &self, blk: &'tcx hir::Block, @@ -3746,12 +3780,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let coerce = ctxt.coerce.as_mut().unwrap(); if let Some(tail_expr_ty) = tail_expr_ty { let tail_expr = tail_expr.unwrap(); - let cause = self.cause(tail_expr.span, - ObligationCauseCode::BlockTailExpression(blk.hir_id)); - coerce.coerce(self, - &cause, - tail_expr, - tail_expr_ty); + let span = self.get_expr_coercion_span(tail_expr); + let cause = self.cause(span, ObligationCauseCode::BlockTailExpression(blk.hir_id)); + coerce.coerce(self, &cause, tail_expr, tail_expr_ty); } else { // Subtle: if there is no explicit tail expression, // that is typically equivalent to a tail expression diff --git a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs index 95b40368143ef..d416db628c03f 100644 --- a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs +++ b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs @@ -17,10 +17,10 @@ fn bar() -> impl std::fmt::Display { fn baz() -> impl std::fmt::Display { if false { - //~^ ERROR mismatched types return 0i32; } else { 1u32 + //~^ ERROR mismatched types } } diff --git a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr index ee1e36081e778..47644d66d1a2c 100644 --- a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr +++ b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr @@ -29,18 +29,16 @@ LL | return 1u32; found type `u32` error[E0308]: mismatched types - --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:19:5 + --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:22:9 | -LL | fn baz() -> impl std::fmt::Display { - | ---------------------- expected because this return type... -LL | / if false { -LL | | -LL | | return 0i32; - | | ---- ...is found to be `i32` here -LL | | } else { -LL | | 1u32 -LL | | } - | |_____^ expected i32, found u32 +LL | fn baz() -> impl std::fmt::Display { + | ---------------------- expected because this return type... +LL | if false { +LL | return 0i32; + | ---- ...is found to be `i32` here +LL | } else { +LL | 1u32 + | ^^^^ expected i32, found u32 | = note: expected type `i32` found type `u32` From dd870d742244fc8c9399bfac6f91e15a51d02d08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 2 Sep 2019 10:19:09 -0700 Subject: [PATCH 2/3] fix typo --- src/librustc_typeck/check/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 4a0b3879cb991..2cfe080771456 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3688,7 +3688,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } /// If `expr` is a `match` expression that has only one non-`!` arm, use that arm's tail - /// expression's `Span`, otherwise return `expr.span`. This is done to give bettern errors + /// expression's `Span`, otherwise return `expr.span`. This is done to give better errors /// when given code like the following: /// ```text /// if false { return 0i32; } else { 1u32 } From c430d743e90967e621e27cdbb8bd64de67969ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 2 Sep 2019 17:37:50 -0700 Subject: [PATCH 3/3] Add match test cases --- ...-to-type-err-cause-on-impl-trait-return.rs | 29 +++++++++++ ...type-err-cause-on-impl-trait-return.stderr | 48 ++++++++++++++++++- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs index d416db628c03f..58109be447e07 100644 --- a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs +++ b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.rs @@ -33,4 +33,33 @@ fn qux() -> impl std::fmt::Display { } } +fn bat() -> impl std::fmt::Display { + match 13 { + 0 => return 0i32, + _ => 1u32, + //~^ ERROR mismatched types + } +} + +fn can() -> impl std::fmt::Display { + match 13 { + //~^ ERROR mismatched types + 0 => return 0i32, + 1 => 1u32, + _ => 2u32, + } +} + +fn cat() -> impl std::fmt::Display { + match 13 { + 0 => { + return 0i32; + } + _ => { + 1u32 + //~^ ERROR mismatched types + } + } +} + fn main() {} diff --git a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr index 47644d66d1a2c..314ff84ae3c46 100644 --- a/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr +++ b/src/test/ui/point-to-type-err-cause-on-impl-trait-return.stderr @@ -59,6 +59,52 @@ LL | | } = note: expected type `i32` found type `u32` -error: aborting due to 4 previous errors +error[E0308]: mismatched types + --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:39:14 + | +LL | fn bat() -> impl std::fmt::Display { + | ---------------------- expected because this return type... +LL | match 13 { +LL | 0 => return 0i32, + | ---- ...is found to be `i32` here +LL | _ => 1u32, + | ^^^^ expected i32, found u32 + | + = note: expected type `i32` + found type `u32` + +error[E0308]: mismatched types + --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:45:5 + | +LL | fn can() -> impl std::fmt::Display { + | ---------------------- expected because this return type... +LL | / match 13 { +LL | | +LL | | 0 => return 0i32, + | | ---- ...is found to be `i32` here +LL | | 1 => 1u32, +LL | | _ => 2u32, +LL | | } + | |_____^ expected i32, found u32 + | + = note: expected type `i32` + found type `u32` + +error[E0308]: mismatched types + --> $DIR/point-to-type-err-cause-on-impl-trait-return.rs:59:13 + | +LL | fn cat() -> impl std::fmt::Display { + | ---------------------- expected because this return type... +... +LL | return 0i32; + | ---- ...is found to be `i32` here +... +LL | 1u32 + | ^^^^ expected i32, found u32 + | + = note: expected type `i32` + found type `u32` + +error: aborting due to 7 previous errors For more information about this error, try `rustc --explain E0308`.