From 3daca3eb4d843199540edfb1092f57f49938d0f6 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 28 Jan 2019 23:06:11 +0100 Subject: Infer type of match guard --- crates/ra_syntax/src/ast/generated.rs | 6 +++++- crates/ra_syntax/src/grammar.ron | 2 +- crates/ra_syntax/src/grammar/expressions/atom.rs | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src') diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 3ace6533c..4f5a96014 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -1981,7 +1981,11 @@ impl ToOwned for MatchGuard { } -impl MatchGuard {} +impl MatchGuard { + pub fn expr(&self) -> Option<&Expr> { + super::child_opt(self) + } +} // MethodCallExpr #[derive(Debug, PartialEq, Eq, Hash)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 85fc79038..e4cad4eb3 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -418,7 +418,7 @@ Grammar( ], collections: [ [ "pats", "Pat" ] ] ), - "MatchGuard": (), + "MatchGuard": (options: ["Expr"]), "StructLit": (options: ["Path", "NamedFieldList", ["spread", "Expr"]]), "NamedFieldList": (collections: [ ["fields", "NamedField"] ]), "NamedField": (options: ["NameRef", "Expr"]), diff --git a/crates/ra_syntax/src/grammar/expressions/atom.rs b/crates/ra_syntax/src/grammar/expressions/atom.rs index 6d6d89f70..600774afd 100644 --- a/crates/ra_syntax/src/grammar/expressions/atom.rs +++ b/crates/ra_syntax/src/grammar/expressions/atom.rs @@ -360,8 +360,8 @@ fn match_arm(p: &mut Parser) -> BlockLike { while p.eat(PIPE) { patterns::pattern(p); } - if p.eat(IF_KW) { - expr(p); + if p.at(IF_KW) { + match_guard(p); } p.expect(FAT_ARROW); let ret = expr_stmt(p); @@ -369,6 +369,20 @@ fn match_arm(p: &mut Parser) -> BlockLike { ret } +// test match_guard +// fn foo() { +// match () { +// _ if foo => (), +// } +// } +fn match_guard(p: &mut Parser) -> CompletedMarker { + assert!(p.at(IF_KW)); + let m = p.start(); + p.bump(); + expr(p); + m.complete(p, MATCH_GUARD) +} + // test block_expr // fn foo() { // {}; -- cgit v1.2.3