Skip to content

Commit 6c08bb8

Browse files
committed
proc_macro: Improve Debug representations
This commit improves the `fmt::Debug` output of `proc_macro` data structures by primarily focusing on the representation exposed by `proc_macro` rather than the compiler's own internal representation. This cuts down quite a bit on assorted wrapper types and ensure a relatively clean output. Closes rust-lang#49720
1 parent a143462 commit 6c08bb8

File tree

2 files changed

+63
-35
lines changed

2 files changed

+63
-35
lines changed

src/libproc_macro/lib.rs

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ impl fmt::Display for TokenStream {
127127
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
128128
impl fmt::Debug for TokenStream {
129129
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130-
self.0.fmt(f)
130+
f.write_str("TokenStream ")?;
131+
f.debug_list().entries(self.clone()).finish()
131132
}
132133
}
133134

@@ -222,7 +223,7 @@ pub fn quote_span(span: Span) -> TokenStream {
222223

223224
/// A region of source code, along with macro expansion information.
224225
#[unstable(feature = "proc_macro", issue = "38356")]
225-
#[derive(Copy, Clone, Debug)]
226+
#[derive(Copy, Clone)]
226227
pub struct Span(syntax_pos::Span);
227228

228229
macro_rules! diagnostic_method {
@@ -334,6 +335,16 @@ impl Span {
334335
diagnostic_method!(help, Level::Help);
335336
}
336337

338+
#[unstable(feature = "proc_macro", issue = "38356")]
339+
impl fmt::Debug for Span {
340+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
341+
write!(f, "{:?} bytes({}...{})",
342+
self.0.ctxt(),
343+
self.0.lo().0,
344+
self.0.hi().0)
345+
}
346+
}
347+
337348
/// A line-column pair representing the start or end of a `Span`.
338349
#[unstable(feature = "proc_macro", issue = "38356")]
339350
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -422,7 +433,7 @@ impl PartialEq<FileName> for SourceFile {
422433

423434
/// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`).
424435
#[unstable(feature = "proc_macro", issue = "38356")]
425-
#[derive(Clone, Debug)]
436+
#[derive(Clone)]
426437
pub enum TokenTree {
427438
/// A delimited tokenstream
428439
Group(Group),
@@ -463,6 +474,20 @@ impl TokenTree {
463474
}
464475
}
465476

477+
#[unstable(feature = "proc_macro", issue = "38356")]
478+
impl fmt::Debug for TokenTree {
479+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
480+
// Each of these has the name in the struct type in the derived debug,
481+
// so don't bother with an extra layer of indirection
482+
match *self {
483+
TokenTree::Group(ref tt) => tt.fmt(f),
484+
TokenTree::Term(ref tt) => tt.fmt(f),
485+
TokenTree::Op(ref tt) => tt.fmt(f),
486+
TokenTree::Literal(ref tt) => tt.fmt(f),
487+
}
488+
}
489+
}
490+
466491
#[unstable(feature = "proc_macro", issue = "38356")]
467492
impl From<Group> for TokenTree {
468493
fn from(g: Group) -> TokenTree {
@@ -717,7 +742,8 @@ impl fmt::Display for Term {
717742
#[derive(Clone, Debug)]
718743
#[unstable(feature = "proc_macro", issue = "38356")]
719744
pub struct Literal {
720-
token: token::Token,
745+
lit: token::Lit,
746+
suffix: Option<ast::Name>,
721747
span: Span,
722748
}
723749

@@ -734,10 +760,9 @@ macro_rules! suffixed_int_literals {
734760
/// below.
735761
#[unstable(feature = "proc_macro", issue = "38356")]
736762
pub fn $name(n: $kind) -> Literal {
737-
let lit = token::Lit::Integer(Symbol::intern(&n.to_string()));
738-
let ty = Some(Symbol::intern(stringify!($kind)));
739763
Literal {
740-
token: token::Literal(lit, ty),
764+
lit: token::Lit::Integer(Symbol::intern(&n.to_string())),
765+
suffix: Some(Symbol::intern(stringify!($kind))),
741766
span: Span::call_site(),
742767
}
743768
}
@@ -759,9 +784,9 @@ macro_rules! unsuffixed_int_literals {
759784
/// below.
760785
#[unstable(feature = "proc_macro", issue = "38356")]
761786
pub fn $name(n: $kind) -> Literal {
762-
let lit = token::Lit::Integer(Symbol::intern(&n.to_string()));
763787
Literal {
764-
token: token::Literal(lit, None),
788+
lit: token::Lit::Integer(Symbol::intern(&n.to_string())),
789+
suffix: None,
765790
span: Span::call_site(),
766791
}
767792
}
@@ -814,9 +839,9 @@ impl Literal {
814839
if !n.is_finite() {
815840
panic!("Invalid float literal {}", n);
816841
}
817-
let lit = token::Lit::Float(Symbol::intern(&n.to_string()));
818842
Literal {
819-
token: token::Literal(lit, None),
843+
lit: token::Lit::Float(Symbol::intern(&n.to_string())),
844+
suffix: None,
820845
span: Span::call_site(),
821846
}
822847
}
@@ -837,9 +862,9 @@ impl Literal {
837862
if !n.is_finite() {
838863
panic!("Invalid float literal {}", n);
839864
}
840-
let lit = token::Lit::Float(Symbol::intern(&n.to_string()));
841865
Literal {
842-
token: token::Literal(lit, Some(Symbol::intern("f32"))),
866+
lit: token::Lit::Float(Symbol::intern(&n.to_string())),
867+
suffix: Some(Symbol::intern("f32")),
843868
span: Span::call_site(),
844869
}
845870
}
@@ -859,9 +884,9 @@ impl Literal {
859884
if !n.is_finite() {
860885
panic!("Invalid float literal {}", n);
861886
}
862-
let lit = token::Lit::Float(Symbol::intern(&n.to_string()));
863887
Literal {
864-
token: token::Literal(lit, None),
888+
lit: token::Lit::Float(Symbol::intern(&n.to_string())),
889+
suffix: None,
865890
span: Span::call_site(),
866891
}
867892
}
@@ -882,9 +907,9 @@ impl Literal {
882907
if !n.is_finite() {
883908
panic!("Invalid float literal {}", n);
884909
}
885-
let lit = token::Lit::Float(Symbol::intern(&n.to_string()));
886910
Literal {
887-
token: token::Literal(lit, Some(Symbol::intern("f64"))),
911+
lit: token::Lit::Float(Symbol::intern(&n.to_string())),
912+
suffix: Some(Symbol::intern("f64")),
888913
span: Span::call_site(),
889914
}
890915
}
@@ -897,7 +922,8 @@ impl Literal {
897922
escaped.extend(ch.escape_debug());
898923
}
899924
Literal {
900-
token: token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None),
925+
lit: token::Lit::Str_(Symbol::intern(&escaped)),
926+
suffix: None,
901927
span: Span::call_site(),
902928
}
903929
}
@@ -908,7 +934,8 @@ impl Literal {
908934
let mut escaped = String::new();
909935
escaped.extend(ch.escape_unicode());
910936
Literal {
911-
token: token::Literal(token::Lit::Char(Symbol::intern(&escaped)), None),
937+
lit: token::Lit::Char(Symbol::intern(&escaped)),
938+
suffix: None,
912939
span: Span::call_site(),
913940
}
914941
}
@@ -919,7 +946,8 @@ impl Literal {
919946
let string = bytes.iter().cloned().flat_map(ascii::escape_default)
920947
.map(Into::<char>::into).collect::<String>();
921948
Literal {
922-
token: token::Literal(token::Lit::ByteStr(Symbol::intern(&string)), None),
949+
lit: token::Lit::ByteStr(Symbol::intern(&string)),
950+
suffix: None,
923951
span: Span::call_site(),
924952
}
925953
}
@@ -1055,7 +1083,7 @@ impl TokenTree {
10551083
Ident(ident, true) => {
10561084
tt!(Term::new(&format!("r#{}", ident), Span(span)))
10571085
}
1058-
Literal(..) => tt!(self::Literal { token, span: Span(span) }),
1086+
Literal(lit, suffix) => tt!(self::Literal { lit, suffix, span: Span(span) }),
10591087
DocComment(c) => {
10601088
let style = comments::doc_comment_style(&c.as_str());
10611089
let stripped = comments::strip_doc_comment_decoration(&c.as_str());
@@ -1111,33 +1139,36 @@ impl TokenTree {
11111139
return TokenTree::Token(tt.span.0, token).into();
11121140
}
11131141
self::TokenTree::Literal(self::Literal {
1114-
token: Literal(Lit::Integer(ref a), b),
1142+
lit: Lit::Integer(ref a),
1143+
suffix,
11151144
span,
11161145
})
11171146
if a.as_str().starts_with("-") =>
11181147
{
11191148
let minus = BinOp(BinOpToken::Minus);
11201149
let integer = Symbol::intern(&a.as_str()[1..]);
1121-
let integer = Literal(Lit::Integer(integer), b);
1150+
let integer = Literal(Lit::Integer(integer), suffix);
11221151
let a = TokenTree::Token(span.0, minus);
11231152
let b = TokenTree::Token(span.0, integer);
11241153
return vec![a, b].into_iter().collect()
11251154
}
11261155
self::TokenTree::Literal(self::Literal {
1127-
token: Literal(Lit::Float(ref a), b),
1156+
lit: Lit::Float(ref a),
1157+
suffix,
11281158
span,
11291159
})
11301160
if a.as_str().starts_with("-") =>
11311161
{
11321162
let minus = BinOp(BinOpToken::Minus);
11331163
let float = Symbol::intern(&a.as_str()[1..]);
1134-
let float = Literal(Lit::Float(float), b);
1164+
let float = Literal(Lit::Float(float), suffix);
11351165
let a = TokenTree::Token(span.0, minus);
11361166
let b = TokenTree::Token(span.0, float);
11371167
return vec![a, b].into_iter().collect()
11381168
}
11391169
self::TokenTree::Literal(tt) => {
1140-
return TokenTree::Token(tt.span.0, tt.token).into()
1170+
let token = Literal(tt.lit, tt.suffix);
1171+
return TokenTree::Token(tt.span.0, token).into()
11411172
}
11421173
};
11431174

src/libproc_macro/quote.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,15 @@ macro_rules! literals {
208208
match self {
209209
$(LiteralKind::$i => {
210210
Literal {
211-
token: token::Literal(token::Lit::$i(sym), suffix),
211+
lit: token::Lit::$i(sym),
212+
suffix,
212213
span: contents.span,
213214
}
214215
})*
215216
$(LiteralKind::$raw(n) => {
216217
Literal {
217-
token: token::Literal(token::Lit::$raw(sym, n), suffix),
218+
lit: token::Lit::$raw(sym, n),
219+
suffix,
218220
span: contents.span,
219221
}
220222
})*
@@ -224,16 +226,11 @@ macro_rules! literals {
224226

225227
impl Literal {
226228
fn kind_contents_and_suffix(self) -> (LiteralKind, Term, Option<Term>) {
227-
let (lit, suffix) = match self.token {
228-
token::Literal(lit, suffix) => (lit, suffix),
229-
_ => panic!("unsupported literal {:?}", self.token),
230-
};
231-
232-
let (kind, contents) = match lit {
229+
let (kind, contents) = match self.lit {
233230
$(token::Lit::$i(contents) => (LiteralKind::$i, contents),)*
234231
$(token::Lit::$raw(contents, n) => (LiteralKind::$raw(n), contents),)*
235232
};
236-
let suffix = suffix.map(|sym| Term::new(&sym.as_str(), self.span()));
233+
let suffix = self.suffix.map(|sym| Term::new(&sym.as_str(), self.span()));
237234
(kind, Term::new(&contents.as_str(), self.span()), suffix)
238235
}
239236
}

0 commit comments

Comments
 (0)