Skip to content

Commit

Permalink
lexer: Avoid some span arithmetic in emit_unescape_error
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Feb 7, 2021
1 parent 2efd070 commit e4e460b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_parse/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,15 @@ impl<'a> StringReader<'a> {
if let Err(err) = result {
let span_with_quotes =
self.mk_sp(content_start - BytePos(1), content_end + BytePos(1));
let (start, end) = (range.start as u32, range.end as u32);
let lo = content_start + BytePos(start);
let hi = lo + BytePos(end - start);
let span = self.mk_sp(lo, hi);
emit_unescape_error(
&self.sess.span_diagnostic,
lit_content,
span_with_quotes,
span,
mode,
range,
err,
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub(crate) fn emit_unescape_error(
lit: &str,
// full span of the literal, including quotes
span_with_quotes: Span,
// interior span of the literal, without quotes
span: Span,
mode: Mode,
// range of the error inside `lit`
range: Range<usize>,
Expand All @@ -26,13 +28,6 @@ pub(crate) fn emit_unescape_error(
range,
error
);
let span = {
let Range { start, end } = range;
let (start, end) = (start as u32, end as u32);
let lo = span_with_quotes.lo() + BytePos(start + 1);
let hi = lo + BytePos(end - start);
span_with_quotes.with_lo(lo).with_hi(hi)
};
let last_char = || {
let c = lit[range.clone()].chars().rev().next().unwrap();
let span = span.with_lo(span.hi() - BytePos(c.len_utf8() as u32));
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/attributes/key-value-non-ascii.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![feature(rustc_attrs)]

#[rustc_dummy = b"ffi.rs"] //~ ERROR byte constant must be ASCII
//~| ERROR byte constant must be ASCII
fn main() {}
14 changes: 14 additions & 0 deletions src/test/ui/attributes/key-value-non-ascii.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: byte constant must be ASCII. Use a \xHH escape for a non-ASCII byte
--> $DIR/key-value-non-ascii.rs:3:19
|
LL | #[rustc_dummy = b"ffi.rs"]
| ^

error: byte constant must be ASCII. Use a \xHH escape for a non-ASCII byte
--> $DIR/key-value-non-ascii.rs:3:21
|
LL | #[rustc_dummy = b"ffi.rs"]
| ^^^

error: aborting due to 2 previous errors

0 comments on commit e4e460b

Please sign in to comment.