Skip to content

Commit 539c863

Browse files
committed
Eliminate precedence arithmetic from rustc_parse
1 parent ca8f474 commit 539c863

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

compiler/rustc_parse/src/parser/expr.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ignore-tidy-filelength
22

33
use core::mem;
4-
use core::ops::ControlFlow;
4+
use core::ops::{Bound, ControlFlow};
55

66
use ast::mut_visit::{self, MutVisitor};
77
use ast::token::IdentIsRaw;
@@ -120,15 +120,15 @@ impl<'a> Parser<'a> {
120120
r: Restrictions,
121121
attrs: AttrWrapper,
122122
) -> PResult<'a, (P<Expr>, bool)> {
123-
self.with_res(r, |this| this.parse_expr_assoc_with(0, attrs))
123+
self.with_res(r, |this| this.parse_expr_assoc_with(Bound::Unbounded, attrs))
124124
}
125125

126126
/// Parses an associative expression with operators of at least `min_prec` precedence.
127127
/// The `bool` in the return value indicates if it was an assoc expr, i.e. with an operator
128128
/// followed by a subexpression (e.g. `1 + 2`).
129129
pub(super) fn parse_expr_assoc_with(
130130
&mut self,
131-
min_prec: usize,
131+
min_prec: Bound<usize>,
132132
attrs: AttrWrapper,
133133
) -> PResult<'a, (P<Expr>, bool)> {
134134
let lhs = if self.token.is_range_separator() {
@@ -144,7 +144,7 @@ impl<'a> Parser<'a> {
144144
/// was actually parsed.
145145
pub(super) fn parse_expr_assoc_rest_with(
146146
&mut self,
147-
min_prec: usize,
147+
min_prec: Bound<usize>,
148148
starts_stmt: bool,
149149
mut lhs: P<Expr>,
150150
) -> PResult<'a, (P<Expr>, bool)> {
@@ -163,7 +163,11 @@ impl<'a> Parser<'a> {
163163
self.restrictions
164164
};
165165
let prec = op.node.precedence();
166-
if prec < min_prec {
166+
if match min_prec {
167+
Bound::Included(min_prec) => prec < min_prec,
168+
Bound::Excluded(min_prec) => prec <= min_prec,
169+
Bound::Unbounded => false,
170+
} {
167171
break;
168172
}
169173
// Check for deprecated `...` syntax
@@ -276,16 +280,16 @@ impl<'a> Parser<'a> {
276280
}
277281

278282
let fixity = op.fixity();
279-
let prec_adjustment = match fixity {
280-
Fixity::Right => 0,
281-
Fixity::Left => 1,
283+
let min_prec = match fixity {
284+
Fixity::Right => Bound::Included(prec),
285+
Fixity::Left => Bound::Excluded(prec),
282286
// We currently have no non-associative operators that are not handled above by
283287
// the special cases. The code is here only for future convenience.
284-
Fixity::None => 1,
288+
Fixity::None => Bound::Excluded(prec),
285289
};
286290
let (rhs, _) = self.with_res(restrictions - Restrictions::STMT_EXPR, |this| {
287291
let attrs = this.parse_outer_attributes()?;
288-
this.parse_expr_assoc_with(prec + prec_adjustment, attrs)
292+
this.parse_expr_assoc_with(min_prec, attrs)
289293
})?;
290294

291295
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
@@ -460,7 +464,7 @@ impl<'a> Parser<'a> {
460464
let maybe_lt = self.token.clone();
461465
let attrs = self.parse_outer_attributes()?;
462466
Some(
463-
self.parse_expr_assoc_with(prec + 1, attrs)
467+
self.parse_expr_assoc_with(Bound::Excluded(prec), attrs)
464468
.map_err(|err| self.maybe_err_dotdotlt_syntax(maybe_lt, err))?
465469
.0,
466470
)
@@ -518,7 +522,7 @@ impl<'a> Parser<'a> {
518522
let (span, opt_end) = if this.is_at_start_of_range_notation_rhs() {
519523
// RHS must be parsed with more associativity than the dots.
520524
let attrs = this.parse_outer_attributes()?;
521-
this.parse_expr_assoc_with(op.unwrap().precedence() + 1, attrs)
525+
this.parse_expr_assoc_with(Bound::Excluded(op.unwrap().precedence()), attrs)
522526
.map(|(x, _)| (lo.to(x.span), Some(x)))
523527
.map_err(|err| this.maybe_err_dotdotlt_syntax(maybe_lt, err))?
524528
} else {
@@ -2643,7 +2647,8 @@ impl<'a> Parser<'a> {
26432647
self.expect(&token::Eq)?;
26442648
}
26452649
let attrs = self.parse_outer_attributes()?;
2646-
let (expr, _) = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), attrs)?;
2650+
let (expr, _) =
2651+
self.parse_expr_assoc_with(Bound::Excluded(prec_let_scrutinee_needs_par()), attrs)?;
26472652
let span = lo.to(expr.span);
26482653
Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, recovered)))
26492654
}

compiler/rustc_parse/src/parser/pat.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::Bound;
2+
13
use rustc_ast::mut_visit::{self, MutVisitor};
24
use rustc_ast::ptr::P;
35
use rustc_ast::token::{self, BinOpToken, Delimiter, IdentIsRaw, Token};
@@ -435,8 +437,9 @@ impl<'a> Parser<'a> {
435437

436438
// Parse an associative expression such as `+ expr`, `% expr`, ...
437439
// Assignments, ranges and `|` are disabled by [`Restrictions::IS_PAT`].
438-
let Ok((expr, _)) =
439-
snapshot.parse_expr_assoc_rest_with(0, false, expr).map_err(|err| err.cancel())
440+
let Ok((expr, _)) = snapshot
441+
.parse_expr_assoc_rest_with(Bound::Unbounded, false, expr)
442+
.map_err(|err| err.cancel())
440443
else {
441444
// We got a trailing method/operator, but that wasn't an expression.
442445
return None;

compiler/rustc_parse/src/parser/stmt.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::mem;
3+
use std::ops::Bound;
34

45
use ast::Label;
56
use rustc_ast as ast;
@@ -207,7 +208,7 @@ impl<'a> Parser<'a> {
207208
// Perform this outside of the `collect_tokens` closure, since our
208209
// outer attributes do not apply to this part of the expression.
209210
let (expr, _) = self.with_res(Restrictions::STMT_EXPR, |this| {
210-
this.parse_expr_assoc_rest_with(0, true, expr)
211+
this.parse_expr_assoc_rest_with(Bound::Unbounded, true, expr)
211212
})?;
212213
Ok(self.mk_stmt(lo.to(self.prev_token.span), StmtKind::Expr(expr)))
213214
} else {
@@ -240,7 +241,7 @@ impl<'a> Parser<'a> {
240241
let e = self.mk_expr(lo.to(hi), ExprKind::MacCall(mac));
241242
let e = self.maybe_recover_from_bad_qpath(e)?;
242243
let e = self.parse_expr_dot_or_call_with(attrs, e, lo)?;
243-
let (e, _) = self.parse_expr_assoc_rest_with(0, false, e)?;
244+
let (e, _) = self.parse_expr_assoc_rest_with(Bound::Unbounded, false, e)?;
244245
StmtKind::Expr(e)
245246
};
246247
Ok(self.mk_stmt(lo.to(hi), kind))

0 commit comments

Comments
 (0)