Refactor interpolated_or_expr_span.

It's much more complicated than it needs to be, and it doesn't modify
the expression. We can do the `Result` handling outside of it, and
change it to just return a span.

Also fix an errant comma that makes the comment hard to read.
This commit is contained in:
Nicholas Nethercote 2023-08-15 09:01:22 +10:00
parent e46caaf84b
commit dee6c9241f

View File

@ -626,8 +626,8 @@ impl<'a> Parser<'a> {
fn parse_expr_prefix_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> { fn parse_expr_prefix_common(&mut self, lo: Span) -> PResult<'a, (Span, P<Expr>)> {
self.bump(); self.bump();
let expr = self.parse_expr_prefix(None); let expr = self.parse_expr_prefix(None)?;
let (span, expr) = self.interpolated_or_expr_span(expr)?; let span = self.interpolated_or_expr_span(&expr);
Ok((lo.to(span), expr)) Ok((lo.to(span), expr))
} }
@ -702,20 +702,12 @@ impl<'a> Parser<'a> {
self.parse_expr_unary(lo, UnOp::Not) self.parse_expr_unary(lo, UnOp::Not)
} }
/// Returns the span of expr, if it was not interpolated or the span of the interpolated token. /// Returns the span of expr if it was not interpolated, or the span of the interpolated token.
fn interpolated_or_expr_span( fn interpolated_or_expr_span(&self, expr: &Expr) -> Span {
&self, match self.prev_token.kind {
expr: PResult<'a, P<Expr>>, TokenKind::Interpolated(..) => self.prev_token.span,
) -> PResult<'a, (Span, P<Expr>)> { _ => expr.span,
expr.map(|e| { }
(
match self.prev_token.kind {
TokenKind::Interpolated(..) => self.prev_token.span,
_ => e.span,
},
e,
)
})
} }
fn parse_assoc_op_cast( fn parse_assoc_op_cast(
@ -898,8 +890,8 @@ impl<'a> Parser<'a> {
self.parse_expr_prefix_range(None) self.parse_expr_prefix_range(None)
} else { } else {
self.parse_expr_prefix(None) self.parse_expr_prefix(None)
}; }?;
let (hi, expr) = self.interpolated_or_expr_span(expr)?; let hi = self.interpolated_or_expr_span(&expr);
let span = lo.to(hi); let span = lo.to(hi);
if let Some(lt) = lifetime { if let Some(lt) = lifetime {
self.error_remove_borrow_lifetime(span, lt.ident.span); self.error_remove_borrow_lifetime(span, lt.ident.span);
@ -930,8 +922,8 @@ impl<'a> Parser<'a> {
fn parse_expr_dot_or_call(&mut self, attrs: Option<AttrWrapper>) -> PResult<'a, P<Expr>> { fn parse_expr_dot_or_call(&mut self, attrs: Option<AttrWrapper>) -> PResult<'a, P<Expr>> {
let attrs = self.parse_or_use_outer_attributes(attrs)?; let attrs = self.parse_or_use_outer_attributes(attrs)?;
self.collect_tokens_for_expr(attrs, |this, attrs| { self.collect_tokens_for_expr(attrs, |this, attrs| {
let base = this.parse_expr_bottom(); let base = this.parse_expr_bottom()?;
let (span, base) = this.interpolated_or_expr_span(base)?; let span = this.interpolated_or_expr_span(&base);
this.parse_expr_dot_or_call_with(base, span, attrs) this.parse_expr_dot_or_call_with(base, span, attrs)
}) })
} }