mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Implement type inference for more binary operators
Mostly just for primitive numeric types such as u32 and f64. Not yet a general solution using trait resolution.
This commit is contained in:
parent
3238c06a5a
commit
7b0eaef580
4 changed files with 84 additions and 49 deletions
|
@ -529,7 +529,7 @@ struct InferenceContext<'a, D: HirDatabase> {
|
||||||
|
|
||||||
// helper function that determines whether a binary operator
|
// helper function that determines whether a binary operator
|
||||||
// always returns a boolean
|
// always returns a boolean
|
||||||
fn is_boolean_operator(op: BinaryOp) -> bool {
|
fn boolean_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty {
|
||||||
match op {
|
match op {
|
||||||
BinaryOp::BooleanOr
|
BinaryOp::BooleanOr
|
||||||
| BinaryOp::BooleanAnd
|
| BinaryOp::BooleanAnd
|
||||||
|
@ -537,8 +537,32 @@ fn is_boolean_operator(op: BinaryOp) -> bool {
|
||||||
| BinaryOp::LesserEqualTest
|
| BinaryOp::LesserEqualTest
|
||||||
| BinaryOp::GreaterEqualTest
|
| BinaryOp::GreaterEqualTest
|
||||||
| BinaryOp::LesserTest
|
| BinaryOp::LesserTest
|
||||||
| BinaryOp::GreaterTest => true,
|
| BinaryOp::GreaterTest => Ty::Bool,
|
||||||
_ => false,
|
BinaryOp::Assignment
|
||||||
|
| BinaryOp::AddAssign
|
||||||
|
| BinaryOp::SubAssign
|
||||||
|
| BinaryOp::DivAssign
|
||||||
|
| BinaryOp::MulAssign
|
||||||
|
| BinaryOp::RemAssign
|
||||||
|
| BinaryOp::ShrAssign
|
||||||
|
| BinaryOp::ShlAssign
|
||||||
|
| BinaryOp::BitAndAssign
|
||||||
|
| BinaryOp::BitOrAssign
|
||||||
|
| BinaryOp::BitXorAssign => Ty::unit(),
|
||||||
|
BinaryOp::Addition
|
||||||
|
| BinaryOp::Subtraction
|
||||||
|
| BinaryOp::Multiplication
|
||||||
|
| BinaryOp::Division
|
||||||
|
| BinaryOp::Remainder
|
||||||
|
| BinaryOp::LeftShift
|
||||||
|
| BinaryOp::RightShift
|
||||||
|
| BinaryOp::BitwiseAnd
|
||||||
|
| BinaryOp::BitwiseOr
|
||||||
|
| BinaryOp::BitwiseXor => match rhs_ty {
|
||||||
|
Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) => rhs_ty,
|
||||||
|
_ => Ty::Unknown,
|
||||||
|
},
|
||||||
|
BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -890,20 +914,59 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
}
|
}
|
||||||
Expr::BinaryOp { lhs, rhs, op } => match op {
|
Expr::BinaryOp { lhs, rhs, op } => match op {
|
||||||
Some(op) => {
|
Some(op) => {
|
||||||
let subtype_expectation = match op {
|
let lhs_expectation = match op {
|
||||||
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => {
|
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => {
|
||||||
Expectation::has_type(Ty::Bool)
|
Expectation::has_type(Ty::Bool)
|
||||||
}
|
}
|
||||||
_ => Expectation::none(),
|
_ => Expectation::none(),
|
||||||
};
|
};
|
||||||
let _lhs_ty = self.infer_expr(*lhs, &subtype_expectation)?;
|
let lhs_ty = self.infer_expr(*lhs, &lhs_expectation)?;
|
||||||
let _rhs_ty = self.infer_expr(*rhs, &subtype_expectation)?;
|
// TODO: find implementation of trait corresponding to operation
|
||||||
|
// symbol and resolve associated `Output` type
|
||||||
|
let rhs_expectation = match op {
|
||||||
|
BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::Bool,
|
||||||
|
BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty {
|
||||||
|
Ty::Uint(..)
|
||||||
|
| Ty::Int(..)
|
||||||
|
| Ty::Float(..)
|
||||||
|
| Ty::Str
|
||||||
|
| Ty::Char
|
||||||
|
| Ty::Bool => lhs_ty,
|
||||||
|
_ => Ty::Unknown,
|
||||||
|
},
|
||||||
|
BinaryOp::LesserEqualTest
|
||||||
|
| BinaryOp::GreaterEqualTest
|
||||||
|
| BinaryOp::LesserTest
|
||||||
|
| BinaryOp::GreaterTest
|
||||||
|
| BinaryOp::AddAssign
|
||||||
|
| BinaryOp::SubAssign
|
||||||
|
| BinaryOp::DivAssign
|
||||||
|
| BinaryOp::MulAssign
|
||||||
|
| BinaryOp::RemAssign
|
||||||
|
| BinaryOp::ShrAssign
|
||||||
|
| BinaryOp::ShlAssign
|
||||||
|
| BinaryOp::BitAndAssign
|
||||||
|
| BinaryOp::BitOrAssign
|
||||||
|
| BinaryOp::BitXorAssign
|
||||||
|
| BinaryOp::Addition
|
||||||
|
| BinaryOp::Subtraction
|
||||||
|
| BinaryOp::Multiplication
|
||||||
|
| BinaryOp::Division
|
||||||
|
| BinaryOp::Remainder
|
||||||
|
| BinaryOp::LeftShift
|
||||||
|
| BinaryOp::RightShift
|
||||||
|
| BinaryOp::BitwiseAnd
|
||||||
|
| BinaryOp::BitwiseOr
|
||||||
|
| BinaryOp::BitwiseXor => match lhs_ty {
|
||||||
|
Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) => lhs_ty,
|
||||||
|
_ => Ty::Unknown,
|
||||||
|
},
|
||||||
|
_ => Ty::Unknown,
|
||||||
|
};
|
||||||
|
let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation))?;
|
||||||
|
|
||||||
if is_boolean_operator(*op) {
|
// TODO: similar as above, return ty is often associated trait type
|
||||||
Ty::Bool
|
boolean_op_return_ty(*op, rhs_ty)
|
||||||
} else {
|
|
||||||
Ty::Unknown
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_ => Ty::Unknown,
|
_ => Ty::Unknown,
|
||||||
},
|
},
|
||||||
|
|
|
@ -157,7 +157,7 @@ impl S {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn infer_boolean_op() {
|
fn infer_binary_op() {
|
||||||
check_inference(
|
check_inference(
|
||||||
r#"
|
r#"
|
||||||
fn f(x: bool) -> i32 {
|
fn f(x: bool) -> i32 {
|
||||||
|
@ -168,15 +168,18 @@ fn test() {
|
||||||
let x = a && b;
|
let x = a && b;
|
||||||
let y = true || false;
|
let y = true || false;
|
||||||
let z = x == y;
|
let z = x == y;
|
||||||
let h = CONST_1 <= CONST_2;
|
let minus_forty: isize = -40isize;
|
||||||
|
let h = minus_forty <= CONST_2;
|
||||||
let c = f(z || y) + 5;
|
let c = f(z || y) + 5;
|
||||||
let d = b;
|
let d = b;
|
||||||
let e = 3i32 && "hello world";
|
let g = minus_forty ^= i;
|
||||||
|
let ten: usize = 10;
|
||||||
|
let ten_is_eleven = ten == some_num;
|
||||||
|
|
||||||
10 < 3
|
ten < 3
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
"boolean_op.txt",
|
"binary_op.txt",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,31 +0,0 @@
|
||||||
[6; 7) 'x': [unknown]
|
|
||||||
[22; 34) '{ 0i32 }': i32
|
|
||||||
[28; 32) '0i32': i32
|
|
||||||
[46; 237) '{ ... < 3 }': bool
|
|
||||||
[56; 57) 'x': bool
|
|
||||||
[60; 61) 'a': bool
|
|
||||||
[60; 66) 'a && b': bool
|
|
||||||
[65; 66) 'b': bool
|
|
||||||
[76; 77) 'y': bool
|
|
||||||
[80; 84) 'true': bool
|
|
||||||
[80; 93) 'true || false': bool
|
|
||||||
[88; 93) 'false': bool
|
|
||||||
[103; 104) 'z': bool
|
|
||||||
[107; 108) 'x': bool
|
|
||||||
[107; 113) 'x == y': bool
|
|
||||||
[112; 113) 'y': bool
|
|
||||||
[123; 124) 'h': bool
|
|
||||||
[127; 134) 'CONST_1': [unknown]
|
|
||||||
[127; 145) 'CONST_...ONST_2': bool
|
|
||||||
[138; 145) 'CONST_2': [unknown]
|
|
||||||
[155; 156) 'c': [unknown]
|
|
||||||
[159; 172) 'f(z || y) + 5': [unknown]
|
|
||||||
[182; 183) 'd': [unknown]
|
|
||||||
[186; 187) 'b': [unknown]
|
|
||||||
[197; 198) 'e': bool
|
|
||||||
[201; 205) '3i32': bool
|
|
||||||
[201; 222) '3i32 &...world"': bool
|
|
||||||
[209; 222) '"hello world"': bool
|
|
||||||
[229; 231) '10': [unknown]
|
|
||||||
[229; 235) '10 < 3': bool
|
|
||||||
[234; 235) '3': [unknown]
|
|
|
@ -549,7 +549,7 @@ pub enum BinOp {
|
||||||
/// The `&=` operator for assignment after bitwise AND
|
/// The `&=` operator for assignment after bitwise AND
|
||||||
BitAndAssign,
|
BitAndAssign,
|
||||||
/// The `^=` operator for assignment after bitwise XOR
|
/// The `^=` operator for assignment after bitwise XOR
|
||||||
BitXorAssin,
|
BitXorAssign,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> BinExpr<'a> {
|
impl<'a> BinExpr<'a> {
|
||||||
|
@ -586,7 +586,7 @@ impl<'a> BinExpr<'a> {
|
||||||
MINUSEQ => Some(BinOp::SubAssign),
|
MINUSEQ => Some(BinOp::SubAssign),
|
||||||
PIPEEQ => Some(BinOp::BitOrAssign),
|
PIPEEQ => Some(BinOp::BitOrAssign),
|
||||||
AMPEQ => Some(BinOp::BitAndAssign),
|
AMPEQ => Some(BinOp::BitAndAssign),
|
||||||
CARETEQ => Some(BinOp::BitXorAssin),
|
CARETEQ => Some(BinOp::BitXorAssign),
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.next()
|
.next()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue