From 125d3a0cc1525e89e93259c8d56feb748179073f Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 15 May 2024 12:33:57 -0700 Subject: [PATCH 1/2] Relocate print_const_argument to generics::printing module --- src/generics.rs | 30 +++++++++++++++++++++++++++++- src/path.rs | 35 +++++------------------------------ 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/generics.rs b/src/generics.rs index a3cd5e6298..618204f38f 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -989,14 +989,16 @@ pub(crate) mod parsing { } #[cfg(feature = "printing")] -mod printing { +pub(crate) mod printing { use crate::attr::FilterAttrs; + use crate::expr::Expr; use crate::generics::{ BoundLifetimes, ConstParam, GenericParam, Generics, ImplGenerics, LifetimeParam, PredicateLifetime, PredicateType, TraitBound, TraitBoundModifier, Turbofish, TypeGenerics, TypeParam, WhereClause, }; use crate::print::TokensOrDefault; + use crate::token; use proc_macro2::TokenStream; use quote::{ToTokens, TokenStreamExt}; @@ -1247,4 +1249,30 @@ mod printing { self.bounds.to_tokens(tokens); } } + + pub(crate) fn print_const_argument(expr: &Expr, tokens: &mut TokenStream) { + match expr { + Expr::Lit(expr) => expr.to_tokens(tokens), + + Expr::Path(expr) + if expr.attrs.is_empty() + && expr.qself.is_none() + && expr.path.get_ident().is_some() => + { + expr.to_tokens(tokens); + } + + #[cfg(feature = "full")] + Expr::Block(expr) => expr.to_tokens(tokens), + + #[cfg(not(feature = "full"))] + Expr::Verbatim(expr) => expr.to_tokens(tokens), + + // ERROR CORRECTION: Add braces to make sure that the + // generated code is valid. + _ => token::Brace::default().surround(tokens, |tokens| { + expr.to_tokens(tokens); + }), + } + } } diff --git a/src/path.rs b/src/path.rs index 14781c310b..191c47025b 100644 --- a/src/path.rs +++ b/src/path.rs @@ -688,7 +688,7 @@ pub(crate) mod parsing { #[cfg(feature = "printing")] pub(crate) mod printing { - use crate::expr::Expr; + use crate::generics; use crate::path::{ AngleBracketedGenericArguments, AssocConst, AssocType, Constraint, GenericArgument, ParenthesizedGenericArguments, Path, PathArguments, PathSegment, QSelf, @@ -696,7 +696,6 @@ pub(crate) mod printing { use crate::print::TokensOrDefault; #[cfg(feature = "parsing")] use crate::spanned::Spanned; - use crate::token; #[cfg(feature = "parsing")] use proc_macro2::Span; use proc_macro2::TokenStream; @@ -741,7 +740,9 @@ pub(crate) mod printing { match self { GenericArgument::Lifetime(lt) => lt.to_tokens(tokens), GenericArgument::Type(ty) => ty.to_tokens(tokens), - GenericArgument::Const(expr) => print_const_argument(expr, tokens), + GenericArgument::Const(expr) => { + generics::printing::print_const_argument(expr, tokens); + } GenericArgument::AssocType(assoc) => assoc.to_tokens(tokens), GenericArgument::AssocConst(assoc) => assoc.to_tokens(tokens), GenericArgument::Constraint(constraint) => constraint.to_tokens(tokens), @@ -749,32 +750,6 @@ pub(crate) mod printing { } } - fn print_const_argument(expr: &Expr, tokens: &mut TokenStream) { - match expr { - Expr::Lit(expr) => expr.to_tokens(tokens), - - Expr::Path(expr) - if expr.attrs.is_empty() - && expr.qself.is_none() - && expr.path.get_ident().is_some() => - { - expr.to_tokens(tokens); - } - - #[cfg(feature = "full")] - Expr::Block(expr) => expr.to_tokens(tokens), - - #[cfg(not(feature = "full"))] - Expr::Verbatim(expr) => expr.to_tokens(tokens), - - // ERROR CORRECTION: Add braces to make sure that the - // generated code is valid. - _ => token::Brace::default().surround(tokens, |tokens| { - expr.to_tokens(tokens); - }), - } - } - #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))] impl ToTokens for AngleBracketedGenericArguments { fn to_tokens(&self, tokens: &mut TokenStream) { @@ -834,7 +809,7 @@ pub(crate) mod printing { self.ident.to_tokens(tokens); self.generics.to_tokens(tokens); self.eq_token.to_tokens(tokens); - print_const_argument(&self.value, tokens); + generics::printing::print_const_argument(&self.value, tokens); } } From dde4a0f6a95ed7e20cd8981682b80d0dcca04f92 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 15 May 2024 12:35:14 -0700 Subject: [PATCH 2/2] Apply const argument legalization to default value of ConstParam --- src/generics.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/generics.rs b/src/generics.rs index 618204f38f..4440ce8b85 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -1216,7 +1216,7 @@ pub(crate) mod printing { self.ty.to_tokens(tokens); if let Some(default) = &self.default { TokensOrDefault(&self.eq_token).to_tokens(tokens); - default.to_tokens(tokens); + print_const_argument(default, tokens); } } }