Skip to content

Commit 4d65ef4

Browse files
committed
Tell unicode escapes can’t be used as bytes earlier/more
1 parent d22f189 commit 4d65ef4

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -738,26 +738,24 @@ impl<'a> StringReader<'a> {
738738
return match e {
739739
'n' | 'r' | 't' | '\\' | '\'' | '"' | '0' => true,
740740
'x' => self.scan_byte_escape(delim, !ascii_only),
741-
'u' if self.curr_is('{') => {
742-
let valid = self.scan_unicode_escape(delim);
743-
if valid && ascii_only {
744-
self.err_span_(
745-
start,
746-
self.last_pos,
741+
'u' => {
742+
let valid = if self.curr_is('{') {
743+
self.scan_unicode_escape(delim) && !ascii_only
744+
} else {
745+
self.err_span_(start, self.last_pos,
746+
"incorrect unicode escape sequence");
747+
self.help_span_(start, self.last_pos,
748+
"format of unicode escape sequences is `\\u{…}`");
749+
false
750+
};
751+
if ascii_only {
752+
self.err_span_(start, self.last_pos,
747753
"unicode escape sequences cannot be used as a byte or in \
748754
a byte string"
749755
);
750-
false
751-
} else {
752-
valid
753756
}
754-
}
755-
'u' if !ascii_only => {
756-
self.err_span_(start, self.last_pos,
757-
"incomplete unicode escape sequence");
758-
self.help_span_(start, self.last_pos,
759-
"format of unicode escape sequences is `\\u{…}`");
760-
false
757+
valid
758+
761759
}
762760
'\n' if delim == '"' => {
763761
self.consume_whitespace();

src/test/parse-fail/issue-23620-invalid-escapes.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ fn main() {
1616
//~^ ERROR unicode escape sequences cannot be used as a byte or in a byte string
1717

1818
let _ = b'\u';
19-
//~^ ERROR unknown byte escape: u
19+
//~^ ERROR incorrect unicode escape sequence
20+
//~^^ ERROR unicode escape sequences cannot be used as a byte or in a byte string
2021

2122
let _ = b'\x5';
2223
//~^ ERROR numeric character escape is too short
@@ -35,11 +36,12 @@ fn main() {
3536
let _ = b"\u{a4a4} \xf \u";
3637
//~^ ERROR unicode escape sequences cannot be used as a byte or in a byte string
3738
//~^^ ERROR illegal character in numeric character escape:
38-
//~^^^ ERROR unknown byte escape: u
39+
//~^^^ ERROR incorrect unicode escape sequence
40+
//~^^^^ ERROR unicode escape sequences cannot be used as a byte or in a byte string
3941

4042
let _ = "\u{ffffff} \xf \u";
4143
//~^ ERROR illegal unicode character escape
4244
//~^^ ERROR illegal character in numeric character escape:
4345
//~^^^ ERROR form of character escape may only be used with characters in the range [\x00-\x7f]
44-
//~^^^^ ERROR incomplete unicode escape sequence
46+
//~^^^^ ERROR incorrect unicode escape sequence
4547
}

0 commit comments

Comments
 (0)