From 3729b17b7e960a95d0887dc0fff28b25a1393385 Mon Sep 17 00:00:00 2001
From: Michael Howell <michael@notriddle.com>
Date: Tue, 22 Mar 2022 15:29:07 -0700
Subject: [PATCH] diagnostics: do not suggest `fn foo({ <body> }`

Instead of suggesting that the body always replace the last character on the
line, presuming it must be a semicolon, the parser should instead check what
the last character is, and append the body if it is anything else.

Fixes #83104
---
 compiler/rustc_ast_passes/src/ast_validation.rs | 9 ++++++++-
 src/test/ui/parser/issues/issue-87635.stderr    | 4 +---
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs
index f5e6b15fcbfd1..8ba6a914c4a4b 100644
--- a/compiler/rustc_ast_passes/src/ast_validation.rs
+++ b/compiler/rustc_ast_passes/src/ast_validation.rs
@@ -471,10 +471,17 @@ impl<'a> AstValidator<'a> {
     }
 
     fn error_item_without_body(&self, sp: Span, ctx: &str, msg: &str, sugg: &str) {
+        let source_map = self.session.source_map();
+        let end = source_map.end_point(sp);
+        let replace_span = if source_map.span_to_snippet(end).map(|s| s == ";").unwrap_or(false) {
+            end
+        } else {
+            sp.shrink_to_hi()
+        };
         self.err_handler()
             .struct_span_err(sp, msg)
             .span_suggestion(
-                self.session.source_map().end_point(sp),
+                replace_span,
                 &format!("provide a definition for the {}", ctx),
                 sugg.to_string(),
                 Applicability::HasPlaceholders,
diff --git a/src/test/ui/parser/issues/issue-87635.stderr b/src/test/ui/parser/issues/issue-87635.stderr
index 0a52d0687b22b..1d459f1b907ed 100644
--- a/src/test/ui/parser/issues/issue-87635.stderr
+++ b/src/test/ui/parser/issues/issue-87635.stderr
@@ -13,9 +13,7 @@ error: associated function in `impl` without body
   --> $DIR/issue-87635.rs:4:5
    |
 LL |     pub fn bar()
-   |     ^^^^^^^^^^^-
-   |                |
-   |                help: provide a definition for the function: `{ <body> }`
+   |     ^^^^^^^^^^^^- help: provide a definition for the function: `{ <body> }`
 
 error: aborting due to 2 previous errors