Skip to content

Commit

Permalink
fix: zero-length span with trailing semi-colon in block
Browse files Browse the repository at this point in the history
  • Loading branch information
EliseZeroTwo committed Dec 24, 2023
1 parent ebb821f commit 4ba9f66
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
11 changes: 10 additions & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3163,8 +3163,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
};
if self.can_eq(self.param_env, expected_ty, ty) {
let span = stmt
.span
.from_expansion()
.then(|| {
let mac_call = rustc_span::source_map::original_sp(stmt.span, block.span);
self.tcx.sess.source_map().mac_call_stmt_semi_span(mac_call)
})
.flatten()
.unwrap_or_else(|| stmt.span.with_lo(tail_expr.span.hi()));
err.span_suggestion_short(
stmt.span.with_lo(tail_expr.span.hi()),
span,
"remove this semicolon",
"",
Applicability::MachineApplicable,
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/typeck/issue-114251.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// compile-flags: -C debug-assertions

macro_rules! feedu8 {
() => {
0u8
}
}

fn main() {
let kitty = {
feedu8!();
//~^ HELP: remove this semicolon
};

let cat: u8 = kitty;
//~^ ERROR: mismatched types
}
14 changes: 14 additions & 0 deletions tests/ui/typeck/issue-114251.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0308]: mismatched types
--> $DIR/issue-114251.rs:15:19
|
LL | feedu8!();
| - help: remove this semicolon
...
LL | let cat: u8 = kitty;
| -- ^^^^^ expected `u8`, found `()`
| |
| expected due to this

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.

0 comments on commit 4ba9f66

Please sign in to comment.