From 3bb65aaae153130c3053bc691080aad9eb60a55c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Thu, 15 Aug 2024 18:21:10 -0700 Subject: [PATCH] Add mod-style printing for paths that cannot contain generic args --- src/attr.rs | 11 ++++++----- src/item.rs | 3 ++- src/mac.rs | 3 ++- src/path.rs | 8 ++++++++ src/restriction.rs | 3 ++- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/attr.rs b/src/attr.rs index bd80f3f051..12290d33aa 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -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; @@ -796,9 +797,9 @@ 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), } } } @@ -806,7 +807,7 @@ mod printing { #[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()); } } @@ -814,7 +815,7 @@ mod printing { #[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); } diff --git a/src/item.rs b/src/item.rs index fa87b42b96..f5472aec7b 100644 --- a/src/item.rs +++ b/src/item.rs @@ -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; @@ -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 { diff --git a/src/mac.rs b/src/mac.rs index 7e1876c648..f64bdfb59c 100644 --- a/src/mac.rs +++ b/src/mac.rs @@ -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; @@ -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()); } diff --git a/src/path.rs b/src/path.rs index 636d5d5e8f..0cc9e762db 100644 --- a/src/path.rs +++ b/src/path.rs @@ -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 { diff --git a/src/restriction.rs b/src/restriction.rs index 8a4d4706a5..224455d0f3 100644 --- a/src/restriction.rs +++ b/src/restriction.rs @@ -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; @@ -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); }); } }