From 95960b7d54ecd05b03734cadea071cd5cc29fa35 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Fri, 18 Feb 2022 15:27:58 -0800 Subject: [PATCH] Make `standalone` an enum --- .../rustc_parse/src/parser/diagnostics.rs | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 4f2a308f2e9..162982ebbe2 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -161,15 +161,24 @@ impl AttemptLocalParseRecovery { #[derive(Debug, Copy, Clone)] struct IncDecRecovery { /// Is this increment/decrement its own statement? - /// - /// This is `None` when we are unsure. - standalone: Option, + standalone: IsStandalone, /// Is this an increment or decrement? op: IncOrDec, /// Is this pre- or postfix? fixity: UnaryFixity, } +/// Is an increment or decrement expression its own statement? +#[derive(Debug, Copy, Clone)] +enum IsStandalone { + /// It's standalone, i.e., its own statement. + Standalone, + /// It's a subexpression, i.e., *not* standalone. + Subexpr, + /// It's maybe standalone; we're not sure. + Maybe, +} + #[derive(Debug, Copy, Clone, PartialEq, Eq)] enum IncOrDec { Inc, @@ -1226,11 +1235,9 @@ impl<'a> Parser<'a> { op_span: Span, prev_is_semi: bool, ) -> PResult<'a, P> { - let kind = IncDecRecovery { - standalone: Some(prev_is_semi), - op: IncOrDec::Inc, - fixity: UnaryFixity::Pre, - }; + let standalone = + if prev_is_semi { IsStandalone::Standalone } else { IsStandalone::Subexpr }; + let kind = IncDecRecovery { standalone, op: IncOrDec::Inc, fixity: UnaryFixity::Pre }; self.recover_from_inc_dec(operand_expr, kind, op_span) } @@ -1240,8 +1247,11 @@ impl<'a> Parser<'a> { operand_expr: P, op_span: Span, ) -> PResult<'a, P> { - let kind = - IncDecRecovery { standalone: None, op: IncOrDec::Inc, fixity: UnaryFixity::Post }; + let kind = IncDecRecovery { + standalone: IsStandalone::Maybe, + op: IncOrDec::Inc, + fixity: UnaryFixity::Post, + }; self.recover_from_inc_dec(operand_expr, kind, op_span) } @@ -1271,8 +1281,10 @@ impl<'a> Parser<'a> { }; match kind.standalone { - Some(true) => self.inc_dec_standalone_recovery(&mut err, kind, spans, false), - Some(false) => { + IsStandalone::Standalone => { + self.inc_dec_standalone_recovery(&mut err, kind, spans, false) + } + IsStandalone::Subexpr => { let Ok(base_src) = self.span_to_snippet(base.span) else { return help_base_case(err, base) }; match kind.fixity { @@ -1284,7 +1296,7 @@ impl<'a> Parser<'a> { } } } - None => { + IsStandalone::Maybe => { let Ok(base_src) = self.span_to_snippet(base.span) else { return help_base_case(err, base) }; match kind.fixity {