Skip to content

suggest hex escapes for C-style escapes#156376

Open
euclio wants to merge 1 commit intorust-lang:mainfrom
euclio:foreign-escapes
Open

suggest hex escapes for C-style escapes#156376
euclio wants to merge 1 commit intorust-lang:mainfrom
euclio:foreign-escapes

Conversation

@euclio
Copy link
Copy Markdown
Contributor

@euclio euclio commented May 9, 2026

Fixes #148884.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 9, 2026

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 9, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 9, 2026

r? @mu001999

rustbot has assigned @mu001999.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@asquared31415
Copy link
Copy Markdown
Contributor

I don't think that the "(perhaps transcribing from another language)" part is useful to the diagnostic. It should be clear from context that Rust simply does this differently.

Copy link
Copy Markdown
Member

@mu001999 mu001999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Just some nits

View changes since this review

Comment thread compiler/rustc_parse/src/lexer/unescape_error_reporting.rs Outdated
Comment thread compiler/rustc_parse/src/lexer/unescape_error_reporting.rs Outdated
Comment thread compiler/rustc_parse/src/lexer/unescape_error_reporting.rs Outdated
Comment thread compiler/rustc_parse/src/lexer/unescape_error_reporting.rs Outdated
@mu001999
Copy link
Copy Markdown
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 10, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@euclio euclio force-pushed the foreign-escapes branch from 774186b to 1ee8da5 Compare May 10, 2026 04:15
@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
##[endgroup]
Executing "/scripts/stage_2_test_set1.sh"
+ /scripts/stage_2_test_set1.sh
+ '[' 1 == 1 ']'
+ echo 'PR_CI_JOB set; skipping tidy'
+ SKIP_TIDY='--skip tidy'
+ ../x.py --stage 2 test --skip tidy --skip compiler --skip src
PR_CI_JOB set; skipping tidy
##[group]Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.04s
##[endgroup]
---

5    |                                ^ unknown byte escape
6    |
7    = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL - static FOO: &'static [u8] = b"\f";
+ LL + static FOO: &'static [u8] = b"\x0C";
+    |
8 
9 error: unknown byte escape: `f`
10   --> $DIR/byte-string-literals.rs:4:8

13    |        ^ unknown byte escape
14    |
15    = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL -     b"\f";
+ LL +     b"\x0C";
+    |
16 
17 error: invalid character in numeric character escape: `Z`
18   --> $DIR/byte-string-literals.rs:5:10

Note: some mismatched output was normalized before being compared
- LL - static FOO: &'static [u8] = b"\f";  //~ ERROR unknown byte escape
- LL + static FOO: &'static [u8] = b"\\x0C";  //~ ERROR unknown byte escape
- LL -     b"\f";  //~ ERROR unknown byte escape
- LL +     b"\\x0C";  //~ ERROR unknown byte escape
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL - static FOO: &'static [u8] = b"\f";
+ LL + static FOO: &'static [u8] = b"\x0C";
+    |
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL -     b"\f";
+ LL +     b"\x0C";
+    |


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args parser/byte-string-literals.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/parser/byte-string-literals.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/parser/byte-string-literals" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: unknown byte escape: `f`
##[error]  --> /checkout/tests/ui/parser/byte-string-literals.rs:1:32
   |
LL | static FOO: &'static [u8] = b"\f";  //~ ERROR unknown byte escape
   |                                ^ unknown byte escape
   |
   = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
   |
LL - static FOO: &'static [u8] = b"\f";  //~ ERROR unknown byte escape
LL + static FOO: &'static [u8] = b"\\x0C";  //~ ERROR unknown byte escape
   |

error: unknown byte escape: `f`
##[error]  --> /checkout/tests/ui/parser/byte-string-literals.rs:4:8
   |
LL |     b"\f";  //~ ERROR unknown byte escape
   |        ^ unknown byte escape
   |
   = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
   |
LL -     b"\f";  //~ ERROR unknown byte escape
LL +     b"\\x0C";  //~ ERROR unknown byte escape
   |

error: invalid character in numeric character escape: `Z`
##[error]  --> /checkout/tests/ui/parser/byte-string-literals.rs:5:10
   |
LL |     b"\x0Z";  //~ ERROR invalid character in numeric character escape: `Z`
   |          ^ invalid character in numeric character escape

error: non-ASCII character in byte string literal
##[error]  --> /checkout/tests/ui/parser/byte-string-literals.rs:6:7
   |
LL |     b"é";  //~ ERROR non-ASCII character in byte string literal
   |       ^ must be ASCII
   |
help: if you meant to use the unicode code point for 'é', use a \xHH escape
   |
LL -     b"é";  //~ ERROR non-ASCII character in byte string literal
LL +     b"\xE9";  //~ ERROR non-ASCII character in byte string literal
   |

error: non-ASCII character in raw byte string literal
##[error]  --> /checkout/tests/ui/parser/byte-string-literals.rs:7:10
   |
LL |     br##"é"##;  //~ ERROR non-ASCII character in raw byte string literal
   |          ^ must be ASCII

error[E0766]: unterminated double quote byte string
##[error]  --> /checkout/tests/ui/parser/byte-string-literals.rs:8:6
   |
LL |       b"a  //~ ERROR unterminated double quote byte string
   |  ______^
LL | | }
   | |__^

error: aborting due to 6 previous errors
---

5    |                     ^ unknown byte escape
6    |
7    = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL - static FOO: u8 = b'\f';
+ LL + static FOO: u8 = b'\x0C';
+    |
8 
9 error: unknown byte escape: `f`
10   --> $DIR/byte-literals.rs:6:8

13    |        ^ unknown byte escape
14    |
15    = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL -     b'\f';
+ LL +     b'\x0C';
+    |
16 
17 error: invalid character in numeric character escape: `Z`
18   --> $DIR/byte-literals.rs:7:10

Note: some mismatched output was normalized before being compared
- LL - static FOO: u8 = b'\f';  //~ ERROR unknown byte escape
- LL + static FOO: u8 = b'\\x0C';  //~ ERROR unknown byte escape
- LL -     b'\f';  //~ ERROR unknown byte escape
- LL +     b'\\x0C';  //~ ERROR unknown byte escape
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL - static FOO: u8 = b'\f';
+ LL + static FOO: u8 = b'\x0C';
+    |
+ help: if you meant to write a form feed, use a hex escape
+    |
+ LL -     b'\f';
+ LL +     b'\x0C';
+    |


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args parser/byte-literals.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/parser/byte-literals.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/parser/byte-literals" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error: unknown byte escape: `f`
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:3:21
   |
LL | static FOO: u8 = b'\f';  //~ ERROR unknown byte escape
   |                     ^ unknown byte escape
   |
   = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
   |
LL - static FOO: u8 = b'\f';  //~ ERROR unknown byte escape
LL + static FOO: u8 = b'\\x0C';  //~ ERROR unknown byte escape
   |

error: unknown byte escape: `f`
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:6:8
   |
LL |     b'\f';  //~ ERROR unknown byte escape
   |        ^ unknown byte escape
   |
   = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals>
help: if you meant to write a form feed, use a hex escape
   |
LL -     b'\f';  //~ ERROR unknown byte escape
LL +     b'\\x0C';  //~ ERROR unknown byte escape
   |

error: invalid character in numeric character escape: `Z`
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:7:10
   |
LL |     b'\x0Z';  //~ ERROR invalid character in numeric character escape: `Z`
   |          ^ invalid character in numeric character escape

error: byte constant must be escaped: `\t`
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:8:7
   |
LL |     b'    ';  //~ ERROR byte constant must be escaped
   |       ^^^^
   |
help: escape the character
   |
LL |     b'\t';  //~ ERROR byte constant must be escaped
   |       ++

error: byte constant must be escaped: `'`
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:9:7
   |
LL |     b''';  //~ ERROR byte constant must be escaped
   |       ^
   |
help: escape the character
   |
LL |     b'\'';  //~ ERROR byte constant must be escaped
   |       +

error: non-ASCII character in byte literal
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:10:7
   |
LL |     b'é';  //~ ERROR non-ASCII character in byte literal
   |       ^ must be ASCII
   |
help: if you meant to use the unicode code point for 'é', use a \xHH escape
   |
LL -     b'é';  //~ ERROR non-ASCII character in byte literal
LL +     b'\xE9';  //~ ERROR non-ASCII character in byte literal
   |

error[E0763]: unterminated byte constant
##[error]  --> /checkout/tests/ui/parser/byte-literals.rs:11:6
   |
LL |     b'a  //~ ERROR unterminated byte constant [E0763]
   |      ^^^^

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0763`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Diagnostics should recognize string/char escape sequences from C and suggest appropriate Rust equivalents

5 participants