From ab5deb78117693d776723bc0144e7b34e6f782d1 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Wed, 16 Jan 2019 19:35:27 +0100 Subject: Create struct patterns up to the hir level --- crates/ra_hir/src/expr.rs | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'crates/ra_hir/src/expr.rs') diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 8f7e75309..c6d442ec4 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -329,13 +329,23 @@ impl Expr { pub struct PatId(RawId); impl_arena_id!(PatId); +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct FieldPat { + name: Name, + pat: Option, +} + /// Close relative to rustc's hir::PatKind #[derive(Debug, Clone, Eq, PartialEq)] pub enum Pat { Missing, // do we need this? Wild, Tuple(Vec), - Struct, // TODO + Struct { + path: Option, + args: Vec, + // TODO: 'ellipsis' option + }, Range { start: ExprId, end: ExprId, @@ -802,11 +812,30 @@ impl ExprCollector { Pat::Tuple(args) } ast::PatKind::PlaceholderPat(_) => Pat::Wild, + ast::PatKind::StructPat(p) => { + let path = p.path().and_then(Path::from_ast); + + if let Some(field_list) = p.field_pat_list() { + let fields = field_list + .field_pats() + .into_iter() + .map(|f| FieldPat { + name: Name::new(f.ident), + pat: f.pat.as_ref().map(|p| self.collect_pat(p)), + }) + .collect(); + + Pat::Struct { + path: path, + args: fields, + } + } else { + Pat::Missing + } + } + // TODO: implement - ast::PatKind::FieldPatList(_) - | ast::PatKind::SlicePat(_) - | ast::PatKind::StructPat(_) - | ast::PatKind::RangePat(_) => Pat::Missing, + ast::PatKind::SlicePat(_) | ast::PatKind::RangePat(_) => Pat::Missing, }; let syntax_ptr = LocalSyntaxPtr::new(pat.syntax()); self.alloc_pat(pattern, syntax_ptr) -- cgit v1.2.3