Skip to content

Commit 39df223

Browse files
committed
Fix assertion message generation
1 parent f9bfe84 commit 39df223

File tree

3 files changed

+24
-58
lines changed

3 files changed

+24
-58
lines changed

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ pub trait PrintState<'a> {
656656
style: ast::StrStyle) -> io::Result<()> {
657657
let st = match style {
658658
ast::StrStyle::Cooked => {
659-
(format!("\"{}\"", st.escape_default()))
659+
(format!("\"{}\"", st.escape_debug()))
660660
}
661661
ast::StrStyle::Raw(n) => {
662662
(format!("r{delim}\"{string}\"{delim}",

src/libsyntax_ext/assert.rs

+4-57
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,13 @@ pub fn expand_assert<'cx>(
4242
tts: if let Some(ts) = custom_msg_args {
4343
ts.into()
4444
} else {
45-
// `expr_to_string` escapes the string literals with `.escape_default()`
46-
// which escapes all non-ASCII characters with `\u`.
47-
let escaped_expr = escape_format_string(&unescape_printable_unicode(
48-
&pprust::expr_to_string(&cond_expr),
49-
));
50-
5145
TokenStream::from(TokenTree::Token(
5246
DUMMY_SP,
5347
token::Literal(
54-
token::Lit::Str_(Name::intern(&format!("assertion failed: {}", escaped_expr))),
48+
token::Lit::Str_(Name::intern(&format!(
49+
"assertion failed: {}",
50+
pprust::expr_to_string(&cond_expr).escape_debug()
51+
))),
5552
None,
5653
),
5754
)).into()
@@ -71,53 +68,3 @@ pub fn expand_assert<'cx>(
7168
);
7269
MacEager::expr(if_expr)
7370
}
74-
75-
/// Escapes a string for use as a formatting string.
76-
fn escape_format_string(s: &str) -> String {
77-
let mut res = String::with_capacity(s.len());
78-
for c in s.chars() {
79-
res.extend(c.escape_debug());
80-
match c {
81-
'{' | '}' => res.push(c),
82-
_ => {}
83-
}
84-
}
85-
res
86-
}
87-
88-
#[test]
89-
fn test_escape_format_string() {
90-
assert!(escape_format_string(r"foo{}\") == r"foo{{}}\\");
91-
}
92-
93-
/// Unescapes the escaped unicodes (`\u{...}`) that are printable.
94-
fn unescape_printable_unicode(mut s: &str) -> String {
95-
use std::{char, u32};
96-
97-
let mut res = String::with_capacity(s.len());
98-
99-
loop {
100-
if let Some(start) = s.find(r"\u{") {
101-
res.push_str(&s[0..start]);
102-
s = &s[start..];
103-
s.find('}')
104-
.and_then(|end| {
105-
let v = u32::from_str_radix(&s[3..end], 16).ok()?;
106-
let c = char::from_u32(v)?;
107-
// Escape unprintable characters.
108-
res.extend(c.escape_debug());
109-
s = &s[end + 1..];
110-
Some(())
111-
})
112-
.expect("lexer should have rejected invalid escape sequences");
113-
} else {
114-
res.push_str(s);
115-
return res;
116-
}
117-
}
118-
}
119-
120-
#[test]
121-
fn test_unescape_printable_unicode() {
122-
assert!(unescape_printable_unicode(r"\u{2603}\n\u{0}") == r"☃\n\u{0}");
123-
}

src/test/compile-fail/issue-50471.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-pass
12+
13+
fn main() {
14+
assert!({false});
15+
16+
assert!(r"\u{41}" == "A");
17+
18+
assert!(r"\u{".is_empty());
19+
}

0 commit comments

Comments
 (0)