Skip to content

Commit 547369d

Browse files
committed
Auto merge of rust-lang#95220 - rust-lang:notriddle/ast-validation-semicolon, r=Dylan-DPC
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 rust-lang#83104
2 parents 9280445 + 3729b17 commit 547369d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,17 @@ impl<'a> AstValidator<'a> {
471471
}
472472

473473
fn error_item_without_body(&self, sp: Span, ctx: &str, msg: &str, sugg: &str) {
474+
let source_map = self.session.source_map();
475+
let end = source_map.end_point(sp);
476+
let replace_span = if source_map.span_to_snippet(end).map(|s| s == ";").unwrap_or(false) {
477+
end
478+
} else {
479+
sp.shrink_to_hi()
480+
};
474481
self.err_handler()
475482
.struct_span_err(sp, msg)
476483
.span_suggestion(
477-
self.session.source_map().end_point(sp),
484+
replace_span,
478485
&format!("provide a definition for the {}", ctx),
479486
sugg.to_string(),
480487
Applicability::HasPlaceholders,

src/test/ui/parser/issues/issue-87635.stderr

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ error: associated function in `impl` without body
1313
--> $DIR/issue-87635.rs:4:5
1414
|
1515
LL | pub fn bar()
16-
| ^^^^^^^^^^^-
17-
| |
18-
| help: provide a definition for the function: `{ <body> }`
16+
| ^^^^^^^^^^^^- help: provide a definition for the function: `{ <body> }`
1917

2018
error: aborting due to 2 previous errors
2119

0 commit comments

Comments
 (0)