about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2021-04-06 19:23:22 -0700
committerEsteban Küber <esteban@kuber.com.ar>2021-04-06 19:23:22 -0700
commit650877de45d5b10a6ecd19e27d9ba282e3b6ccd7 (patch)
treebfc3ec078dbada46b941f80f99c66e258c0482cc
parent16143d10679537d3fde4247e15334e78ad9d55b9 (diff)
downloadrust-650877de45d5b10a6ecd19e27d9ba282e3b6ccd7.tar.gz
rust-650877de45d5b10a6ecd19e27d9ba282e3b6ccd7.zip
Use a more appropriate span for `;` suggestion
Fix #83892.
-rw-r--r--compiler/rustc_typeck/src/check/coercion.rs5
-rw-r--r--compiler/rustc_typeck/src/check/expr.rs2
-rw-r--r--compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs8
-rw-r--r--src/test/ui/suggestions/issue-83892.fixed11
-rw-r--r--src/test/ui/suggestions/issue-83892.rs11
-rw-r--r--src/test/ui/suggestions/issue-83892.stderr14
6 files changed, 42 insertions, 9 deletions
diff --git a/compiler/rustc_typeck/src/check/coercion.rs b/compiler/rustc_typeck/src/check/coercion.rs
index 68a923a55eb..37538267b86 100644
--- a/compiler/rustc_typeck/src/check/coercion.rs
+++ b/compiler/rustc_typeck/src/check/coercion.rs
@@ -1440,9 +1440,8 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
         // as prior return coercions would not be relevant (#57664).
         let parent_id = fcx.tcx.hir().get_parent_node(id);
         let fn_decl = if let Some((expr, blk_id)) = expression {
-            pointing_at_return_type = fcx.suggest_mismatched_types_on_tail(
-                &mut err, expr, expected, found, cause.span, blk_id,
-            );
+            pointing_at_return_type =
+                fcx.suggest_mismatched_types_on_tail(&mut err, expr, expected, found, blk_id);
             let parent = fcx.tcx.hir().get(parent_id);
             if let (Some(cond_expr), true, false) = (
                 fcx.tcx.hir().get_if_cause(expr.hir_id),
diff --git a/compiler/rustc_typeck/src/check/expr.rs b/compiler/rustc_typeck/src/check/expr.rs
index 67714b714c9..acd8b9c485d 100644
--- a/compiler/rustc_typeck/src/check/expr.rs
+++ b/compiler/rustc_typeck/src/check/expr.rs
@@ -603,7 +603,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                         &cause,
                         &mut |mut err| {
                             self.suggest_mismatched_types_on_tail(
-                                &mut err, expr, ty, e_ty, cause.span, target_id,
+                                &mut err, expr, ty, e_ty, target_id,
                             );
                             if let Some(val) = ty_kind_suggestion(ty) {
                                 let label = destination
diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
index 6112a5fdc91..bb13cee3b20 100644
--- a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
+++ b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
@@ -41,15 +41,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         expr: &'tcx hir::Expr<'tcx>,
         expected: Ty<'tcx>,
         found: Ty<'tcx>,
-        cause_span: Span,
         blk_id: hir::HirId,
     ) -> bool {
         let expr = expr.peel_drop_temps();
         // If the expression is from an external macro, then do not suggest
         // adding a semicolon, because there's nowhere to put it.
         // See issue #81943.
-        if expr.can_have_side_effects() && !in_external_macro(self.tcx.sess, cause_span) {
-            self.suggest_missing_semicolon(err, expr, expected, cause_span);
+        if expr.can_have_side_effects() && !in_external_macro(self.tcx.sess, expr.span) {
+            self.suggest_missing_semicolon(err, expr, expected);
         }
         let mut pointing_at_return_type = false;
         if let Some((fn_decl, can_suggest)) = self.get_fn_decl(blk_id) {
@@ -388,7 +387,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         err: &mut DiagnosticBuilder<'_>,
         expression: &'tcx hir::Expr<'tcx>,
         expected: Ty<'tcx>,
-        cause_span: Span,
     ) {
         if expected.is_unit() {
             // `BlockTailExpression` only relevant if the tail expr would be
@@ -403,7 +401,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     if expression.can_have_side_effects() =>
                 {
                     err.span_suggestion(
-                        cause_span.shrink_to_hi(),
+                        expression.span.shrink_to_hi(),
                         "consider using a semicolon here",
                         ";".to_string(),
                         Applicability::MachineApplicable,
diff --git a/src/test/ui/suggestions/issue-83892.fixed b/src/test/ui/suggestions/issue-83892.fixed
new file mode 100644
index 00000000000..dd093a7a0e3
--- /dev/null
+++ b/src/test/ui/suggestions/issue-83892.fixed
@@ -0,0 +1,11 @@
+// run-rustfix
+
+fn func() -> u8 {
+    0
+}
+
+fn main() {
+    match () {
+        () => func() //~ ERROR mismatched types
+    };
+}
diff --git a/src/test/ui/suggestions/issue-83892.rs b/src/test/ui/suggestions/issue-83892.rs
new file mode 100644
index 00000000000..1d56ecee868
--- /dev/null
+++ b/src/test/ui/suggestions/issue-83892.rs
@@ -0,0 +1,11 @@
+// run-rustfix
+
+fn func() -> u8 {
+    0
+}
+
+fn main() {
+    match () {
+        () => func() //~ ERROR mismatched types
+    }
+}
diff --git a/src/test/ui/suggestions/issue-83892.stderr b/src/test/ui/suggestions/issue-83892.stderr
new file mode 100644
index 00000000000..baf6b1447e6
--- /dev/null
+++ b/src/test/ui/suggestions/issue-83892.stderr
@@ -0,0 +1,14 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-83892.rs:9:15
+   |
+LL | fn main() {
+   |           - expected `()` because of default return type
+LL |     match () {
+LL |         () => func()
+   |               ^^^^^^ expected `()`, found `u8`
+LL |     }
+   |      - help: consider using a semicolon here: `;`
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.