Skip to content

Commit 04114cd

Browse files
committed
Auto merge of #116124 - WaffleLapkin:fix-proc-macro-literal-to-string, r=compiler-errors
Properly print cstr literals in `proc_macro::Literal::to_string` Previously we printed the contents of the string, rather than the actual string literal (e.g. `the c string` instead of `c"the c string"`). Fixes #112820 cc #105723
2 parents 67ad3c2 + 99a2fa1 commit 04114cd

File tree

4 files changed

+158
-1
lines changed

4 files changed

+158
-1
lines changed

library/proc_macro/src/lib.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,15 @@ impl Literal {
14181418
let hashes = get_hashes_str(n);
14191419
f(&["br", hashes, "\"", symbol, "\"", hashes, suffix])
14201420
}
1421-
_ => f(&[symbol, suffix]),
1421+
bridge::LitKind::CStr => f(&["c\"", symbol, "\"", suffix]),
1422+
bridge::LitKind::CStrRaw(n) => {
1423+
let hashes = get_hashes_str(n);
1424+
f(&["cr", hashes, "\"", symbol, "\"", hashes, suffix])
1425+
}
1426+
1427+
bridge::LitKind::Integer | bridge::LitKind::Float | bridge::LitKind::Err => {
1428+
f(&[symbol, suffix])
1429+
}
14221430
})
14231431
}
14241432
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
#![crate_type = "proc-macro"]
4+
5+
extern crate proc_macro;
6+
7+
use proc_macro::{TokenStream, TokenTree};
8+
9+
#[proc_macro]
10+
pub fn print_tokens(input: TokenStream) -> TokenStream {
11+
println!("{:#?}", input);
12+
for token in input {
13+
println!("{token}");
14+
}
15+
TokenStream::new()
16+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// check-pass
2+
// edition: 2021
3+
#![feature(c_str_literals)]
4+
5+
// aux-build: print-tokens.rs
6+
extern crate print_tokens;
7+
8+
fn main() {
9+
print_tokens::print_tokens! {
10+
1
11+
17u8
12+
42.
13+
3.14f32
14+
b'a'
15+
b'\xFF'
16+
'c'
17+
'\x32'
18+
"\"str\""
19+
r#""raw" str"#
20+
r###"very ##"raw"## str"###
21+
b"\"byte\" str"
22+
br#""raw" "byte" str"#
23+
c"\"c\" str"
24+
cr#""raw" "c" str"#
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
TokenStream [
2+
Literal {
3+
kind: Integer,
4+
symbol: "1",
5+
suffix: None,
6+
span: #0 bytes(172..173),
7+
},
8+
Literal {
9+
kind: Integer,
10+
symbol: "17",
11+
suffix: Some("u8"),
12+
span: #0 bytes(182..186),
13+
},
14+
Literal {
15+
kind: Float,
16+
symbol: "42.",
17+
suffix: None,
18+
span: #0 bytes(195..198),
19+
},
20+
Literal {
21+
kind: Float,
22+
symbol: "3.14",
23+
suffix: Some("f32"),
24+
span: #0 bytes(207..214),
25+
},
26+
Literal {
27+
kind: Byte,
28+
symbol: "a",
29+
suffix: None,
30+
span: #0 bytes(223..227),
31+
},
32+
Literal {
33+
kind: Byte,
34+
symbol: "\xFF",
35+
suffix: None,
36+
span: #0 bytes(236..243),
37+
},
38+
Literal {
39+
kind: Char,
40+
symbol: "c",
41+
suffix: None,
42+
span: #0 bytes(252..255),
43+
},
44+
Literal {
45+
kind: Char,
46+
symbol: "\x32",
47+
suffix: None,
48+
span: #0 bytes(264..270),
49+
},
50+
Literal {
51+
kind: Str,
52+
symbol: "\\"str\\"",
53+
suffix: None,
54+
span: #0 bytes(279..288),
55+
},
56+
Literal {
57+
kind: StrRaw(1),
58+
symbol: "\"raw\" str",
59+
suffix: None,
60+
span: #0 bytes(297..311),
61+
},
62+
Literal {
63+
kind: StrRaw(3),
64+
symbol: "very ##\"raw\"## str",
65+
suffix: None,
66+
span: #0 bytes(320..347),
67+
},
68+
Literal {
69+
kind: ByteStr,
70+
symbol: "\\"byte\\" str",
71+
suffix: None,
72+
span: #0 bytes(356..371),
73+
},
74+
Literal {
75+
kind: ByteStrRaw(1),
76+
symbol: "\"raw\" \"byte\" str",
77+
suffix: None,
78+
span: #0 bytes(380..402),
79+
},
80+
Literal {
81+
kind: CStr,
82+
symbol: "\\"c\\" str",
83+
suffix: None,
84+
span: #0 bytes(411..423),
85+
},
86+
Literal {
87+
kind: CStrRaw(1),
88+
symbol: "\"raw\" \"c\" str",
89+
suffix: None,
90+
span: #0 bytes(432..451),
91+
},
92+
]
93+
1
94+
17u8
95+
42.
96+
3.14f32
97+
b'a'
98+
b'\xFF'
99+
'c'
100+
'\x32'
101+
"\"str\""
102+
r#""raw" str"#
103+
r###"very ##"raw"## str"###
104+
b"\"byte\" str"
105+
br#""raw" "byte" str"#
106+
c"\"c\" str"
107+
cr#""raw" "c" str"#

0 commit comments

Comments
 (0)