From da8eb29a2f70a58122903bf087bd6c1d0fbd6d3f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 3 Apr 2020 15:38:42 +0200 Subject: Macro patterns are not confused with expressions. We treat macro calls as expressions (there's appropriate Into impl), which causes problem if there's expresison and non-expression macro in the same node (like in the match arm). We fix this problem by nesting macor patterns into another node (the same way we nest path into PathExpr or PathPat). Ideally, we probably should add a similar nesting for macro expressions, but that needs some careful thinking about macros in blocks: `{ am_i_expression!() }`. --- crates/ra_syntax/src/ast/generated.rs | 42 ++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast/generated.rs') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 7204ca5b1..0c339b987 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -2563,6 +2563,38 @@ impl LiteralPat { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct MacroPat { + pub(crate) syntax: SyntaxNode, +} +impl std::fmt::Display for MacroPat { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + std::fmt::Display::fmt(self.syntax(), f) + } +} +impl AstNode for MacroPat { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + MACRO_PAT => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { + &self.syntax + } +} +impl MacroPat { + pub fn macro_call(&self) -> Option { + AstChildren::new(&self.syntax).next() + } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RecordPat { pub(crate) syntax: SyntaxNode, } @@ -4600,6 +4632,7 @@ pub enum Pat { SlicePat(SlicePat), RangePat(RangePat), LiteralPat(LiteralPat), + MacroPat(MacroPat), } impl From for Pat { fn from(node: OrPat) -> Pat { @@ -4671,6 +4704,11 @@ impl From for Pat { Pat::LiteralPat(node) } } +impl From for Pat { + fn from(node: MacroPat) -> Pat { + Pat::MacroPat(node) + } +} impl std::fmt::Display for Pat { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { std::fmt::Display::fmt(self.syntax(), f) @@ -4681,7 +4719,7 @@ impl AstNode for Pat { match kind { OR_PAT | PAREN_PAT | REF_PAT | BOX_PAT | BIND_PAT | PLACEHOLDER_PAT | DOT_DOT_PAT | PATH_PAT | RECORD_PAT | TUPLE_STRUCT_PAT | TUPLE_PAT | SLICE_PAT | RANGE_PAT - | LITERAL_PAT => true, + | LITERAL_PAT | MACRO_PAT => true, _ => false, } } @@ -4701,6 +4739,7 @@ impl AstNode for Pat { SLICE_PAT => Pat::SlicePat(SlicePat { syntax }), RANGE_PAT => Pat::RangePat(RangePat { syntax }), LITERAL_PAT => Pat::LiteralPat(LiteralPat { syntax }), + MACRO_PAT => Pat::MacroPat(MacroPat { syntax }), _ => return None, }; Some(res) @@ -4721,6 +4760,7 @@ impl AstNode for Pat { Pat::SlicePat(it) => &it.syntax, Pat::RangePat(it) => &it.syntax, Pat::LiteralPat(it) => &it.syntax, + Pat::MacroPat(it) => &it.syntax, } } } -- cgit v1.2.3