Skip to content

Commit 45ee336

Browse files
committed
Fix {:#?} representation of proc_macro::Literal
Before: 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: 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), }, ]
1 parent 85f0da6 commit 45ee336

File tree

5 files changed

+19
-9
lines changed

5 files changed

+19
-9
lines changed

src/libproc_macro/bridge/client.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,15 @@ 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+
.field("kind", &format_args!("{}", &self.debug_kind()))
209+
.field("symbol", &self.symbol())
210+
// format `Some("...")` on one line even in {:#?} mode
211+
.field("suffix", &format_args!("{:?}", &self.suffix()))
212+
.field("span", &self.span())
213+
.finish()
209214
}
210215
}
211216

src/libproc_macro/bridge/mod.rs

Lines changed: 3 additions & 2 deletions
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;

src/libproc_macro/lib.rs

Lines changed: 0 additions & 1 deletion
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
}

src/librustc_expand/proc_macro_server.rs

Lines changed: 8 additions & 3 deletions
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)

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

Lines changed: 1 addition & 1 deletion
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
|

0 commit comments

Comments
 (0)