From a961e6785c7ed33a532bb6172ae0c95f44db5726 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Wed, 13 Jan 2021 16:28:57 -0500 Subject: Set tokens on AST node in `collect_tokens` A new `HasTokens` trait is introduced, which is used to move logic from the callers of `collect_tokens` into the body of `collect_tokens`. In addition to reducing duplication, this paves the way for PR #80689, which needs to perform additional logic during token collection. --- compiler/rustc_parse/src/parser/expr.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'compiler/rustc_parse/src/parser/expr.rs') diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index f4332e4548a..6db415ead41 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -472,7 +472,8 @@ impl<'a> Parser<'a> { /// Parses a prefix-unary-operator expr. fn parse_prefix_expr(&mut self, attrs: Option) -> PResult<'a, P> { let attrs = self.parse_or_use_outer_attributes(attrs)?; - self.maybe_collect_tokens(super::attr::maybe_needs_tokens(&attrs), |this| { + let needs_tokens = super::attr::maybe_needs_tokens(&attrs); + let do_parse = |this: &mut Parser<'a>| { let lo = this.token.span; // Note: when adding new unary operators, don't forget to adjust TokenKind::can_begin_expr() let (hi, ex) = match this.token.uninterpolate().kind { @@ -488,7 +489,8 @@ impl<'a> Parser<'a> { _ => return this.parse_dot_or_call_expr(Some(attrs)), }?; Ok(this.mk_expr(lo.to(hi), ex, attrs)) - }) + }; + if needs_tokens { self.collect_tokens(do_parse) } else { do_parse(self) } } fn parse_prefix_expr_common(&mut self, lo: Span) -> PResult<'a, (Span, P)> { @@ -1125,20 +1127,6 @@ impl<'a> Parser<'a> { } } - fn maybe_collect_tokens( - &mut self, - needs_tokens: bool, - f: impl FnOnce(&mut Self) -> PResult<'a, P>, - ) -> PResult<'a, P> { - if needs_tokens { - let (mut expr, tokens) = self.collect_tokens(f)?; - expr.tokens = tokens; - Ok(expr) - } else { - f(self) - } - } - fn parse_lit_expr(&mut self, attrs: AttrVec) -> PResult<'a, P> { let lo = self.token.span; match self.parse_opt_lit() { -- cgit 1.4.1-3-g733a5