Skip to content

Commit 96175be

Browse files
committed
Integrate Expr::Let into precedence fixups
1 parent 1f096d3 commit 96175be

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

src/fixup.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl FixupContext {
357357
}
358358
_ => default_prec <= self.previous_operator,
359359
} && match self.next_operator {
360-
Precedence::Range => true,
360+
Precedence::Range | Precedence::Or | Precedence::And => true,
361361
_ => !self.next_operator_can_begin_expr,
362362
} {
363363
if let Scan::Bailout | Scan::Fail = scan_right(expr, self, self.previous_operator, 1, 0)
@@ -700,6 +700,37 @@ fn scan_right(
700700
Scan::Consume
701701
}
702702
}
703+
Expr::Let(e) => {
704+
if bailout_offset >= 1 {
705+
return Scan::Consume;
706+
}
707+
let right_fixup = fixup.rightmost_subexpression_fixup(false, false, Precedence::Let);
708+
let scan = scan_right(
709+
&e.expr,
710+
right_fixup,
711+
Precedence::Let,
712+
1,
713+
if fixup.next_operator < Precedence::Let {
714+
0
715+
} else {
716+
1
717+
},
718+
);
719+
match scan {
720+
Scan::Fail | Scan::Bailout if fixup.next_operator < Precedence::Let => {
721+
return Scan::Bailout;
722+
}
723+
Scan::Consume => return Scan::Consume,
724+
_ => {}
725+
}
726+
if right_fixup.rightmost_subexpression_precedence(&e.expr) < Precedence::Let {
727+
Scan::Consume
728+
} else if let Scan::Fail = scan {
729+
Scan::Bailout
730+
} else {
731+
Scan::Consume
732+
}
733+
}
703734
Expr::Array(_)
704735
| Expr::Async(_)
705736
| Expr::Await(_)
@@ -714,7 +745,6 @@ fn scan_right(
714745
| Expr::If(_)
715746
| Expr::Index(_)
716747
| Expr::Infer(_)
717-
| Expr::Let(_)
718748
| Expr::Lit(_)
719749
| Expr::Loop(_)
720750
| Expr::Macro(_)
@@ -731,6 +761,9 @@ fn scan_right(
731761
| Expr::Verbatim(_)
732762
| Expr::While(_) => match fixup.next_operator {
733763
Precedence::Assign | Precedence::Range if precedence == Precedence::Range => Scan::Fail,
764+
_ if precedence == Precedence::Let && fixup.next_operator < Precedence::Let => {
765+
Scan::Fail
766+
}
734767
_ => consume_by_precedence,
735768
},
736769
}

tests/test_expr.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use syn::visit_mut::VisitMut as _;
2424
use syn::{
2525
parse_quote, token, AngleBracketedGenericArguments, Arm, BinOp, Block, Expr, ExprArray,
2626
ExprAssign, ExprAsync, ExprAwait, ExprBinary, ExprBlock, ExprBreak, ExprCall, ExprCast,
27-
ExprClosure, ExprConst, ExprContinue, ExprField, ExprForLoop, ExprIf, ExprIndex, ExprLit,
28-
ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprPath, ExprRange, ExprRawAddr,
27+
ExprClosure, ExprConst, ExprContinue, ExprField, ExprForLoop, ExprIf, ExprIndex, ExprLet,
28+
ExprLit, ExprLoop, ExprMacro, ExprMatch, ExprMethodCall, ExprPath, ExprRange, ExprRawAddr,
2929
ExprReference, ExprReturn, ExprStruct, ExprTry, ExprTryBlock, ExprTuple, ExprUnary, ExprUnsafe,
3030
ExprWhile, ExprYield, GenericArgument, Label, Lifetime, Lit, LitInt, Macro, MacroDelimiter,
3131
Member, Pat, PatWild, Path, PathArguments, PathSegment, PointerMutability, QSelf, RangeLimits,
@@ -1076,6 +1076,20 @@ fn test_permutations() -> ExitCode {
10761076
}));
10771077
});
10781078

1079+
// Expr::Let
1080+
iter(depth, &mut |expr| {
1081+
f(Expr::Let(ExprLet {
1082+
attrs: Vec::new(),
1083+
let_token: Token![let](span),
1084+
pat: Box::new(Pat::Wild(PatWild {
1085+
attrs: Vec::new(),
1086+
underscore_token: Token![_](span),
1087+
})),
1088+
eq_token: Token![=](span),
1089+
expr: Box::new(expr),
1090+
}));
1091+
});
1092+
10791093
// Expr::Range
10801094
f(Expr::Range(ExprRange {
10811095
// `..`

0 commit comments

Comments
 (0)