Skip to content

Commit 27d523d

Browse files
authored
Rollup merge of #72233 - dtolnay:literal, r=petrochenkov
Fix {:#?} representation of proc_macro::Literal Before: ```rust TokenStream [ Ident { ident: "name", span: #0 bytes(37..41), }, Punct { ch: '=', spacing: Alone, span: #0 bytes(42..43), }, Literal { lit: Lit { kind: Str, symbol: "SNPP", suffix: None }, span: Span { lo: BytePos(44), hi: BytePos(50), ctxt: #0 } }, Punct { ch: ',', spacing: Alone, span: #0 bytes(50..51), }, Ident { ident: "owner", span: #0 bytes(56..61), }, Punct { ch: '=', spacing: Alone, span: #0 bytes(62..63), }, Literal { lit: Lit { kind: Str, symbol: "Canary M Burns", suffix: None }, span: Span { lo: BytePos(64), hi: BytePos(80), ctxt: #0 } }, ] ``` After: ```rust TokenStream [ Ident { ident: "name", span: #0 bytes(37..41), }, Punct { ch: '=', spacing: Alone, span: #0 bytes(42..43), }, Literal { kind: Str, symbol: "SNPP", suffix: None, span: #0 bytes(44..50), }, Punct { ch: ',', spacing: Alone, span: #0 bytes(50..51), }, Ident { ident: "owner", span: #0 bytes(56..61), }, Punct { ch: '=', spacing: Alone, span: #0 bytes(62..63), }, Literal { kind: Str, symbol: "Canary M Burns", suffix: None, span: #0 bytes(64..80), }, ] ```
2 parents 568e280 + bea2c59 commit 27d523d

File tree

8 files changed

+241
-9
lines changed

8 files changed

+241
-9
lines changed

Diff for: src/libproc_macro/bridge/client.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,16 @@ impl Clone for Literal {
202202
}
203203
}
204204

205-
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
206205
impl fmt::Debug for Literal {
207206
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
208-
f.write_str(&self.debug())
207+
f.debug_struct("Literal")
208+
// format the kind without quotes, as in `kind: Float`
209+
.field("kind", &format_args!("{}", &self.debug_kind()))
210+
.field("symbol", &self.symbol())
211+
// format `Some("...")` on one line even in {:#?} mode
212+
.field("suffix", &format_args!("{:?}", &self.suffix()))
213+
.field("span", &self.span())
214+
.finish()
209215
}
210216
}
211217

Diff for: src/libproc_macro/bridge/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ macro_rules! with_api {
103103
Literal {
104104
fn drop($self: $S::Literal);
105105
fn clone($self: &$S::Literal) -> $S::Literal;
106-
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
107-
fn debug($self: &$S::Literal) -> String;
106+
fn debug_kind($self: &$S::Literal) -> String;
107+
fn symbol($self: &$S::Literal) -> String;
108+
fn suffix($self: &$S::Literal) -> Option<String>;
108109
fn integer(n: &str) -> $S::Literal;
109110
fn typed_integer(n: &str, kind: &str) -> $S::Literal;
110111
fn float(n: &str) -> $S::Literal;

Diff for: src/libproc_macro/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,6 @@ impl fmt::Display for Literal {
11341134
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
11351135
impl fmt::Debug for Literal {
11361136
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1137-
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
11381137
self.0.fmt(f)
11391138
}
11401139
}

Diff for: src/librustc_expand/proc_macro_server.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,14 @@ impl server::Ident for Rustc<'_> {
507507
}
508508

509509
impl server::Literal for Rustc<'_> {
510-
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
511-
fn debug(&mut self, literal: &Self::Literal) -> String {
512-
format!("{:?}", literal)
510+
fn debug_kind(&mut self, literal: &Self::Literal) -> String {
511+
format!("{:?}", literal.lit.kind)
512+
}
513+
fn symbol(&mut self, literal: &Self::Literal) -> String {
514+
literal.lit.symbol.to_string()
515+
}
516+
fn suffix(&mut self, literal: &Self::Literal) -> Option<String> {
517+
literal.lit.suffix.as_ref().map(Symbol::to_string)
513518
}
514519
fn integer(&mut self, n: &str) -> Self::Literal {
515520
self.lit(token::Integer, Symbol::intern(n), None)

Diff for: src/test/ui/lint/redundant-semicolon/redundant-semi-proc-macro.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
TokenStream [Ident { ident: "fn", span: #0 bytes(198..200) }, Ident { ident: "span_preservation", span: #0 bytes(201..218) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(218..220) }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "let", span: #0 bytes(228..231) }, Ident { ident: "tst", span: #0 bytes(232..235) }, Punct { ch: '=', spacing: Alone, span: #0 bytes(236..237) }, Literal { lit: Lit { kind: Integer, symbol: "123", suffix: None }, span: Span { lo: BytePos(238), hi: BytePos(241), ctxt: #0 } }, Punct { ch: ';', spacing: Joint, span: #0 bytes(241..242) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(242..243) }, Ident { ident: "match", span: #0 bytes(289..294) }, Ident { ident: "tst", span: #0 bytes(295..298) }, Group { delimiter: Brace, stream: TokenStream [Literal { lit: Lit { kind: Integer, symbol: "123", suffix: None }, span: Span { lo: BytePos(483), hi: BytePos(486), ctxt: #0 } }, Punct { ch: '=', spacing: Joint, span: #0 bytes(487..489) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(487..489) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(490..492) }, Punct { ch: ',', spacing: Alone, span: #0 bytes(492..493) }, Ident { ident: "_", span: #0 bytes(502..503) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(504..506) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(504..506) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(507..509) }], span: #0 bytes(299..515) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(515..516) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(516..517) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(517..518) }], span: #0 bytes(222..562) }]
1+
TokenStream [Ident { ident: "fn", span: #0 bytes(198..200) }, Ident { ident: "span_preservation", span: #0 bytes(201..218) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(218..220) }, Group { delimiter: Brace, stream: TokenStream [Ident { ident: "let", span: #0 bytes(228..231) }, Ident { ident: "tst", span: #0 bytes(232..235) }, Punct { ch: '=', spacing: Alone, span: #0 bytes(236..237) }, Literal { kind: Integer, symbol: "123", suffix: None, span: #0 bytes(238..241) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(241..242) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(242..243) }, Ident { ident: "match", span: #0 bytes(289..294) }, Ident { ident: "tst", span: #0 bytes(295..298) }, Group { delimiter: Brace, stream: TokenStream [Literal { kind: Integer, symbol: "123", suffix: None, span: #0 bytes(483..486) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(487..489) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(487..489) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(490..492) }, Punct { ch: ',', spacing: Alone, span: #0 bytes(492..493) }, Ident { ident: "_", span: #0 bytes(502..503) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(504..506) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(504..506) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(507..509) }], span: #0 bytes(299..515) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(515..516) }, Punct { ch: ';', spacing: Joint, span: #0 bytes(516..517) }, Punct { ch: ';', spacing: Alone, span: #0 bytes(517..518) }], span: #0 bytes(222..562) }]
22
error: unnecessary trailing semicolon
33
--> $DIR/redundant-semi-proc-macro.rs:9:19
44
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
#![crate_name = "macro_dump_debug"]
6+
7+
extern crate proc_macro;
8+
use proc_macro::TokenStream;
9+
10+
#[proc_macro]
11+
pub fn dump_debug(tokens: TokenStream) -> TokenStream {
12+
eprintln!("{:?}", tokens);
13+
eprintln!("{:#?}", tokens);
14+
TokenStream::new()
15+
}

Diff for: src/test/ui/proc-macro/debug/dump-debug.rs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// run-pass
2+
// aux-build:macro-dump-debug.rs
3+
4+
extern crate macro_dump_debug;
5+
use macro_dump_debug::dump_debug;
6+
7+
dump_debug! {
8+
ident // ident
9+
r#ident // raw ident
10+
, // alone punct
11+
==> // joint punct
12+
() // empty group
13+
[_] // nonempty group
14+
15+
// unsuffixed literals
16+
0
17+
1.0
18+
"S"
19+
b"B"
20+
r"R"
21+
r##"R"##
22+
br"BR"
23+
br##"BR"##
24+
'C'
25+
b'B'
26+
27+
// suffixed literals
28+
0q
29+
1.0q
30+
"S"q
31+
b"B"q
32+
r"R"q
33+
r##"R"##q
34+
br"BR"q
35+
br##"BR"##q
36+
'C'q
37+
b'B'q
38+
}
39+
40+
fn main() {}

Diff for: src/test/ui/proc-macro/debug/dump-debug.stderr

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
TokenStream [Ident { ident: "ident", span: #0 bytes(130..135) }, Ident { ident: "r#ident", span: #0 bytes(151..158) }, Punct { ch: ',', spacing: Alone, span: #0 bytes(176..177) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(203..205) }, Punct { ch: '=', spacing: Joint, span: #0 bytes(203..205) }, Punct { ch: '>', spacing: Alone, span: #0 bytes(205..206) }, Group { delimiter: Parenthesis, stream: TokenStream [], span: #0 bytes(230..232) }, Group { delimiter: Bracket, stream: TokenStream [Ident { ident: "_", span: #0 bytes(258..259) }], span: #0 bytes(257..260) }, Literal { kind: Integer, symbol: "0", suffix: None, span: #0 bytes(315..316) }, Literal { kind: Float, symbol: "1.0", suffix: None, span: #0 bytes(321..324) }, Literal { kind: Str, symbol: "S", suffix: None, span: #0 bytes(329..332) }, Literal { kind: ByteStr, symbol: "B", suffix: None, span: #0 bytes(337..341) }, Literal { kind: StrRaw(0), symbol: "R", suffix: None, span: #0 bytes(346..350) }, Literal { kind: StrRaw(2), symbol: "R", suffix: None, span: #0 bytes(355..363) }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: None, span: #0 bytes(368..374) }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: None, span: #0 bytes(379..389) }, Literal { kind: Char, symbol: "C", suffix: None, span: #0 bytes(394..397) }, Literal { kind: Byte, symbol: "B", suffix: None, span: #0 bytes(402..406) }, Literal { kind: Integer, symbol: "0", suffix: Some("q"), span: #0 bytes(437..439) }, Literal { kind: Float, symbol: "1.0", suffix: Some("q"), span: #0 bytes(444..448) }, Literal { kind: Str, symbol: "S", suffix: Some("q"), span: #0 bytes(453..457) }, Literal { kind: ByteStr, symbol: "B", suffix: Some("q"), span: #0 bytes(462..467) }, Literal { kind: StrRaw(0), symbol: "R", suffix: Some("q"), span: #0 bytes(472..477) }, Literal { kind: StrRaw(2), symbol: "R", suffix: Some("q"), span: #0 bytes(482..491) }, Literal { kind: ByteStrRaw(0), symbol: "BR", suffix: Some("q"), span: #0 bytes(496..503) }, Literal { kind: ByteStrRaw(2), symbol: "BR", suffix: Some("q"), span: #0 bytes(508..519) }, Literal { kind: Char, symbol: "C", suffix: Some("q"), span: #0 bytes(524..528) }, Literal { kind: Byte, symbol: "B", suffix: Some("q"), span: #0 bytes(533..538) }]
2+
TokenStream [
3+
Ident {
4+
ident: "ident",
5+
span: #0 bytes(130..135),
6+
},
7+
Ident {
8+
ident: "r#ident",
9+
span: #0 bytes(151..158),
10+
},
11+
Punct {
12+
ch: ',',
13+
spacing: Alone,
14+
span: #0 bytes(176..177),
15+
},
16+
Punct {
17+
ch: '=',
18+
spacing: Joint,
19+
span: #0 bytes(203..205),
20+
},
21+
Punct {
22+
ch: '=',
23+
spacing: Joint,
24+
span: #0 bytes(203..205),
25+
},
26+
Punct {
27+
ch: '>',
28+
spacing: Alone,
29+
span: #0 bytes(205..206),
30+
},
31+
Group {
32+
delimiter: Parenthesis,
33+
stream: TokenStream [],
34+
span: #0 bytes(230..232),
35+
},
36+
Group {
37+
delimiter: Bracket,
38+
stream: TokenStream [
39+
Ident {
40+
ident: "_",
41+
span: #0 bytes(258..259),
42+
},
43+
],
44+
span: #0 bytes(257..260),
45+
},
46+
Literal {
47+
kind: Integer,
48+
symbol: "0",
49+
suffix: None,
50+
span: #0 bytes(315..316),
51+
},
52+
Literal {
53+
kind: Float,
54+
symbol: "1.0",
55+
suffix: None,
56+
span: #0 bytes(321..324),
57+
},
58+
Literal {
59+
kind: Str,
60+
symbol: "S",
61+
suffix: None,
62+
span: #0 bytes(329..332),
63+
},
64+
Literal {
65+
kind: ByteStr,
66+
symbol: "B",
67+
suffix: None,
68+
span: #0 bytes(337..341),
69+
},
70+
Literal {
71+
kind: StrRaw(0),
72+
symbol: "R",
73+
suffix: None,
74+
span: #0 bytes(346..350),
75+
},
76+
Literal {
77+
kind: StrRaw(2),
78+
symbol: "R",
79+
suffix: None,
80+
span: #0 bytes(355..363),
81+
},
82+
Literal {
83+
kind: ByteStrRaw(0),
84+
symbol: "BR",
85+
suffix: None,
86+
span: #0 bytes(368..374),
87+
},
88+
Literal {
89+
kind: ByteStrRaw(2),
90+
symbol: "BR",
91+
suffix: None,
92+
span: #0 bytes(379..389),
93+
},
94+
Literal {
95+
kind: Char,
96+
symbol: "C",
97+
suffix: None,
98+
span: #0 bytes(394..397),
99+
},
100+
Literal {
101+
kind: Byte,
102+
symbol: "B",
103+
suffix: None,
104+
span: #0 bytes(402..406),
105+
},
106+
Literal {
107+
kind: Integer,
108+
symbol: "0",
109+
suffix: Some("q"),
110+
span: #0 bytes(437..439),
111+
},
112+
Literal {
113+
kind: Float,
114+
symbol: "1.0",
115+
suffix: Some("q"),
116+
span: #0 bytes(444..448),
117+
},
118+
Literal {
119+
kind: Str,
120+
symbol: "S",
121+
suffix: Some("q"),
122+
span: #0 bytes(453..457),
123+
},
124+
Literal {
125+
kind: ByteStr,
126+
symbol: "B",
127+
suffix: Some("q"),
128+
span: #0 bytes(462..467),
129+
},
130+
Literal {
131+
kind: StrRaw(0),
132+
symbol: "R",
133+
suffix: Some("q"),
134+
span: #0 bytes(472..477),
135+
},
136+
Literal {
137+
kind: StrRaw(2),
138+
symbol: "R",
139+
suffix: Some("q"),
140+
span: #0 bytes(482..491),
141+
},
142+
Literal {
143+
kind: ByteStrRaw(0),
144+
symbol: "BR",
145+
suffix: Some("q"),
146+
span: #0 bytes(496..503),
147+
},
148+
Literal {
149+
kind: ByteStrRaw(2),
150+
symbol: "BR",
151+
suffix: Some("q"),
152+
span: #0 bytes(508..519),
153+
},
154+
Literal {
155+
kind: Char,
156+
symbol: "C",
157+
suffix: Some("q"),
158+
span: #0 bytes(524..528),
159+
},
160+
Literal {
161+
kind: Byte,
162+
symbol: "B",
163+
suffix: Some("q"),
164+
span: #0 bytes(533..538),
165+
},
166+
]

0 commit comments

Comments
 (0)