Skip to content

Commit 05e2056

Browse files
Rollup merge of #116811 - narpfel:unpretty-unicode-escape-in-format-string-literal, r=Nilstrieb
Preserve unicode escapes in format string literals when pretty-printing AST Fixes #116799 Thanks to `@Nilstrieb` for the pointer to the correct location, that was really helpful for someone unfamiliar with the codebase.
2 parents 23000c3 + 587899e commit 05e2056

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,8 @@ pub fn reconstruct_format_args_template_string(pieces: &[FormatArgsPiece]) -> St
684684
for piece in pieces {
685685
match piece {
686686
FormatArgsPiece::Literal(s) => {
687-
for c in s.as_str().escape_debug() {
688-
template.push(c);
687+
for c in s.as_str().chars() {
688+
template.extend(c.escape_debug());
689689
if let '{' | '}' = c {
690690
template.push(c);
691691
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(prelude_import)]
2+
#![no_std]
3+
#[prelude_import]
4+
use ::std::prelude::rust_2015::*;
5+
#[macro_use]
6+
extern crate std;
7+
// pretty-compare-only
8+
// pretty-mode:expanded
9+
// pp-exact:format-args-str-escape.pp
10+
11+
fn main() {
12+
{ ::std::io::_print(format_args!("\u{1b}[1mHello, world!\u{1b}[0m\n")); };
13+
{ ::std::io::_print(format_args!("\u{1b}[1mHello, world!\u{1b}[0m\n")); };
14+
{
15+
::std::io::_print(format_args!("Not an escape sequence: \\u{{1B}}[1mbold\\x1B[0m\n"));
16+
};
17+
{
18+
::std::io::_print(format_args!("{0}\n",
19+
"\x1B[1mHello, world!\x1B[0m"));
20+
};
21+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// pretty-compare-only
2+
// pretty-mode:expanded
3+
// pp-exact:format-args-str-escape.pp
4+
5+
fn main() {
6+
println!("\x1B[1mHello, world!\x1B[0m");
7+
println!("\u{1B}[1mHello, world!\u{1B}[0m");
8+
println!("Not an escape sequence: \\u{{1B}}[1mbold\\x1B[0m");
9+
println!("{}", "\x1B[1mHello, world!\x1B[0m");
10+
}

0 commit comments

Comments
 (0)