From 82d9a77dade454ee8d09f198fa839e7755ff7bfb Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Sun, 6 Jan 2019 21:39:36 +0100 Subject: Touch up type inference for boolean operators Also try to infer its subexpressions and set type expectations whenever possible. --- crates/ra_syntax/src/ast.rs | 35 ++++++++++++++++++++++++----------- crates/ra_syntax/src/ast/generated.rs | 10 +--------- crates/ra_syntax/src/grammar.ron | 7 +------ 3 files changed, 26 insertions(+), 26 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 1bce6fa40..9df8ec663 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs @@ -511,20 +511,33 @@ impl<'a> BinExpr<'a> { pub fn op(&self) -> Option { self.syntax() .children() - .filter_map(|c| { - match c.kind() { - PIPEPIPE => Some(BinOp::BooleanOr), - AMPAMP => Some(BinOp::BooleanAnd), - EQEQ => Some(BinOp::EqualityTest), - LTEQ => Some(BinOp::LesserEqualTest), - GTEQ => Some(BinOp::GreaterEqualTest), - L_ANGLE => Some(BinOp::LesserTest), - R_ANGLE => Some(BinOp::GreaterTest), - _ => None, - } + .filter_map(|c| match c.kind() { + PIPEPIPE => Some(BinOp::BooleanOr), + AMPAMP => Some(BinOp::BooleanAnd), + EQEQ => Some(BinOp::EqualityTest), + LTEQ => Some(BinOp::LesserEqualTest), + GTEQ => Some(BinOp::GreaterEqualTest), + L_ANGLE => Some(BinOp::LesserTest), + R_ANGLE => Some(BinOp::GreaterTest), + _ => None, }) .next() } + + pub fn lhs(self) -> Option> { + children(self).nth(0) + } + + pub fn rhs(self) -> Option> { + children(self).nth(1) + } + + pub fn sub_exprs(self) -> (Option>, Option>) { + let mut children = children(self); + let first = children.next(); + let second = children.next(); + (first, second) + } } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index ac320606d..7df6a9c46 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -217,15 +217,7 @@ impl> BinExprNode { } -impl<'a> BinExpr<'a> { - pub fn lhs(self) -> Option> { - super::child_opt(self) - } - - pub fn rhs(self) -> Option> { - super::child_opt(self) - } -} +impl<'a> BinExpr<'a> {} // BindPat #[derive(Debug, Clone, Copy,)] diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index e59961da3..c55e9e07a 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -422,12 +422,7 @@ Grammar( "RefExpr": (options: ["Expr"]), "PrefixExpr": (options: ["Expr"]), "RangeExpr": (), - "BinExpr": ( - options: [ - ["lhs", "Expr"], - ["rhs", "Expr"] - ] - ), + "BinExpr": (), "String": (), "Byte": (), "ByteString": (), -- cgit v1.2.3