Skip to content

Commit 7b052ae

Browse files
authored
Merge pull request #79 from alexcrichton/debug
Improve Debug representations
2 parents 861af7d + 034205f commit 7b052ae

File tree

3 files changed

+126
-7
lines changed

3 files changed

+126
-7
lines changed

src/lib.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl fmt::Debug for Span {
251251
}
252252
}
253253

254-
#[derive(Clone, Debug)]
254+
#[derive(Clone)]
255255
pub enum TokenTree {
256256
Group(Group),
257257
Term(Term),
@@ -314,7 +314,20 @@ impl fmt::Display for TokenTree {
314314
}
315315
}
316316

317-
#[derive(Clone, Debug)]
317+
impl fmt::Debug for TokenTree {
318+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
319+
// Each of these has the name in the struct type in the derived debug,
320+
// so don't bother with an extra layer of indirection
321+
match *self {
322+
TokenTree::Group(ref t) => t.fmt(f),
323+
TokenTree::Term(ref t) => t.fmt(f),
324+
TokenTree::Op(ref t) => t.fmt(f),
325+
TokenTree::Literal(ref t) => t.fmt(f),
326+
}
327+
}
328+
}
329+
330+
#[derive(Clone)]
318331
pub struct Group {
319332
delimiter: Delimiter,
320333
stream: TokenStream,
@@ -361,7 +374,18 @@ impl fmt::Display for Group {
361374
}
362375
}
363376

364-
#[derive(Copy, Clone, Debug)]
377+
impl fmt::Debug for Group {
378+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
379+
let mut debug = fmt.debug_struct("Group");
380+
debug.field("delimiter", &self.delimiter);
381+
debug.field("stream", &self.stream);
382+
#[cfg(procmacro2_semver_exempt)]
383+
debug.field("span", &self.span);
384+
debug.finish()
385+
}
386+
}
387+
388+
#[derive(Copy, Clone)]
365389
pub struct Op {
366390
op: char,
367391
spacing: Spacing,
@@ -406,6 +430,17 @@ impl fmt::Display for Op {
406430
}
407431
}
408432

433+
impl fmt::Debug for Op {
434+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
435+
let mut debug = fmt.debug_struct("Op");
436+
debug.field("op", &self.op);
437+
debug.field("spacing", &self.spacing);
438+
#[cfg(procmacro2_semver_exempt)]
439+
debug.field("span", &self.span);
440+
debug.finish()
441+
}
442+
}
443+
409444
#[derive(Copy, Clone)]
410445
pub struct Term {
411446
inner: imp::Term,

src/stable.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use unicode_xid::UnicodeXID;
1616

1717
use {Delimiter, Group, Op, Spacing, TokenTree};
1818

19-
#[derive(Clone, Debug)]
19+
#[derive(Clone)]
2020
pub struct TokenStream {
2121
inner: Vec<TokenTree>,
2222
}
@@ -111,6 +111,13 @@ impl fmt::Display for TokenStream {
111111
}
112112
}
113113

114+
impl fmt::Debug for TokenStream {
115+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116+
f.write_str("TokenStream ")?;
117+
f.debug_list().entries(self.clone()).finish()
118+
}
119+
}
120+
114121
#[cfg(feature = "proc-macro")]
115122
impl From<::proc_macro::TokenStream> for TokenStream {
116123
fn from(inner: ::proc_macro::TokenStream) -> TokenStream {
@@ -314,7 +321,7 @@ impl Codemap {
314321
}
315322
}
316323

317-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
324+
#[derive(Clone, Copy, PartialEq, Eq)]
318325
pub struct Span {
319326
#[cfg(procmacro2_semver_exempt)]
320327
lo: u32,
@@ -393,6 +400,16 @@ impl Span {
393400
}
394401
}
395402

403+
impl fmt::Debug for Span {
404+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
405+
#[cfg(procmacro2_semver_exempt)]
406+
return write!(f, "bytes({}..{})", self.lo, self.hi);
407+
408+
#[cfg(not(procmacro2_semver_exempt))]
409+
write!(f, "Span")
410+
}
411+
}
412+
396413
#[derive(Copy, Clone)]
397414
pub struct Term {
398415
intern: usize,
@@ -466,7 +483,11 @@ fn validate_term(string: &str) {
466483

467484
impl fmt::Debug for Term {
468485
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
469-
f.debug_tuple("Term").field(&self.as_str()).finish()
486+
let mut debug = f.debug_struct("Term");
487+
debug.field("sym", &format_args!("{}", self.as_str()));
488+
#[cfg(procmacro2_semver_exempt)]
489+
debug.field("span", &self.span);
490+
debug.finish()
470491
}
471492
}
472493

@@ -508,7 +529,7 @@ impl Interner {
508529
}
509530
}
510531

511-
#[derive(Clone, Debug)]
532+
#[derive(Clone)]
512533
pub struct Literal {
513534
text: String,
514535
span: Span,
@@ -629,6 +650,16 @@ impl fmt::Display for Literal {
629650
}
630651
}
631652

653+
impl fmt::Debug for Literal {
654+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
655+
let mut debug = fmt.debug_struct("Literal");
656+
debug.field("lit", &format_args!("{}", self.text));
657+
#[cfg(procmacro2_semver_exempt)]
658+
debug.field("span", &self.span);
659+
debug.finish()
660+
}
661+
}
662+
632663
fn token_stream(mut input: Cursor) -> PResult<::TokenStream> {
633664
let mut trees = Vec::new();
634665
loop {

tests/test.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,56 @@ fn raw_identifier() {
314314
}
315315
assert!(tts.next().is_none());
316316
}
317+
318+
#[test]
319+
fn test_debug() {
320+
let tts = TokenStream::from_str("[a + 1]").unwrap();
321+
322+
#[cfg(not(procmacro2_semver_exempt))]
323+
let expected = "\
324+
TokenStream [
325+
Group {
326+
delimiter: Bracket,
327+
stream: TokenStream [
328+
Term {
329+
sym: a
330+
},
331+
Op {
332+
op: '+',
333+
spacing: Alone
334+
},
335+
Literal {
336+
lit: 1
337+
}
338+
]
339+
}
340+
]\
341+
";
342+
343+
#[cfg(procmacro2_semver_exempt)]
344+
let expected = "\
345+
TokenStream [
346+
Group {
347+
delimiter: Bracket,
348+
stream: TokenStream [
349+
Term {
350+
sym: a,
351+
span: bytes(2..3)
352+
},
353+
Op {
354+
op: '+',
355+
spacing: Alone,
356+
span: bytes(4..5)
357+
},
358+
Literal {
359+
lit: 1,
360+
span: bytes(6..7)
361+
}
362+
],
363+
span: bytes(1..8)
364+
}
365+
]\
366+
";
367+
368+
assert_eq!(expected, format!("{:#?}", tts));
369+
}

0 commit comments

Comments
 (0)