From e1efa17faa234e3f69596001c1256c3fc10bedc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 6 Apr 2021 17:23:48 -0700 Subject: [PATCH] Account for `ExprKind::Block` when suggesting .into() and deref Fix #83943. --- .../src/check/fn_ctxt/suggestions.rs | 1 + src/test/ui/suggestions/issue-82361.stderr | 8 ++++---- src/test/ui/suggestions/issue-83943.fixed | 9 +++++++++ src/test/ui/suggestions/issue-83943.rs | 9 +++++++++ src/test/ui/suggestions/issue-83943.stderr | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/test/ui/suggestions/issue-83943.fixed create mode 100644 src/test/ui/suggestions/issue-83943.rs create mode 100644 src/test/ui/suggestions/issue-83943.stderr diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs index 6112a5fdc91d6..c87a808243d0c 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs @@ -205,6 +205,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { found: Ty<'tcx>, expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>, ) { + let expr = expr.peel_blocks(); if let Some((sp, msg, suggestion, applicability)) = self.check_ref(expr, found, expected) { err.span_suggestion(sp, msg, suggestion, applicability); } else if let (ty::FnDef(def_id, ..), true) = diff --git a/src/test/ui/suggestions/issue-82361.stderr b/src/test/ui/suggestions/issue-82361.stderr index c19d59ccd4c66..4c78293ebb7f9 100644 --- a/src/test/ui/suggestions/issue-82361.stderr +++ b/src/test/ui/suggestions/issue-82361.stderr @@ -21,10 +21,10 @@ LL | | 1 | | - expected because of this LL | | } else { LL | | &1 - | | -^ + | | ^^ | | | | | expected integer, found `&{integer}` - | | help: consider removing the `&` + | | help: consider removing the borrow: `1` LL | | }; | |_____- `if` and `else` have incompatible types @@ -36,10 +36,10 @@ LL | | 1 | | - expected because of this LL | | } else { LL | | &mut 1 - | | -----^ + | | ^^^^^^ | | | | | expected integer, found `&mut {integer}` - | | help: consider removing the `&mut` + | | help: consider removing the borrow: `1` LL | | }; | |_____- `if` and `else` have incompatible types diff --git a/src/test/ui/suggestions/issue-83943.fixed b/src/test/ui/suggestions/issue-83943.fixed new file mode 100644 index 0000000000000..e0d4ee29ebf88 --- /dev/null +++ b/src/test/ui/suggestions/issue-83943.fixed @@ -0,0 +1,9 @@ +// run-rustfix + +fn main() { + if true { + "A".to_string() + } else { + "B".to_string() //~ ERROR `if` and `else` have incompatible types + }; +} diff --git a/src/test/ui/suggestions/issue-83943.rs b/src/test/ui/suggestions/issue-83943.rs new file mode 100644 index 0000000000000..68d50c1775c7f --- /dev/null +++ b/src/test/ui/suggestions/issue-83943.rs @@ -0,0 +1,9 @@ +// run-rustfix + +fn main() { + if true { + "A".to_string() + } else { + "B" //~ ERROR `if` and `else` have incompatible types + }; +} diff --git a/src/test/ui/suggestions/issue-83943.stderr b/src/test/ui/suggestions/issue-83943.stderr new file mode 100644 index 0000000000000..a26700ea3c7c5 --- /dev/null +++ b/src/test/ui/suggestions/issue-83943.stderr @@ -0,0 +1,18 @@ +error[E0308]: `if` and `else` have incompatible types + --> $DIR/issue-83943.rs:7:9 + | +LL | / if true { +LL | | "A".to_string() + | | --------------- expected because of this +LL | | } else { +LL | | "B" + | | ^^^ + | | | + | | expected struct `String`, found `&str` + | | help: try using a conversion method: `"B".to_string()` +LL | | }; + | |_____- `if` and `else` have incompatible types + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0308`.