Skip to content

Commit

Permalink
Add mod-style printing for paths that cannot contain generic args
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Aug 16, 2024
1 parent ae8c84a commit 3bb65aa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ pub(crate) mod parsing {
#[cfg(feature = "printing")]
mod printing {
use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
use crate::path;
use proc_macro2::TokenStream;
use quote::ToTokens;

Expand All @@ -796,25 +797,25 @@ mod printing {
impl ToTokens for Meta {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Meta::Path(meta) => meta.to_tokens(tokens),
Meta::List(meta) => meta.to_tokens(tokens),
Meta::NameValue(meta) => meta.to_tokens(tokens),
Meta::Path(path) => path::printing::print_mod_style(tokens, path),
Meta::List(meta_list) => meta_list.to_tokens(tokens),
Meta::NameValue(meta_name_value) => meta_name_value.to_tokens(tokens),
}
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for MetaList {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.path.to_tokens(tokens);
path::printing::print_mod_style(tokens, &self.path);
self.delimiter.surround(tokens, self.tokens.clone());
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for MetaNameValue {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.path.to_tokens(tokens);
path::printing::print_mod_style(tokens, &self.path);
self.eq_token.to_tokens(tokens);
self.value.to_tokens(tokens);
}
Expand Down
3 changes: 2 additions & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2894,6 +2894,7 @@ mod printing {
UsePath, UseRename, Variadic,
};
use crate::mac::MacroDelimiter;
use crate::path;
use crate::print::TokensOrDefault;
use crate::ty::Type;
use proc_macro2::TokenStream;
Expand Down Expand Up @@ -3135,7 +3136,7 @@ mod printing {
impl ToTokens for ItemMacro {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
self.mac.path.to_tokens(tokens);
path::printing::print_mod_style(tokens, &self.mac.path);
self.mac.bang_token.to_tokens(tokens);
self.ident.to_tokens(tokens);
match &self.mac.delimiter {
Expand Down
3 changes: 2 additions & 1 deletion src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ pub(crate) mod parsing {
#[cfg(feature = "printing")]
mod printing {
use crate::mac::{Macro, MacroDelimiter};
use crate::path;
use crate::token;
use proc_macro2::{Delimiter, TokenStream};
use quote::ToTokens;
Expand All @@ -215,7 +216,7 @@ mod printing {
#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for Macro {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.path.to_tokens(tokens);
path::printing::print_mod_style(tokens, &self.path);
self.bang_token.to_tokens(tokens);
self.delimiter.surround(tokens, self.tokens.clone());
}
Expand Down
8 changes: 8 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,14 @@ pub(crate) mod printing {
}
}

pub(crate) fn print_mod_style(tokens: &mut TokenStream, path: &Path) {
path.leading_colon.to_tokens(tokens);
for segment in path.segments.pairs() {
segment.value().ident.to_tokens(tokens);
segment.punct().to_tokens(tokens);
}
}

#[cfg(feature = "parsing")]
#[cfg_attr(docsrs, doc(cfg(all(feature = "parsing", feature = "printing"))))]
impl Spanned for QSelf {
Expand Down
3 changes: 2 additions & 1 deletion src/restriction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub(crate) mod parsing {

#[cfg(feature = "printing")]
mod printing {
use crate::path;
use crate::restriction::{VisRestricted, Visibility};
use proc_macro2::TokenStream;
use quote::ToTokens;
Expand All @@ -169,7 +170,7 @@ mod printing {
// TODO: If we have a path which is not "self" or "super" or
// "crate", automatically add the "in" token.
self.in_token.to_tokens(tokens);
self.path.to_tokens(tokens);
path::printing::print_mod_style(tokens, &self.path);
});
}
}
Expand Down

0 comments on commit 3bb65aa

Please sign in to comment.