-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
suggest hex escapes for C-style escapes #156376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
euclio
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
euclio:foreign-escapes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::iter::once; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| use std::ops::Range; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| use rustc_errors::{Applicability, DiagCtxtHandle, ErrorGuaranteed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| use rustc_errors::{Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| use rustc_literal_escaper::{EscapeError, Mode}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| use rustc_span::{BytePos, Span}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| use tracing::debug; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -172,6 +172,8 @@ pub(crate) fn emit_unescape_error( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| "for more information, visit \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| <https://doc.rust-lang.org/reference/tokens.html#literals>", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreign_escape_suggestion(&mut diag, (&ec, span), err_span); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag.emit() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -302,6 +304,43 @@ pub(crate) fn emit_unescape_error( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Add additional suggestions for escapes that are supported by C. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| fn foreign_escape_suggestion( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag: &mut Diag<'_>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| (escaped_char, escape_span): (&str, Span), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| err_span: Span, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if escaped_char == "?" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag.span_suggestion( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| err_span, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "if you meant to write a literal question mark, don't escape the character", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "?", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Applicability::MaybeIncorrect, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| let (name, hex) = match escaped_char { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "a" => ("an audible bell", String::from("07")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "b" => ("a backspace", String::from("08")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "f" => ("a form feed", String::from("0C")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "v" => ("a vertical tab", String::from("0B")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "e" => ("an ANSI escape sequence", String::from("1B")), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ec if u8::from_str_radix(ec, 8).is_ok() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag.help(r"if you meant to write an ASCII control code, use a `\xNN` hex escape"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ => return, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+323
to
+334
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| diag.span_suggestion( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| escape_span, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| format!("if you meant to write {name}, use a hex escape"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| format!("x{hex}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Applicability::MaybeIncorrect, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
euclio marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Pushes a character to a message string for error reporting | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| pub(crate) fn escaped_char(c: char) -> String { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| match c { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Specified by both C and Rust | ||
| pub const SINGLE_QUOTE: char = '\''; | ||
| pub const DOUBLE_QUOTE: char = '\"'; | ||
| pub const BACKSLASH: char = '\\'; | ||
| pub const NEWLINE: char = '\n'; | ||
| pub const CARRIAGE_RETURN: char = '\r'; | ||
| pub const HORIZONTAL_TAB: char = '\t'; | ||
| pub const NULL: char = '\0'; | ||
|
|
||
| // Specified by C, but not Rust | ||
| pub const QUESTION_MARK: char = '\?'; //~ ERROR unknown character escape | ||
| pub const AUDIBLE_BELL: char = '\a'; //~ ERROR unknown character escape | ||
| pub const BACKSPACE: char = '\b'; //~ ERROR unknown character escape | ||
| pub const FORM_FEED: char = '\f'; //~ ERROR unknown character escape | ||
| pub const VERTICAL_TAB: char = '\v'; //~ ERROR unknown character escape | ||
| pub const OCTAL: char = '\1'; //~ ERROR unknown character escape | ||
| pub const OCTAL_TWO_DIGIT: char = '\12'; //~ ERROR unknown character escape | ||
| pub const OCTAL_THREE_DIGIT: char = '\12'; //~ ERROR unknown character escape | ||
| pub const OCTAL_OUT_OF_RANGE: char = '\9'; //~ ERROR unknown character escape | ||
|
|
||
| // Not specified by C, but recognized by GCC as an extension. | ||
| // Used for ANSI escape sequences in terminal emulators. | ||
| pub const ESCAPE: char = '\e'; //~ ERROR unknown character escape | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| error: unknown character escape: `?` | ||
| --> $DIR/foreign-escapes.rs:11:35 | ||
| | | ||
| LL | pub const QUESTION_MARK: char = '\?'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const QUESTION_MARK: char = '\?'; | ||
| LL + pub const QUESTION_MARK: char = r"\?"; | ||
| | | ||
| help: if you meant to write a literal question mark, don't escape the character | ||
| | | ||
| LL - pub const QUESTION_MARK: char = '\?'; | ||
| LL + pub const QUESTION_MARK: char = '?'; | ||
| | | ||
|
|
||
| error: unknown character escape: `a` | ||
| --> $DIR/foreign-escapes.rs:12:34 | ||
| | | ||
| LL | pub const AUDIBLE_BELL: char = '\a'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const AUDIBLE_BELL: char = '\a'; | ||
| LL + pub const AUDIBLE_BELL: char = r"\a"; | ||
| | | ||
| help: if you meant to write an audible bell, use a hex escape | ||
| | | ||
| LL - pub const AUDIBLE_BELL: char = '\a'; | ||
| LL + pub const AUDIBLE_BELL: char = '\x07'; | ||
| | | ||
|
|
||
| error: unknown character escape: `b` | ||
| --> $DIR/foreign-escapes.rs:13:31 | ||
| | | ||
| LL | pub const BACKSPACE: char = '\b'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const BACKSPACE: char = '\b'; | ||
| LL + pub const BACKSPACE: char = r"\b"; | ||
| | | ||
| help: if you meant to write a backspace, use a hex escape | ||
| | | ||
| LL - pub const BACKSPACE: char = '\b'; | ||
| LL + pub const BACKSPACE: char = '\x08'; | ||
| | | ||
|
|
||
| error: unknown character escape: `f` | ||
| --> $DIR/foreign-escapes.rs:14:31 | ||
| | | ||
| LL | pub const FORM_FEED: char = '\f'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const FORM_FEED: char = '\f'; | ||
| LL + pub const FORM_FEED: char = r"\f"; | ||
| | | ||
| help: if you meant to write a form feed, use a hex escape | ||
| | | ||
| LL - pub const FORM_FEED: char = '\f'; | ||
| LL + pub const FORM_FEED: char = '\x0C'; | ||
| | | ||
|
|
||
| error: unknown character escape: `v` | ||
| --> $DIR/foreign-escapes.rs:15:34 | ||
| | | ||
| LL | pub const VERTICAL_TAB: char = '\v'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const VERTICAL_TAB: char = '\v'; | ||
| LL + pub const VERTICAL_TAB: char = r"\v"; | ||
| | | ||
| help: if you meant to write a vertical tab, use a hex escape | ||
| | | ||
| LL - pub const VERTICAL_TAB: char = '\v'; | ||
| LL + pub const VERTICAL_TAB: char = '\x0B'; | ||
| | | ||
|
|
||
| error: unknown character escape: `1` | ||
| --> $DIR/foreign-escapes.rs:16:27 | ||
| | | ||
| LL | pub const OCTAL: char = '\1'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| = help: if you meant to write an ASCII control code, use a `\xNN` hex escape | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const OCTAL: char = '\1'; | ||
| LL + pub const OCTAL: char = r"\1"; | ||
| | | ||
|
|
||
| error: unknown character escape: `1` | ||
| --> $DIR/foreign-escapes.rs:17:37 | ||
| | | ||
| LL | pub const OCTAL_TWO_DIGIT: char = '\12'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| = help: if you meant to write an ASCII control code, use a `\xNN` hex escape | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const OCTAL_TWO_DIGIT: char = '\12'; | ||
| LL + pub const OCTAL_TWO_DIGIT: char = r"\12"; | ||
| | | ||
|
|
||
| error: unknown character escape: `1` | ||
| --> $DIR/foreign-escapes.rs:18:39 | ||
| | | ||
| LL | pub const OCTAL_THREE_DIGIT: char = '\12'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| = help: if you meant to write an ASCII control code, use a `\xNN` hex escape | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const OCTAL_THREE_DIGIT: char = '\12'; | ||
| LL + pub const OCTAL_THREE_DIGIT: char = r"\12"; | ||
| | | ||
|
|
||
| error: unknown character escape: `9` | ||
| --> $DIR/foreign-escapes.rs:19:40 | ||
| | | ||
| LL | pub const OCTAL_OUT_OF_RANGE: char = '\9'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const OCTAL_OUT_OF_RANGE: char = '\9'; | ||
| LL + pub const OCTAL_OUT_OF_RANGE: char = r"\9"; | ||
| | | ||
|
|
||
| error: unknown character escape: `e` | ||
| --> $DIR/foreign-escapes.rs:23:28 | ||
| | | ||
| LL | pub const ESCAPE: char = '\e'; | ||
| | ^ unknown character escape | ||
| | | ||
| = help: for more information, visit <https://doc.rust-lang.org/reference/tokens.html#literals> | ||
| help: if you meant to write a literal backslash (perhaps escaping in a regular expression), consider a raw string literal | ||
| | | ||
| LL - pub const ESCAPE: char = '\e'; | ||
| LL + pub const ESCAPE: char = r"\e"; | ||
| | | ||
| help: if you meant to write an ANSI escape sequence, use a hex escape | ||
| | | ||
| LL - pub const ESCAPE: char = '\e'; | ||
| LL + pub const ESCAPE: char = '\x1B'; | ||
| | | ||
|
|
||
| error: aborting due to 10 previous errors | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.