Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,6 @@ parse_or_in_let_chain = `||` operators are not supported in let chain conditions
parse_or_pattern_not_allowed_in_fn_parameters = function parameters require top-level or-patterns in parentheses
parse_or_pattern_not_allowed_in_let_binding = `let` bindings require top-level or-patterns in parentheses
parse_out_of_range_hex_escape = out of range hex escape
.label = must be a character in the range [\x00-\x7f]
parse_outer_attr_explanation = outer attributes, like `#[test]`, annotate the item following them
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,12 +2455,6 @@ pub(crate) enum UnescapeError {
is_hex: bool,
ch: String,
},
#[diag(parse_out_of_range_hex_escape)]
OutOfRangeHexEscape(
#[primary_span]
#[label]
Span,
),
#[diag(parse_leading_underscore_unicode_escape)]
LeadingUnderscoreUnicodeEscape {
#[primary_span]
Expand Down
22 changes: 21 additions & 1 deletion compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,27 @@ pub(crate) fn emit_unescape_error(
err.emit()
}
EscapeError::OutOfRangeHexEscape => {
dcx.emit_err(UnescapeError::OutOfRangeHexEscape(err_span))
let err_span = full_lit_span
.with_lo(full_lit_span.lo() + BytePos(1))
.with_hi(full_lit_span.hi() - BytePos(1));
let mut err = dcx.struct_span_err(err_span, "out of range hex escape");
err.span_label(err_span, "must be a character in the range [\\x00-\\x7f]");

let escape_str = &lit[range];
if lit.len() <= 4
&& escape_str.len() == 4
&& escape_str.starts_with("\\x")
&& let Ok(value) = u8::from_str_radix(&escape_str[2..4], 16)
&& matches!(mode, Mode::Char | Mode::Str)
{
err.help(format!("if you want to write a byte literal, use `b'{}'`", escape_str));
err.help(format!(
"if you want to write a Unicode character, use `'\\u{{{:X}}}'`",
value
));
}

err.emit()
}
EscapeError::LeadingUnderscoreUnicodeEscape => {
let (c, span) = last_char();
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/parser/ascii-only-character-escape.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ error: out of range hex escape
|
LL | let x = "\x80";
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\x80'`
= help: if you want to write a Unicode character, use `'\u{80}'`

error: out of range hex escape
--> $DIR/ascii-only-character-escape.rs:3:14
|
LL | let y = "\xff";
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xff'`
= help: if you want to write a Unicode character, use `'\u{FF}'`

error: out of range hex escape
--> $DIR/ascii-only-character-escape.rs:4:14
|
LL | let z = "\xe2";
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xe2'`
= help: if you want to write a Unicode character, use `'\u{E2}'`

error: aborting due to 3 previous errors

19 changes: 19 additions & 0 deletions tests/ui/parser/out-of-range-hex-escape-suggestions-148917.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn main() {
let _c = '\xFF'; //~ ERROR out of range hex escape
let _s = "\xFF"; //~ ERROR out of range hex escape

let _c2 = '\xff'; //~ ERROR out of range hex escape
let _s2 = "\xff"; //~ ERROR out of range hex escape

let _c3 = '\x80'; //~ ERROR out of range hex escape
let _s3 = "\x80"; //~ ERROR out of range hex escape

// Byte literals should not get suggestions (they're already valid)
let _b = b'\xFF'; // OK
let _bs = b"\xFF"; // OK

dbg!('\xFF'); //~ ERROR out of range hex escape

// do not suggest for out of range escapes that are too long
dbg!("\xFFFFF"); //~ ERROR out of range hex escape
}
71 changes: 71 additions & 0 deletions tests/ui/parser/out-of-range-hex-escape-suggestions-148917.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:2:15
|
LL | let _c = '\xFF';
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xFF'`
= help: if you want to write a Unicode character, use `'\u{FF}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:3:15
|
LL | let _s = "\xFF";
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xFF'`
= help: if you want to write a Unicode character, use `'\u{FF}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:5:16
|
LL | let _c2 = '\xff';
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xff'`
= help: if you want to write a Unicode character, use `'\u{FF}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:6:16
|
LL | let _s2 = "\xff";
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xff'`
= help: if you want to write a Unicode character, use `'\u{FF}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:8:16
|
LL | let _c3 = '\x80';
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\x80'`
= help: if you want to write a Unicode character, use `'\u{80}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:9:16
|
LL | let _s3 = "\x80";
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\x80'`
= help: if you want to write a Unicode character, use `'\u{80}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:15:11
|
LL | dbg!('\xFF');
| ^^^^ must be a character in the range [\x00-\x7f]
|
= help: if you want to write a byte literal, use `b'\xFF'`
= help: if you want to write a Unicode character, use `'\u{FF}'`

error: out of range hex escape
--> $DIR/out-of-range-hex-escape-suggestions-148917.rs:18:11
|
LL | dbg!("\xFFFFF");
| ^^^^^^^ must be a character in the range [\x00-\x7f]

error: aborting due to 8 previous errors

Loading