diff options
| author | Paul Gey <narpfel@gmx.de> | 2023-10-16 20:02:50 +0200 |
|---|---|---|
| committer | Paul Gey <narpfel@gmx.de> | 2023-10-16 21:20:21 +0200 |
| commit | 587899e9ca5a836ffd71c6bd77885bb1f14c5b4a (patch) | |
| tree | 52ccb5c1f8db3f6c516330f066178d4627bc6993 | |
| parent | 98c1e3d95ba7f5d29915dac3f391a296648aa74c (diff) | |
| download | rust-587899e9ca5a836ffd71c6bd77885bb1f14c5b4a.tar.gz rust-587899e9ca5a836ffd71c6bd77885bb1f14c5b4a.zip | |
Preserve unicode escapes in format string literals when pretty-printing AST
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pprust/state/expr.rs | 4 | ||||
| -rw-r--r-- | tests/pretty/format-args-str-escape.pp | 21 | ||||
| -rw-r--r-- | tests/pretty/format-args-str-escape.rs | 10 |
3 files changed, 33 insertions, 2 deletions
diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index 1142d492160..269cb8f2380 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -684,8 +684,8 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St for piece in pieces { match piece { FormatArgsPiece::Literal(s) => { - for c in s.as_str().escape_debug() { - template.push(c); + for c in s.as_str().chars() { + template.extend(c.escape_debug()); if let '{' | '}' = c { template.push(c); } diff --git a/tests/pretty/format-args-str-escape.pp b/tests/pretty/format-args-str-escape.pp new file mode 100644 index 00000000000..b84bc2303b7 --- /dev/null +++ b/tests/pretty/format-args-str-escape.pp @@ -0,0 +1,21 @@ +#![feature(prelude_import)] +#![no_std] +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +// pretty-compare-only +// pretty-mode:expanded +// pp-exact:format-args-str-escape.pp + +fn main() { + { ::std::io::_print(format_args!("\u{1b}[1mHello, world!\u{1b}[0m\n")); }; + { ::std::io::_print(format_args!("\u{1b}[1mHello, world!\u{1b}[0m\n")); }; + { + ::std::io::_print(format_args!("Not an escape sequence: \\u{{1B}}[1mbold\\x1B[0m\n")); + }; + { + ::std::io::_print(format_args!("{0}\n", + "\x1B[1mHello, world!\x1B[0m")); + }; +} diff --git a/tests/pretty/format-args-str-escape.rs b/tests/pretty/format-args-str-escape.rs new file mode 100644 index 00000000000..e596fcfd8bc --- /dev/null +++ b/tests/pretty/format-args-str-escape.rs @@ -0,0 +1,10 @@ +// pretty-compare-only +// pretty-mode:expanded +// pp-exact:format-args-str-escape.pp + +fn main() { + println!("\x1B[1mHello, world!\x1B[0m"); + println!("\u{1B}[1mHello, world!\u{1B}[0m"); + println!("Not an escape sequence: \\u{{1B}}[1mbold\\x1B[0m"); + println!("{}", "\x1B[1mHello, world!\x1B[0m"); +} |
