440: Implement type inference for boolean operators r=flodiebold a=marcusklaas

Tried implementing the easiest part of https://github.com/rust-analyzer/rust-analyzer/issues/390. Hope this is somewhat close to what the intent of the issue was. Found it surprisingly easy to find my way around the repository - it's well organized!

Very grateful for any pointers.

Co-authored-by: Marcus Klaas de Vries <mail@marcusklaas.nl>
This commit is contained in:
bors[bot] 2019-01-06 21:28:36 +00:00
commit 31c1999505
4 changed files with 140 additions and 2 deletions

View File

@ -26,7 +26,7 @@
use ra_db::{LocalSyntaxPtr, Cancelable};
use ra_syntax::{
ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp},
ast::{self, AstNode, LoopBodyOwner, ArgListOwner, PrefixOp, BinOp},
SyntaxNodeRef
};
@ -527,6 +527,20 @@ struct InferenceContext<'a, D: HirDatabase> {
return_ty: Ty,
}
// helper function that determines whether a binary operator
// always returns a boolean
fn is_boolean_operator(op: BinOp) -> bool {
match op {
BinOp::BooleanOr
| BinOp::BooleanAnd
| BinOp::EqualityTest
| BinOp::LesserEqualTest
| BinOp::GreaterEqualTest
| BinOp::LesserTest
| BinOp::GreaterTest => true,
}
}
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
fn new(
db: &'a D,
@ -899,7 +913,24 @@ fn infer_expr(&mut self, expr: ast::Expr, expected: &Expectation) -> Cancelable<
}
}
ast::Expr::RangeExpr(_e) => Ty::Unknown,
ast::Expr::BinExpr(_e) => Ty::Unknown,
ast::Expr::BinExpr(e) => match e.op() {
Some(op) => {
let subtype_expectation = match op {
BinOp::BooleanAnd | BinOp::BooleanOr => Expectation::has_type(Ty::Bool),
_ => Expectation::none(),
};
let (lhs, rhs) = e.sub_exprs();
let _lhs_ty = self.infer_expr_opt(lhs, &subtype_expectation)?;
let _rhs_ty = self.infer_expr_opt(rhs, &subtype_expectation)?;
if is_boolean_operator(op) {
Ty::Bool
} else {
Ty::Unknown
}
}
_ => Ty::Unknown,
},
ast::Expr::Literal(_e) => Ty::Unknown,
};
// use a new type variable if we got Ty::Unknown here

View File

@ -156,6 +156,30 @@ fn test2(self: &Self) {
);
}
#[test]
fn infer_boolean_op() {
check_inference(
r#"
fn f(x: bool) -> i32 {
0i32
}
fn test() {
let x = a && b;
let y = true || false;
let z = x == y;
let h = CONST_1 <= CONST_2;
let c = f(z || y) + 5;
let d = b;
let e = 3i32 && "hello world";
10 < 3
}
"#,
"0008_boolean_op.txt",
);
}
fn infer(content: &str) -> String {
let (db, _, file_id) = MockDatabase::with_single_file(content);
let source_file = db.source_file(file_id);

View File

@ -0,0 +1,31 @@
[28; 32) '0i32': i32
[22; 34) '{ 0i32 }': i32
[6; 7) 'x': [unknown]
[127; 134) 'CONST_1': [unknown]
[201; 205) '3i32': bool
[76; 77) 'y': bool
[65; 66) 'b': bool
[60; 66) 'a && b': bool
[127; 145) 'CONST_...ONST_2': bool
[182; 183) 'd': [unknown]
[229; 231) '10': [unknown]
[209; 222) '"hello world"': bool
[229; 235) '10 < 3': bool
[186; 187) 'b': [unknown]
[159; 172) 'f(z || y) + 5': [unknown]
[56; 57) 'x': bool
[112; 113) 'y': bool
[201; 222) '3i32 &...world"': bool
[234; 235) '3': [unknown]
[138; 145) 'CONST_2': [unknown]
[80; 93) 'true || false': bool
[46; 237) '{ ... < 3 }': bool
[197; 198) 'e': bool
[107; 113) 'x == y': bool
[88; 93) 'false': bool
[80; 84) 'true': bool
[123; 124) 'h': bool
[155; 156) 'c': [unknown]
[103; 104) 'z': bool
[60; 61) 'a': bool
[107; 108) 'x': bool

View File

@ -488,6 +488,58 @@ pub fn op(&self) -> Option<PrefixOp> {
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum BinOp {
/// The `||` operator for boolean OR
BooleanOr,
/// The `&&` operator for boolean AND
BooleanAnd,
/// The `==` operator for equality testing
EqualityTest,
/// The `<=` operator for lesser-equal testing
LesserEqualTest,
/// The `>=` operator for greater-equal testing
GreaterEqualTest,
/// The `<` operator for comparison
LesserTest,
/// The `>` operator for comparison
GreaterTest,
// TODO: lots of others
}
impl<'a> BinExpr<'a> {
pub fn op(&self) -> Option<BinOp> {
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,
})
.next()
}
pub fn lhs(self) -> Option<Expr<'a>> {
children(self).nth(0)
}
pub fn rhs(self) -> Option<Expr<'a>> {
children(self).nth(1)
}
pub fn sub_exprs(self) -> (Option<Expr<'a>>, Option<Expr<'a>>) {
let mut children = children(self);
let first = children.next();
let second = children.next();
(first, second)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum SelfParamFlavor {
/// self