diff options
| author | Aaron Hill <aa1ronham@gmail.com> | 2020-07-27 18:02:29 -0400 |
|---|---|---|
| committer | Aaron Hill <aa1ronham@gmail.com> | 2020-08-20 02:45:28 -0400 |
| commit | 607a1900592c91ba2b284afda8ea9a1521d52475 (patch) | |
| tree | e7193d6992e112c2a6fa8ca9a6964188e6b2cc15 /src/librustc_parse/parser | |
| parent | f6049b60dbbc267a69b5c5f1d335f8f54bac2173 (diff) | |
Capture tokens for Pat used in macro_rules! argument
This extends PR #73293 to handle patterns (Pat). Unlike expressions, patterns do not support custom attributes, so we only need to capture tokens during macro_rules! argument parsing.
Diffstat (limited to 'src/librustc_parse/parser')
| -rw-r--r-- | src/librustc_parse/parser/diagnostics.rs | 11 | ||||
| -rw-r--r-- | src/librustc_parse/parser/nonterminal.rs | 9 | ||||
| -rw-r--r-- | src/librustc_parse/parser/pat.rs | 2 |
3 files changed, 18 insertions, 4 deletions
diff --git a/src/librustc_parse/parser/diagnostics.rs b/src/librustc_parse/parser/diagnostics.rs index be01c7f8498..12efe391fb9 100644 --- a/src/librustc_parse/parser/diagnostics.rs +++ b/src/librustc_parse/parser/diagnostics.rs @@ -26,6 +26,7 @@ pub(super) fn dummy_arg(ident: Ident) -> Param { id: ast::DUMMY_NODE_ID, kind: PatKind::Ident(BindingMode::ByValue(Mutability::Not), ident, None), span: ident.span, + tokens: None, }); let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID }; Param { @@ -83,7 +84,12 @@ impl RecoverQPath for Pat { self.to_ty() } fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self { - Self { span: path.span, kind: PatKind::Path(qself, path), id: ast::DUMMY_NODE_ID } + Self { + span: path.span, + kind: PatKind::Path(qself, path), + id: ast::DUMMY_NODE_ID, + tokens: None, + } } } @@ -1526,7 +1532,8 @@ impl<'a> Parser<'a> { .emit(); // Pretend the pattern is `_`, to avoid duplicate errors from AST validation. - let pat = P(Pat { kind: PatKind::Wild, span: pat.span, id: ast::DUMMY_NODE_ID }); + let pat = + P(Pat { kind: PatKind::Wild, span: pat.span, id: ast::DUMMY_NODE_ID, tokens: None }); Ok((pat, ty)) } diff --git a/src/librustc_parse/parser/nonterminal.rs b/src/librustc_parse/parser/nonterminal.rs index 12139771bbf..f40cd1131d2 100644 --- a/src/librustc_parse/parser/nonterminal.rs +++ b/src/librustc_parse/parser/nonterminal.rs @@ -116,7 +116,14 @@ impl<'a> Parser<'a> { Some(s) => token::NtStmt(s), None => return Err(self.struct_span_err(self.token.span, "expected a statement")), }, - NonterminalKind::Pat => token::NtPat(self.parse_pat(None)?), + NonterminalKind::Pat => { + let (mut pat, tokens) = self.collect_tokens(|this| this.parse_pat(None))?; + // We have have eaten an NtPat, which could already have tokens + if pat.tokens.is_none() { + pat.tokens = Some(tokens); + } + token::NtPat(pat) + } NonterminalKind::Expr => { let (mut expr, tokens) = self.collect_tokens(|this| this.parse_expr())?; // If we captured tokens during parsing (due to outer attributes), diff --git a/src/librustc_parse/parser/pat.rs b/src/librustc_parse/parser/pat.rs index 1d8e6cc9bac..2c0133a24dc 100644 --- a/src/librustc_parse/parser/pat.rs +++ b/src/librustc_parse/parser/pat.rs @@ -1007,6 +1007,6 @@ impl<'a> Parser<'a> { } fn mk_pat(&self, span: Span, kind: PatKind) -> P<Pat> { - P(Pat { kind, span, id: ast::DUMMY_NODE_ID }) + P(Pat { kind, span, id: ast::DUMMY_NODE_ID, tokens: None }) } } |
