Skip to content

Commit

Permalink
Merge pull request #1569 from dtolnay/cfgvisit
Browse files Browse the repository at this point in the history
Render doc cfg on Visit/VisitMut/Fold trait methods
  • Loading branch information
dtolnay authored Jan 1, 2024
2 parents 1728630 + 694a1bb commit 4523437
Show file tree
Hide file tree
Showing 9 changed files with 1,053 additions and 17 deletions.
27 changes: 22 additions & 5 deletions codegen/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn_codegen::Features;

pub fn features(features: &Features) -> TokenStream {
pub fn features<'a>(
features: &Features,
overriding_cfg: impl Into<Option<&'a str>>,
) -> TokenStream {
let features = &features.any;
match features.len() {
0 => quote!(),
1 => quote!(#[cfg(feature = #(#features)*)]),
_ => quote!(#[cfg(any(#(feature = #features),*))]),
let cfg = match features.len() {
0 => None,
1 => Some(quote! { cfg(feature = #(#features)*) }),
_ => Some(quote! { cfg(any(#(feature = #features),*)) }),
};
match (cfg, overriding_cfg.into()) {
(Some(cfg), Some(overriding_cfg)) => quote! {
#[#cfg]
#[cfg_attr(doc_cfg, doc(cfg(feature = #overriding_cfg)))]
},
(Some(cfg), None) => quote! {
#[#cfg]
#[cfg_attr(doc_cfg, doc(#cfg))]
},
(None, Some(overriding_cfg)) => quote! {
#[cfg_attr(doc_cfg, doc(cfg(feature = #overriding_cfg)))]
},
(None, None) => TokenStream::new(),
}
}
5 changes: 1 addition & 4 deletions codegen/src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
}

let ident = Ident::new(&node.ident, Span::call_site());
let cfg_features = cfg::features(&node.features);
let cfg_features = cfg::features(&node.features, "clone-impls");

let copy = node.ident == "AttrStyle"
|| node.ident == "BinOp"
Expand All @@ -86,10 +86,8 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
if copy {
return quote! {
#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "clone-impls")))]
impl Copy for #ident {}
#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "clone-impls")))]
impl Clone for #ident {
fn clone(&self) -> Self {
*self
Expand All @@ -102,7 +100,6 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {

quote! {
#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "clone-impls")))]
impl Clone for #ident {
fn clone(&self) -> Self {
#body
Expand Down
3 changes: 1 addition & 2 deletions codegen/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ fn expand_impl(defs: &Definitions, node: &Node, syntax_tree_variants: &Set<&str>
}

let ident = Ident::new(&node.ident, Span::call_site());
let cfg_features = cfg::features(&node.features);
let cfg_features = cfg::features(&node.features, "extra-traits");
let body = expand_impl_body(defs, node, syntax_tree_variants);
let formatter = match &node.data {
Data::Enum(variants) if variants.is_empty() => quote!(_formatter),
Expand All @@ -156,7 +156,6 @@ fn expand_impl(defs: &Definitions, node: &Node, syntax_tree_variants: &Set<&str>

quote! {
#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
impl Debug for #ident {
fn fmt(&self, #formatter: &mut fmt::Formatter) -> fmt::Result {
#body
Expand Down
4 changes: 1 addition & 3 deletions codegen/src/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
}

let ident = Ident::new(&node.ident, Span::call_site());
let cfg_features = cfg::features(&node.features);
let cfg_features = cfg::features(&node.features, "extra-traits");

let eq = quote! {
#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
impl Eq for #ident {}
};

Expand All @@ -138,7 +137,6 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
#eq

#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
impl PartialEq for #ident {
fn eq(&self, #other: &Self) -> bool {
#body
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn traverse(
let mut traits = TokenStream::new();
let mut impls = TokenStream::new();
for s in types {
let features = cfg::features(&s.features);
let features = cfg::features(&s.features, None);
traits.extend(features.clone());
impls.extend(features);
node(&mut traits, &mut impls, &s, defs);
Expand Down
3 changes: 1 addition & 2 deletions codegen/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {
}

let ident = Ident::new(&node.ident, Span::call_site());
let cfg_features = cfg::features(&node.features);
let cfg_features = cfg::features(&node.features, "extra-traits");

let body = expand_impl_body(defs, node);

Expand All @@ -135,7 +135,6 @@ fn expand_impl(defs: &Definitions, node: &Node) -> TokenStream {

quote! {
#cfg_features
#[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))]
impl Hash for #ident {
fn hash<H>(&self, #hasher: &mut H)
where
Expand Down
Loading

0 comments on commit 4523437

Please sign in to comment.