Skip to content

Commit

Permalink
Merge pull request #1655 from dtolnay/constparam
Browse files Browse the repository at this point in the history
Apply const argument legalization to default value of ConstParam
  • Loading branch information
dtolnay committed May 15, 2024
2 parents 1d218f9 + dde4a0f commit 9597ebc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
32 changes: 30 additions & 2 deletions src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -1214,7 +1216,7 @@ 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);
}
}
}
Expand Down Expand Up @@ -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);
}),
}
}
}
35 changes: 5 additions & 30 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,15 +688,14 @@ 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,
};
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;
Expand Down Expand Up @@ -741,40 +740,16 @@ 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),
}
}
}

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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9597ebc

Please sign in to comment.