Skip to content

Commit efa8a29

Browse files
sinkuupietroalbini
authored andcommitted
Fix assertion message generation
1 parent fd9c710 commit efa8a29

File tree

4 files changed

+25
-58
lines changed

4 files changed

+25
-58
lines changed

src/libsyntax/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#![cfg_attr(stage0, feature(i128_type))]
2828
#![feature(const_atomic_usize_new)]
2929
#![feature(rustc_attrs)]
30+
#![feature(str_escape)]
3031

3132
// See librustc_cratesio_shim/Cargo.toml for a comment explaining this.
3233
#[allow(unused_extern_crates)]

src/libsyntax/print/pprust.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ pub trait PrintState<'a> {
651651
style: ast::StrStyle) -> io::Result<()> {
652652
let st = match style {
653653
ast::StrStyle::Cooked => {
654-
(format!("\"{}\"", parse::escape_default(st)))
654+
(format!("\"{}\"", st.escape_debug()))
655655
}
656656
ast::StrStyle::Raw(n) => {
657657
(format!("r{delim}\"{string}\"{delim}",

src/libsyntax_ext/assert.rs

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

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+
// must-compile-successfully
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)