Skip to content

Commit a439eb2

Browse files
Don't use bytepos offsets when computing semicolon span for removal
1 parent 033becf commit a439eb2

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::traits::{
1515
};
1616
use rustc_middle::ty::print::with_no_trimmed_paths;
1717
use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt};
18-
use rustc_span::{sym, BytePos, Span};
18+
use rustc_span::{sym, Span};
1919

2020
use crate::errors::{
2121
ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
@@ -763,8 +763,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
763763
let mac_call = rustc_span::source_map::original_sp(last_stmt.span, blk.span);
764764
self.tcx.sess.source_map().mac_call_stmt_semi_span(mac_call)?
765765
} else {
766-
last_stmt.span.with_lo(last_stmt.span.hi() - BytePos(1))
766+
self.tcx
767+
.sess
768+
.source_map()
769+
.span_extend_while(last_expr.span, |c| c.is_whitespace())
770+
.ok()?
771+
.shrink_to_hi()
772+
.with_hi(last_stmt.span.hi())
767773
};
774+
768775
Some((span, needs_box))
769776
}
770777

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Ensures our "remove semicolon" suggestion isn't hardcoded with a character width,
2+
// in case it was accidentally mixed up with a greek question mark.
3+
// issue: rust-lang/rust#123607
4+
5+
pub fn square(num: i32) -> i32 {
6+
//~^ ERROR mismatched types
7+
num * num;
8+
//~^ ERROR unknown start of token
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: unknown start of token: \u{37e}
2+
--> $DIR/remove-semi-but-confused-char.rs:7:14
3+
|
4+
LL | num * num;
5+
| ^
6+
|
7+
help: Unicode character ';' (Greek Question Mark) looks like ';' (Semicolon), but it is not
8+
|
9+
LL | num * num;
10+
| ~
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/remove-semi-but-confused-char.rs:5:28
14+
|
15+
LL | pub fn square(num: i32) -> i32 {
16+
| ------ ^^^ expected `i32`, found `()`
17+
| |
18+
| implicitly returns `()` as its body has no tail or `return` expression
19+
LL |
20+
LL | num * num;
21+
| - help: remove this semicolon to return this value
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)