Skip to content

Commit 99eac01

Browse files
committed
proc_macro: avoid exposing internal details in formatting impls.
1 parent bc2b21c commit 99eac01

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

src/libproc_macro/lib.rs

+37-7
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl fmt::Display for TokenTree {
588588
/// A delimited token stream.
589589
///
590590
/// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
591-
#[derive(Clone, Debug)]
591+
#[derive(Clone)]
592592
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
593593
pub struct Group {
594594
delimiter: Delimiter,
@@ -682,12 +682,23 @@ impl fmt::Display for Group {
682682
}
683683
}
684684

685+
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
686+
impl fmt::Debug for Group {
687+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
688+
f.debug_struct("Group")
689+
.field("delimiter", &self.delimiter())
690+
.field("stream", &self.stream())
691+
.field("span", &self.span())
692+
.finish()
693+
}
694+
}
695+
685696
/// An `Punct` is an single punctuation character like `+`, `-` or `#`.
686697
///
687698
/// Multicharacter operators like `+=` are represented as two instances of `Punct` with different
688699
/// forms of `Spacing` returned.
689700
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
690-
#[derive(Clone, Debug)]
701+
#[derive(Clone)]
691702
pub struct Punct {
692703
ch: char,
693704
spacing: Spacing,
@@ -771,8 +782,19 @@ impl fmt::Display for Punct {
771782
}
772783
}
773784

785+
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
786+
impl fmt::Debug for Punct {
787+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
788+
f.debug_struct("Punct")
789+
.field("ch", &self.as_char())
790+
.field("spacing", &self.spacing())
791+
.field("span", &self.span())
792+
.finish()
793+
}
794+
}
795+
774796
/// An identifier (`ident`).
775-
#[derive(Clone, Debug)]
797+
#[derive(Clone)]
776798
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
777799
pub struct Ident {
778800
sym: Symbol,
@@ -851,17 +873,25 @@ impl Ident {
851873
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
852874
impl fmt::Display for Ident {
853875
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
854-
if self.is_raw {
855-
f.write_str("r#")?;
856-
}
857-
self.sym.as_str().fmt(f)
876+
TokenStream::from(TokenTree::from(self.clone())).fmt(f)
877+
}
878+
}
879+
880+
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
881+
impl fmt::Debug for Ident {
882+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
883+
f.debug_struct("Ident")
884+
.field("ident", &self.to_string())
885+
.field("span", &self.span())
886+
.finish()
858887
}
859888
}
860889

861890
/// A literal string (`"hello"`), byte string (`b"hello"`),
862891
/// character (`'a'`), byte character (`b'a'`), an integer or floating point number
863892
/// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
864893
/// Boolean literals like `true` and `false` do not belong here, they are `Ident`s.
894+
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
865895
#[derive(Clone, Debug)]
866896
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
867897
pub struct Literal {

0 commit comments

Comments
 (0)