Skip to content

Commit 94c439b

Browse files
petrochenkovpietroalbini
authored andcommitted
lexer: Avoid some span arithmetic in emit_unescape_error
1 parent fe8e0c5 commit 94c439b

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

compiler/rustc_parse/src/lexer/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,15 @@ impl<'a> StringReader<'a> {
532532
if let Err(err) = result {
533533
let span_with_quotes =
534534
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1));
535+
let (start, end) = (range.start as u32, range.end as u32);
536+
let lo = content_start + BytePos(start);
537+
let hi = lo + BytePos(end - start);
538+
let span = self.mk_sp(lo, hi);
535539
emit_unescape_error(
536540
&self.sess.span_diagnostic,
537541
lit_content,
538542
span_with_quotes,
543+
span,
539544
mode,
540545
range,
541546
err,

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub(crate) fn emit_unescape_error(
1313
lit: &str,
1414
// full span of the literal, including quotes
1515
span_with_quotes: Span,
16+
// interior span of the literal, without quotes
17+
span: Span,
1618
mode: Mode,
1719
// range of the error inside `lit`
1820
range: Range<usize>,
@@ -26,13 +28,6 @@ pub(crate) fn emit_unescape_error(
2628
range,
2729
error
2830
);
29-
let span = {
30-
let Range { start, end } = range;
31-
let (start, end) = (start as u32, end as u32);
32-
let lo = span_with_quotes.lo() + BytePos(start + 1);
33-
let hi = lo + BytePos(end - start);
34-
span_with_quotes.with_lo(lo).with_hi(hi)
35-
};
3631
let last_char = || {
3732
let c = lit[range.clone()].chars().rev().next().unwrap();
3833
let span = span.with_lo(span.hi() - BytePos(c.len_utf8() as u32));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[rustc_dummy = b"ffi.rs"] //~ ERROR byte constant must be ASCII
4+
//~| ERROR byte constant must be ASCII
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: byte constant must be ASCII. Use a \xHH escape for a non-ASCII byte
2+
--> $DIR/key-value-non-ascii.rs:3:19
3+
|
4+
LL | #[rustc_dummy = b"ffi.rs"]
5+
| ^
6+
7+
error: byte constant must be ASCII. Use a \xHH escape for a non-ASCII byte
8+
--> $DIR/key-value-non-ascii.rs:3:21
9+
|
10+
LL | #[rustc_dummy = b"ffi.rs"]
11+
| ^^^
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)