From 42e47dfe8251c8356c409485d66164b6fceb03c6 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 18 Jun 2024 20:21:05 +1000 Subject: [PATCH] Remove `From` impls for `LhsExpr`. The `Option` one maps to the first two variants, and the `P` one maps to the third. Weird. The code is shorter and clearer without them. --- compiler/rustc_parse/src/parser/expr.rs | 28 +++++++------------------ compiler/rustc_parse/src/parser/pat.rs | 7 +++---- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index cdeb29118994..a0f57ffecfa6 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -75,25 +75,6 @@ pub(super) enum LhsExpr { AlreadyParsed { expr: P, starts_statement: bool }, } -impl From> for LhsExpr { - /// Converts `Some(attrs)` into `LhsExpr::AttributesParsed(attrs)` - /// and `None` into `LhsExpr::NotYetParsed`. - /// - /// This conversion does not allocate. - fn from(o: Option) -> Self { - if let Some(attrs) = o { LhsExpr::AttributesParsed(attrs) } else { LhsExpr::NotYetParsed } - } -} - -impl From> for LhsExpr { - /// Converts the `expr: P` into `LhsExpr::AlreadyParsed { expr, starts_statement: false }`. - /// - /// This conversion does not allocate. - fn from(expr: P) -> Self { - LhsExpr::AlreadyParsed { expr, starts_statement: false } - } -} - #[derive(Debug)] enum DestructuredFloat { /// 1e2 @@ -166,7 +147,11 @@ impl<'a> Parser<'a> { &mut self, already_parsed_attrs: Option, ) -> PResult<'a, P> { - self.parse_expr_assoc_with(0, already_parsed_attrs.into()) + let lhs = match already_parsed_attrs { + Some(attrs) => LhsExpr::AttributesParsed(attrs), + None => LhsExpr::NotYetParsed, + }; + self.parse_expr_assoc_with(0, lhs) } /// Parses an associative expression with operators of at least `min_prec` precedence. @@ -2660,7 +2645,8 @@ impl<'a> Parser<'a> { } else { self.expect(&token::Eq)?; } - let expr = self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), None.into())?; + let expr = + self.parse_expr_assoc_with(1 + prec_let_scrutinee_needs_par(), LhsExpr::NotYetParsed)?; let span = lo.to(expr.span); Ok(self.mk_expr(span, ExprKind::Let(pat, expr, span, recovered))) } diff --git a/compiler/rustc_parse/src/parser/pat.rs b/compiler/rustc_parse/src/parser/pat.rs index 8af415f7c9dd..a3efecffcc65 100644 --- a/compiler/rustc_parse/src/parser/pat.rs +++ b/compiler/rustc_parse/src/parser/pat.rs @@ -10,7 +10,7 @@ use crate::errors::{ UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern, }; -use crate::parser::expr::could_be_unclosed_char_literal; +use crate::parser::expr::{could_be_unclosed_char_literal, LhsExpr}; use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole}; use rustc_ast::mut_visit::{noop_visit_pat, MutVisitor}; use rustc_ast::ptr::P; @@ -398,9 +398,8 @@ impl<'a> Parser<'a> { // Parse an associative expression such as `+ expr`, `% expr`, ... // Assignements, ranges and `|` are disabled by [`Restrictions::IS_PAT`]. - if let Ok(expr) = - snapshot.parse_expr_assoc_with(0, expr.into()).map_err(|err| err.cancel()) - { + let lhs = LhsExpr::AlreadyParsed { expr, starts_statement: false }; + if let Ok(expr) = snapshot.parse_expr_assoc_with(0, lhs).map_err(|err| err.cancel()) { // We got a valid expression. self.restore_snapshot(snapshot); self.restrictions.remove(Restrictions::IS_PAT);