From f1daf234e942ffda27ed45ec67aa6cd984081c2a Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 7 Jul 2024 17:56:56 -0700 Subject: [PATCH] Enable expression fixups in 'derive' mode --- src/classify.rs | 14 ++- src/expr.rs | 223 ++++++++++++------------------------------------ src/fixup.rs | 38 ++++++++- src/lib.rs | 7 +- 4 files changed, 108 insertions(+), 174 deletions(-) diff --git a/src/classify.rs b/src/classify.rs index ab735f142..42732e6d8 100644 --- a/src/classify.rs +++ b/src/classify.rs @@ -1,11 +1,19 @@ +#[cfg(feature = "full")] use crate::expr::Expr; +#[cfg(any(feature = "printing", feature = "full"))] use crate::generics::TypeParamBound; +#[cfg(any(feature = "printing", feature = "full"))] use crate::path::{Path, PathArguments}; +#[cfg(any(feature = "printing", feature = "full"))] use crate::punctuated::Punctuated; +#[cfg(any(feature = "printing", feature = "full"))] use crate::ty::{ReturnType, Type}; +#[cfg(feature = "full")] use proc_macro2::{Delimiter, TokenStream, TokenTree}; +#[cfg(any(feature = "printing", feature = "full"))] use std::ops::ControlFlow; +#[cfg(feature = "full")] pub(crate) fn requires_semi_to_be_stmt(expr: &Expr) -> bool { match expr { Expr::Macro(expr) => !expr.mac.delimiter.is_brace(), @@ -13,6 +21,7 @@ pub(crate) fn requires_semi_to_be_stmt(expr: &Expr) -> bool { } } +#[cfg(feature = "full")] pub(crate) fn requires_comma_to_be_match_arm(expr: &Expr) -> bool { match expr { Expr::If(_) @@ -57,7 +66,7 @@ pub(crate) fn requires_comma_to_be_match_arm(expr: &Expr) -> bool { } } -#[cfg(feature = "printing")] +#[cfg(all(feature = "printing", feature = "full"))] pub(crate) fn confusable_with_adjacent_block(mut expr: &Expr) -> bool { let mut stack = Vec::new(); @@ -199,7 +208,7 @@ pub(crate) fn trailing_unparameterized_path(mut ty: &Type) -> bool { } /// Whether the expression's first token is the label of a loop/block. -#[cfg(feature = "printing")] +#[cfg(all(feature = "printing", feature = "full"))] pub(crate) fn expr_leading_label(mut expr: &Expr) -> bool { loop { match expr { @@ -252,6 +261,7 @@ pub(crate) fn expr_leading_label(mut expr: &Expr) -> bool { } /// Whether the expression's last token is `}`. +#[cfg(feature = "full")] pub(crate) fn expr_trailing_brace(mut expr: &Expr) -> bool { loop { match expr { diff --git a/src/expr.rs b/src/expr.rs index c130fa928..e4a7b0e1c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2995,9 +2995,7 @@ pub(crate) mod printing { ExprMethodCall, ExprParen, ExprPath, ExprReference, ExprStruct, ExprUnary, FieldValue, Index, Member, }; - #[cfg(feature = "full")] use crate::fixup::FixupContext; - #[cfg(feature = "full")] use crate::op::BinOp; use crate::path; use crate::precedence::Precedence; @@ -3034,13 +3032,9 @@ pub(crate) mod printing { expr: &Expr, needs_group: bool, tokens: &mut TokenStream, - #[cfg(feature = "full")] mut fixup: FixupContext, + mut fixup: FixupContext, ) { - #[cfg(not(feature = "full"))] - let do_print_expr = |tokens: &mut TokenStream| expr.to_tokens(tokens); - - #[cfg(feature = "full")] - let do_print_expr = { + if needs_group { // If we are surrounding the whole cond in parentheses, such as: // // if (return Struct {}) {} @@ -3052,11 +3046,10 @@ pub(crate) mod printing { // // if x == (Struct {}) {} // - if needs_group { - fixup = FixupContext::NONE; - } - |tokens: &mut TokenStream| print_expr(expr, tokens, fixup) - }; + fixup = FixupContext::NONE; + } + + let do_print_expr = |tokens: &mut TokenStream| print_expr(expr, tokens, fixup); if needs_group { token::Paren::default().surround(tokens, do_print_expr); @@ -3065,20 +3058,26 @@ pub(crate) mod printing { } } - #[cfg(feature = "full")] pub(crate) fn print_expr(expr: &Expr, tokens: &mut TokenStream, mut fixup: FixupContext) { + #[cfg(feature = "full")] let needs_group = fixup.would_cause_statement_boundary(expr); + #[cfg(not(feature = "full"))] + let needs_group = false; + if needs_group { fixup = FixupContext::NONE; } let do_print_expr = |tokens: &mut TokenStream| match expr { Expr::Array(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Assign(e) => print_expr_assign(e, tokens, fixup), Expr::Async(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Await(e) => print_expr_await(e, tokens, fixup), Expr::Binary(e) => print_expr_binary(e, tokens, fixup), Expr::Block(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Break(e) => print_expr_break(e, tokens, fixup), Expr::Call(e) => print_expr_call(e, tokens, fixup), Expr::Cast(e) => print_expr_cast(e, tokens, fixup), @@ -3089,8 +3088,10 @@ pub(crate) mod printing { Expr::ForLoop(e) => e.to_tokens(tokens), Expr::Group(e) => e.to_tokens(tokens), Expr::If(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Index(e) => print_expr_index(e, tokens, fixup), Expr::Infer(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Let(e) => print_expr_let(e, tokens, fixup), Expr::Lit(e) => e.to_tokens(tokens), Expr::Loop(e) => e.to_tokens(tokens), @@ -3099,11 +3100,14 @@ pub(crate) mod printing { Expr::MethodCall(e) => print_expr_method_call(e, tokens, fixup), Expr::Paren(e) => e.to_tokens(tokens), Expr::Path(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Range(e) => print_expr_range(e, tokens, fixup), Expr::Reference(e) => print_expr_reference(e, tokens, fixup), Expr::Repeat(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Return(e) => print_expr_return(e, tokens, fixup), Expr::Struct(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Try(e) => print_expr_try(e, tokens, fixup), Expr::TryBlock(e) => e.to_tokens(tokens), Expr::Tuple(e) => e.to_tokens(tokens), @@ -3111,7 +3115,11 @@ pub(crate) mod printing { Expr::Unsafe(e) => e.to_tokens(tokens), Expr::Verbatim(e) => e.to_tokens(tokens), Expr::While(e) => e.to_tokens(tokens), + #[cfg(feature = "full")] Expr::Yield(e) => print_expr_yield(e, tokens, fixup), + + #[cfg(not(feature = "full"))] + _ => unreachable!(), }; if needs_group { @@ -3121,20 +3129,6 @@ pub(crate) mod printing { } } - fn leading_precedence(e: &Expr, #[cfg(feature = "full")] fixup: FixupContext) -> Precedence { - #[cfg(feature = "full")] - return fixup.leading_precedence(e); - #[cfg(not(feature = "full"))] - return Precedence::of(e); - } - - fn trailing_precedence(e: &Expr, #[cfg(feature = "full")] fixup: FixupContext) -> Precedence { - #[cfg(feature = "full")] - return fixup.trailing_precedence(e); - #[cfg(not(feature = "full"))] - return Precedence::of(e); - } - #[cfg(feature = "full")] #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprArray { @@ -3166,7 +3160,7 @@ pub(crate) mod printing { e.eq_token.to_tokens(tokens); print_subexpression( &e.right, - trailing_precedence(&e.right, fixup) < Precedence::Assign, + fixup.trailing_precedence(&e.right) < Precedence::Assign, tokens, fixup.subsequent_subexpression(), ); @@ -3207,24 +3201,15 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprBinary { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_binary( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_binary(self, tokens, FixupContext::NONE); } } - fn print_expr_binary( - e: &ExprBinary, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_binary(e: &ExprBinary, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); - #[cfg(feature = "full")] let left_fixup = fixup.leftmost_subexpression_with_begin_operator( + #[cfg(feature = "full")] match &e.op { BinOp::Sub(_) | BinOp::Mul(_) @@ -3241,38 +3226,22 @@ pub(crate) mod printing { _ => false, }, ); - let left_prec = leading_precedence( - &e.left, - #[cfg(feature = "full")] - left_fixup, - ); - - let right_prec = trailing_precedence( - &e.right, - #[cfg(feature = "full")] - fixup, - ); let binop_prec = Precedence::of_binop(&e.op); + let left_prec = left_fixup.leading_precedence(&e.left); + let right_prec = fixup.trailing_precedence(&e.right); let (left_needs_group, right_needs_group) = match binop_prec { Precedence::Assign => (left_prec <= Precedence::Range, right_prec < binop_prec), Precedence::Compare => (left_prec <= binop_prec, right_prec <= binop_prec), _ => (left_prec < binop_prec, right_prec <= binop_prec), }; - print_subexpression( - &e.left, - left_needs_group, - tokens, - #[cfg(feature = "full")] - left_fixup, - ); + print_subexpression(&e.left, left_needs_group, tokens, left_fixup); e.op.to_tokens(tokens); print_subexpression( &e.right, right_needs_group, tokens, - #[cfg(feature = "full")] fixup.subsequent_subexpression(), ); } @@ -3318,20 +3287,11 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprCall { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_call( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_call(self, tokens, FixupContext::NONE); } } - fn print_expr_call( - e: &ExprCall, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_call(e: &ExprCall, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); let call_precedence = if let Expr::Field(_) = &*e.func { @@ -3339,18 +3299,15 @@ pub(crate) mod printing { } else { Precedence::Unambiguous }; - #[cfg(feature = "full")] - let func_fixup = fixup.leftmost_subexpression_with_begin_operator(true, false); - let func_precedence = leading_precedence( - &e.func, + let func_fixup = fixup.leftmost_subexpression_with_begin_operator( #[cfg(feature = "full")] - func_fixup, + true, + false, ); print_subexpression( &e.func, - func_precedence < call_precedence, + func_fixup.leading_precedence(&e.func) < call_precedence, tokens, - #[cfg(feature = "full")] func_fixup, ); @@ -3362,26 +3319,16 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprCast { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_cast( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_cast(self, tokens, FixupContext::NONE); } } - fn print_expr_cast( - e: &ExprCast, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_cast(e: &ExprCast, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); print_subexpression( &e.expr, Precedence::of(&e.expr) < Precedence::Cast, tokens, - #[cfg(feature = "full")] fixup.leftmost_subexpression(), ); e.as_token.to_tokens(tokens); @@ -3438,26 +3385,16 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprField { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_field( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_field(self, tokens, FixupContext::NONE); } } - fn print_expr_field( - e: &ExprField, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_field(e: &ExprField, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); print_subexpression( &e.base, Precedence::of(&e.base) < Precedence::Unambiguous, tokens, - #[cfg(feature = "full")] fixup.leftmost_subexpression_with_dot(), ); e.dot_token.to_tokens(tokens); @@ -3533,33 +3470,21 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprIndex { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_index( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_index(self, tokens, FixupContext::NONE); } } - fn print_expr_index( - e: &ExprIndex, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_index(e: &ExprIndex, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); - #[cfg(feature = "full")] - let obj_fixup = fixup.leftmost_subexpression_with_begin_operator(true, false); - let obj_precedence = leading_precedence( - &e.expr, + let obj_fixup = fixup.leftmost_subexpression_with_begin_operator( #[cfg(feature = "full")] - obj_fixup, + true, + false, ); print_subexpression( &e.expr, - obj_precedence < Precedence::Unambiguous, + obj_fixup.leading_precedence(&e.expr) < Precedence::Unambiguous, tokens, - #[cfg(feature = "full")] obj_fixup, ); e.bracket_token.surround(tokens, |tokens| { @@ -3656,26 +3581,16 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprMethodCall { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_method_call( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_method_call(self, tokens, FixupContext::NONE); } } - fn print_expr_method_call( - e: &ExprMethodCall, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_method_call(e: &ExprMethodCall, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); print_subexpression( &e.receiver, Precedence::of(&e.receiver) < Precedence::Unambiguous, tokens, - #[cfg(feature = "full")] fixup.leftmost_subexpression_with_dot(), ); e.dot_token.to_tokens(tokens); @@ -3727,7 +3642,7 @@ pub(crate) mod printing { if let Some(end) = &e.end { print_subexpression( end, - trailing_precedence(end, fixup) <= Precedence::Range, + fixup.trailing_precedence(end) <= Precedence::Range, tokens, fixup.subsequent_subexpression(), ); @@ -3737,33 +3652,18 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprReference { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_reference( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_reference(self, tokens, FixupContext::NONE); } } - fn print_expr_reference( - e: &ExprReference, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_reference(e: &ExprReference, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); e.and_token.to_tokens(tokens); e.mutability.to_tokens(tokens); - let prec = trailing_precedence( - &e.expr, - #[cfg(feature = "full")] - fixup, - ); print_subexpression( &e.expr, - prec < Precedence::Prefix, + fixup.trailing_precedence(&e.expr) < Precedence::Prefix, tokens, - #[cfg(feature = "full")] fixup.subsequent_subexpression(), ); } @@ -3864,32 +3764,17 @@ pub(crate) mod printing { #[cfg_attr(docsrs, doc(cfg(feature = "printing")))] impl ToTokens for ExprUnary { fn to_tokens(&self, tokens: &mut TokenStream) { - print_expr_unary( - self, - tokens, - #[cfg(feature = "full")] - FixupContext::NONE, - ); + print_expr_unary(self, tokens, FixupContext::NONE); } } - fn print_expr_unary( - e: &ExprUnary, - tokens: &mut TokenStream, - #[cfg(feature = "full")] fixup: FixupContext, - ) { + fn print_expr_unary(e: &ExprUnary, tokens: &mut TokenStream, fixup: FixupContext) { outer_attrs_to_tokens(&e.attrs, tokens); e.op.to_tokens(tokens); - let prec = trailing_precedence( - &e.expr, - #[cfg(feature = "full")] - fixup, - ); print_subexpression( &e.expr, - prec < Precedence::Prefix, + fixup.trailing_precedence(&e.expr) < Precedence::Prefix, tokens, - #[cfg(feature = "full")] fixup.subsequent_subexpression(), ); } diff --git a/src/fixup.rs b/src/fixup.rs index b8605a7d1..58ed9e73d 100644 --- a/src/fixup.rs +++ b/src/fixup.rs @@ -13,6 +13,7 @@ pub(crate) struct FixupContext { // // match x {}; // not when its own statement // + #[cfg(feature = "full")] stmt: bool, // This is the difference between: @@ -44,6 +45,7 @@ pub(crate) struct FixupContext { // Example: `$match;` // // No parentheses required. + #[cfg(feature = "full")] leftmost_subexpression_in_stmt: bool, // Print expression such that it can be parsed as a match arm. @@ -59,6 +61,7 @@ pub(crate) struct FixupContext { // _ => m! {} - 1, // binary subtraction operator // } // + #[cfg(feature = "full")] match_arm: bool, // This is almost equivalent to `leftmost_subexpression_in_stmt`, other than @@ -74,6 +77,7 @@ pub(crate) struct FixupContext { // _ => m! {} - 1, // no parens // } // + #[cfg(feature = "full")] leftmost_subexpression_in_match_arm: bool, // This is the difference between: @@ -84,6 +88,7 @@ pub(crate) struct FixupContext { // () if let _ = Struct {} => {} // no parens // } // + #[cfg(feature = "full")] parenthesize_exterior_struct_lit: bool, // This is the difference between: @@ -92,6 +97,7 @@ pub(crate) struct FixupContext { // // let _ = 1 + (return 1) + 1; // needs parens // + #[cfg(feature = "full")] parenthesize_exterior_jump: bool, // This is the difference between: @@ -100,6 +106,7 @@ pub(crate) struct FixupContext { // // let _ = return + 1; // no paren because '+' cannot begin expr // + #[cfg(feature = "full")] next_operator_can_begin_expr: bool, // This is the difference between: @@ -116,18 +123,26 @@ impl FixupContext { /// The default amount of fixing is minimal fixing. Fixups should be turned /// on in a targeted fashion where needed. pub const NONE: Self = FixupContext { + #[cfg(feature = "full")] stmt: false, + #[cfg(feature = "full")] leftmost_subexpression_in_stmt: false, + #[cfg(feature = "full")] match_arm: false, + #[cfg(feature = "full")] leftmost_subexpression_in_match_arm: false, + #[cfg(feature = "full")] parenthesize_exterior_struct_lit: false, + #[cfg(feature = "full")] parenthesize_exterior_jump: false, + #[cfg(feature = "full")] next_operator_can_begin_expr: false, next_operator_can_begin_generics: false, }; /// Create the initial fixup for printing an expression in statement /// position. + #[cfg(feature = "full")] pub fn new_stmt() -> Self { FixupContext { stmt: true, @@ -137,6 +152,7 @@ impl FixupContext { /// Create the initial fixup for printing an expression as the right-hand /// side of a match arm. + #[cfg(feature = "full")] pub fn new_match_arm() -> Self { FixupContext { match_arm: true, @@ -148,6 +164,7 @@ impl FixupContext { /// of an `if` or `while`. There are a few other positions which are /// grammatically equivalent and also use this, such as the iterator /// expression in `for` and the scrutinee in `match`. + #[cfg(feature = "full")] pub fn new_condition() -> Self { FixupContext { parenthesize_exterior_struct_lit: true, @@ -168,11 +185,16 @@ impl FixupContext { /// `-$a` nor `[$a]` have one. pub fn leftmost_subexpression(self) -> Self { FixupContext { + #[cfg(feature = "full")] stmt: false, + #[cfg(feature = "full")] leftmost_subexpression_in_stmt: self.stmt || self.leftmost_subexpression_in_stmt, + #[cfg(feature = "full")] match_arm: false, + #[cfg(feature = "full")] leftmost_subexpression_in_match_arm: self.match_arm || self.leftmost_subexpression_in_match_arm, + #[cfg(feature = "full")] parenthesize_exterior_jump: true, ..self } @@ -184,10 +206,15 @@ impl FixupContext { /// subexpressions. pub fn leftmost_subexpression_with_dot(self) -> Self { FixupContext { + #[cfg(feature = "full")] stmt: self.stmt || self.leftmost_subexpression_in_stmt, + #[cfg(feature = "full")] leftmost_subexpression_in_stmt: false, + #[cfg(feature = "full")] match_arm: self.match_arm || self.leftmost_subexpression_in_match_arm, + #[cfg(feature = "full")] leftmost_subexpression_in_match_arm: false, + #[cfg(feature = "full")] parenthesize_exterior_jump: true, ..self } @@ -198,10 +225,11 @@ impl FixupContext { /// first token of an expression. pub fn leftmost_subexpression_with_begin_operator( self, - next_operator_can_begin_expr: bool, + #[cfg(feature = "full")] next_operator_can_begin_expr: bool, next_operator_can_begin_generics: bool, ) -> Self { FixupContext { + #[cfg(feature = "full")] next_operator_can_begin_expr, next_operator_can_begin_generics, ..self.leftmost_subexpression() @@ -218,9 +246,13 @@ impl FixupContext { /// `$a.f($b)`. pub fn subsequent_subexpression(self) -> Self { FixupContext { + #[cfg(feature = "full")] stmt: false, + #[cfg(feature = "full")] leftmost_subexpression_in_stmt: false, + #[cfg(feature = "full")] match_arm: false, + #[cfg(feature = "full")] leftmost_subexpression_in_match_arm: false, ..self } @@ -231,6 +263,7 @@ impl FixupContext { /// /// The documentation on `FixupContext::leftmost_subexpression_in_stmt` has /// examples. + #[cfg(feature = "full")] pub fn would_cause_statement_boundary(self, expr: &Expr) -> bool { (self.leftmost_subexpression_in_stmt && !classify::requires_semi_to_be_stmt(expr)) || ((self.stmt || self.leftmost_subexpression_in_stmt) && matches!(expr, Expr::Let(_))) @@ -249,6 +282,7 @@ impl FixupContext { /// /// - `true && false`, because otherwise this would be misinterpreted as a /// "let chain". + #[cfg(feature = "full")] pub fn needs_group_as_let_scrutinee(self, expr: &Expr) -> bool { self.parenthesize_exterior_struct_lit && classify::confusable_with_adjacent_block(expr) || self.trailing_precedence(expr) < Precedence::Let @@ -257,6 +291,7 @@ impl FixupContext { /// Determines the effective precedence of a left subexpression. Some /// expressions have lower precedence when adjacent to particular operators. pub fn leading_precedence(self, expr: &Expr) -> Precedence { + #[cfg(feature = "full")] if self.next_operator_can_begin_expr { // Decrease precedence of value-less jumps when followed by an // operator that would otherwise get interpreted as beginning a @@ -272,6 +307,7 @@ impl FixupContext { /// expressions have higher precedence on the right side of a binary /// operator than on the left. pub fn trailing_precedence(self, expr: &Expr) -> Precedence { + #[cfg(feature = "full")] if !self.parenthesize_exterior_jump { match expr { // Increase precedence of expressions that extend to the end of diff --git a/src/lib.rs b/src/lib.rs index 5390f2edf..dce19ca55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -330,7 +330,10 @@ mod bigint; #[cfg_attr(docsrs, doc(cfg(feature = "parsing")))] pub mod buffer; -#[cfg(all(any(feature = "parsing", feature = "printing"), feature = "full"))] +#[cfg(any( + all(feature = "parsing", feature = "full"), + all(feature = "printing", any(feature = "full", feature = "derive")), +))] mod classify; mod custom_keyword; @@ -384,7 +387,7 @@ mod file; #[cfg_attr(docsrs, doc(cfg(feature = "full")))] pub use crate::file::File; -#[cfg(all(feature = "full", feature = "printing"))] +#[cfg(all(any(feature = "full", feature = "derive"), feature = "printing"))] mod fixup; #[cfg(any(feature = "full", feature = "derive"))]