diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-07 20:05:44 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-07 20:05:44 +0000 |
commit | 812e47785b4f14a961f97414d0ca69d8c9bf5c9c (patch) | |
tree | f4e1936de76bee89502613ce038f4ecaef19ec4b | |
parent | e2592cf09087ae0a6cad5b588cbf1ab1161440e9 (diff) | |
parent | 5d15dd70b037b3d1623ebd83d8ef0f66ad6950af (diff) |
Merge #451
451: More type inference for more binary expressions r=flodiebold a=marcusklaas
Implements more of https://github.com/rust-analyzer/rust-analyzer/issues/390. Just works for primitive (numeric) types for now.
Found an issue where `let x: Ty = expr;` doesn't actually propagate the type information unless `Ty` is primitive and numeric. I'll open an issue for this.
Co-authored-by: Marcus Klaas de Vries <[email protected]>
-rw-r--r-- | crates/ra_hir/src/ty.rs | 87 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/binary_op.txt | 46 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests/data/boolean_op.txt | 31 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 70 | ||||
-rw-r--r-- | crates/ra_syntax/src/grammar.ron | 1 | ||||
-rw-r--r-- | crates/ra_syntax/src/syntax_kinds/generated.rs | 2 |
7 files changed, 200 insertions, 50 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index bba8527b7..7827e82c4 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -527,9 +527,7 @@ struct InferenceContext<'a, D: HirDatabase> { | |||
527 | return_ty: Ty, | 527 | return_ty: Ty, |
528 | } | 528 | } |
529 | 529 | ||
530 | // helper function that determines whether a binary operator | 530 | fn binary_op_return_ty(op: BinaryOp, rhs_ty: Ty) -> Ty { |
531 | // always returns a boolean | ||
532 | fn is_boolean_operator(op: BinaryOp) -> bool { | ||
533 | match op { | 531 | match op { |
534 | BinaryOp::BooleanOr | 532 | BinaryOp::BooleanOr |
535 | | BinaryOp::BooleanAnd | 533 | | BinaryOp::BooleanAnd |
@@ -537,7 +535,70 @@ fn is_boolean_operator(op: BinaryOp) -> bool { | |||
537 | | BinaryOp::LesserEqualTest | 535 | | BinaryOp::LesserEqualTest |
538 | | BinaryOp::GreaterEqualTest | 536 | | BinaryOp::GreaterEqualTest |
539 | | BinaryOp::LesserTest | 537 | | BinaryOp::LesserTest |
540 | | BinaryOp::GreaterTest => true, | 538 | | BinaryOp::GreaterTest => Ty::Bool, |
539 | BinaryOp::Assignment | ||
540 | | BinaryOp::AddAssign | ||
541 | | BinaryOp::SubAssign | ||
542 | | BinaryOp::DivAssign | ||
543 | | BinaryOp::MulAssign | ||
544 | | BinaryOp::RemAssign | ||
545 | | BinaryOp::ShrAssign | ||
546 | | BinaryOp::ShlAssign | ||
547 | | BinaryOp::BitAndAssign | ||
548 | | BinaryOp::BitOrAssign | ||
549 | | BinaryOp::BitXorAssign => Ty::unit(), | ||
550 | BinaryOp::Addition | ||
551 | | BinaryOp::Subtraction | ||
552 | | BinaryOp::Multiplication | ||
553 | | BinaryOp::Division | ||
554 | | BinaryOp::Remainder | ||
555 | | BinaryOp::LeftShift | ||
556 | | BinaryOp::RightShift | ||
557 | | BinaryOp::BitwiseAnd | ||
558 | | BinaryOp::BitwiseOr | ||
559 | | BinaryOp::BitwiseXor => match rhs_ty { | ||
560 | Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) => rhs_ty, | ||
561 | _ => Ty::Unknown, | ||
562 | }, | ||
563 | BinaryOp::RangeRightOpen | BinaryOp::RangeRightClosed => Ty::Unknown, | ||
564 | } | ||
565 | } | ||
566 | |||
567 | fn binary_op_rhs_expectation(op: BinaryOp, lhs_ty: Ty) -> Ty { | ||
568 | match op { | ||
569 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => Ty::Bool, | ||
570 | BinaryOp::Assignment | BinaryOp::EqualityTest => match lhs_ty { | ||
571 | Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) | Ty::Str | Ty::Char | Ty::Bool => lhs_ty, | ||
572 | _ => Ty::Unknown, | ||
573 | }, | ||
574 | BinaryOp::LesserEqualTest | ||
575 | | BinaryOp::GreaterEqualTest | ||
576 | | BinaryOp::LesserTest | ||
577 | | BinaryOp::GreaterTest | ||
578 | | BinaryOp::AddAssign | ||
579 | | BinaryOp::SubAssign | ||
580 | | BinaryOp::DivAssign | ||
581 | | BinaryOp::MulAssign | ||
582 | | BinaryOp::RemAssign | ||
583 | | BinaryOp::ShrAssign | ||
584 | | BinaryOp::ShlAssign | ||
585 | | BinaryOp::BitAndAssign | ||
586 | | BinaryOp::BitOrAssign | ||
587 | | BinaryOp::BitXorAssign | ||
588 | | BinaryOp::Addition | ||
589 | | BinaryOp::Subtraction | ||
590 | | BinaryOp::Multiplication | ||
591 | | BinaryOp::Division | ||
592 | | BinaryOp::Remainder | ||
593 | | BinaryOp::LeftShift | ||
594 | | BinaryOp::RightShift | ||
595 | | BinaryOp::BitwiseAnd | ||
596 | | BinaryOp::BitwiseOr | ||
597 | | BinaryOp::BitwiseXor => match lhs_ty { | ||
598 | Ty::Uint(..) | Ty::Int(..) | Ty::Float(..) => lhs_ty, | ||
599 | _ => Ty::Unknown, | ||
600 | }, | ||
601 | _ => Ty::Unknown, | ||
541 | } | 602 | } |
542 | } | 603 | } |
543 | 604 | ||
@@ -889,20 +950,20 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
889 | } | 950 | } |
890 | Expr::BinaryOp { lhs, rhs, op } => match op { | 951 | Expr::BinaryOp { lhs, rhs, op } => match op { |
891 | Some(op) => { | 952 | Some(op) => { |
892 | let subtype_expectation = match op { | 953 | let lhs_expectation = match op { |
893 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { | 954 | BinaryOp::BooleanAnd | BinaryOp::BooleanOr => { |
894 | Expectation::has_type(Ty::Bool) | 955 | Expectation::has_type(Ty::Bool) |
895 | } | 956 | } |
896 | _ => Expectation::none(), | 957 | _ => Expectation::none(), |
897 | }; | 958 | }; |
898 | let _lhs_ty = self.infer_expr(*lhs, &subtype_expectation)?; | 959 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation)?; |
899 | let _rhs_ty = self.infer_expr(*rhs, &subtype_expectation)?; | 960 | // TODO: find implementation of trait corresponding to operation |
900 | 961 | // symbol and resolve associated `Output` type | |
901 | if is_boolean_operator(*op) { | 962 | let rhs_expectation = binary_op_rhs_expectation(*op, lhs_ty); |
902 | Ty::Bool | 963 | let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation))?; |
903 | } else { | 964 | |
904 | Ty::Unknown | 965 | // TODO: similar as above, return ty is often associated trait type |
905 | } | 966 | binary_op_return_ty(*op, rhs_ty) |
906 | } | 967 | } |
907 | _ => Ty::Unknown, | 968 | _ => Ty::Unknown, |
908 | }, | 969 | }, |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index e6c7e225b..2749d740c 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -157,7 +157,7 @@ impl S { | |||
157 | } | 157 | } |
158 | 158 | ||
159 | #[test] | 159 | #[test] |
160 | fn infer_boolean_op() { | 160 | fn infer_binary_op() { |
161 | check_inference( | 161 | check_inference( |
162 | r#" | 162 | r#" |
163 | fn f(x: bool) -> i32 { | 163 | fn f(x: bool) -> i32 { |
@@ -168,15 +168,18 @@ fn test() { | |||
168 | let x = a && b; | 168 | let x = a && b; |
169 | let y = true || false; | 169 | let y = true || false; |
170 | let z = x == y; | 170 | let z = x == y; |
171 | let h = CONST_1 <= CONST_2; | 171 | let minus_forty: isize = -40isize; |
172 | let h = minus_forty <= CONST_2; | ||
172 | let c = f(z || y) + 5; | 173 | let c = f(z || y) + 5; |
173 | let d = b; | 174 | let d = b; |
174 | let e = 3i32 && "hello world"; | 175 | let g = minus_forty ^= i; |
176 | let ten: usize = 10; | ||
177 | let ten_is_eleven = ten == some_num; | ||
175 | 178 | ||
176 | 10 < 3 | 179 | ten < 3 |
177 | } | 180 | } |
178 | "#, | 181 | "#, |
179 | "boolean_op.txt", | 182 | "binary_op.txt", |
180 | ); | 183 | ); |
181 | } | 184 | } |
182 | 185 | ||
diff --git a/crates/ra_hir/src/ty/tests/data/binary_op.txt b/crates/ra_hir/src/ty/tests/data/binary_op.txt new file mode 100644 index 000000000..59c07ff43 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/binary_op.txt | |||
@@ -0,0 +1,46 @@ | |||
1 | [6; 7) 'x': [unknown] | ||
2 | [22; 34) '{ 0i32 }': i32 | ||
3 | [28; 32) '0i32': i32 | ||
4 | [46; 342) '{ ... < 3 }': bool | ||
5 | [56; 57) 'x': bool | ||
6 | [60; 61) 'a': bool | ||
7 | [60; 66) 'a && b': bool | ||
8 | [65; 66) 'b': bool | ||
9 | [76; 77) 'y': bool | ||
10 | [80; 84) 'true': bool | ||
11 | [80; 93) 'true || false': bool | ||
12 | [88; 93) 'false': bool | ||
13 | [103; 104) 'z': bool | ||
14 | [107; 108) 'x': bool | ||
15 | [107; 113) 'x == y': bool | ||
16 | [112; 113) 'y': bool | ||
17 | [123; 134) 'minus_forty': isize | ||
18 | [144; 152) '-40isize': isize | ||
19 | [145; 152) '40isize': [unknown] | ||
20 | [162; 163) 'h': bool | ||
21 | [166; 177) 'minus_forty': isize | ||
22 | [166; 188) 'minus_...ONST_2': bool | ||
23 | [181; 188) 'CONST_2': isize | ||
24 | [198; 199) 'c': i32 | ||
25 | [202; 203) 'f': fn([unknown],) -> i32 | ||
26 | [202; 211) 'f(z || y)': i32 | ||
27 | [202; 215) 'f(z || y) + 5': i32 | ||
28 | [204; 205) 'z': bool | ||
29 | [204; 210) 'z || y': bool | ||
30 | [209; 210) 'y': bool | ||
31 | [214; 215) '5': i32 | ||
32 | [225; 226) 'd': [unknown] | ||
33 | [229; 230) 'b': [unknown] | ||
34 | [240; 241) 'g': () | ||
35 | [244; 255) 'minus_forty': isize | ||
36 | [244; 260) 'minus_...y ^= i': () | ||
37 | [259; 260) 'i': isize | ||
38 | [270; 273) 'ten': usize | ||
39 | [283; 285) '10': usize | ||
40 | [295; 308) 'ten_is_eleven': bool | ||
41 | [311; 314) 'ten': usize | ||
42 | [311; 326) 'ten == some_num': bool | ||
43 | [318; 326) 'some_num': usize | ||
44 | [333; 336) 'ten': usize | ||
45 | [333; 340) 'ten < 3': bool | ||
46 | [339; 340) '3': usize | ||
diff --git a/crates/ra_hir/src/ty/tests/data/boolean_op.txt b/crates/ra_hir/src/ty/tests/data/boolean_op.txt deleted file mode 100644 index cce8d68fb..000000000 --- a/crates/ra_hir/src/ty/tests/data/boolean_op.txt +++ /dev/null | |||
@@ -1,31 +0,0 @@ | |||
1 | [6; 7) 'x': [unknown] | ||
2 | [22; 34) '{ 0i32 }': i32 | ||
3 | [28; 32) '0i32': i32 | ||
4 | [46; 237) '{ ... < 3 }': bool | ||
5 | [56; 57) 'x': bool | ||
6 | [60; 61) 'a': bool | ||
7 | [60; 66) 'a && b': bool | ||
8 | [65; 66) 'b': bool | ||
9 | [76; 77) 'y': bool | ||
10 | [80; 84) 'true': bool | ||
11 | [80; 93) 'true || false': bool | ||
12 | [88; 93) 'false': bool | ||
13 | [103; 104) 'z': bool | ||
14 | [107; 108) 'x': bool | ||
15 | [107; 113) 'x == y': bool | ||
16 | [112; 113) 'y': bool | ||
17 | [123; 124) 'h': bool | ||
18 | [127; 134) 'CONST_1': [unknown] | ||
19 | [127; 145) 'CONST_...ONST_2': bool | ||
20 | [138; 145) 'CONST_2': [unknown] | ||
21 | [155; 156) 'c': [unknown] | ||
22 | [159; 172) 'f(z || y) + 5': [unknown] | ||
23 | [182; 183) 'd': [unknown] | ||
24 | [186; 187) 'b': [unknown] | ||
25 | [197; 198) 'e': bool | ||
26 | [201; 205) '3i32': bool | ||
27 | [201; 222) '3i32 &...world"': bool | ||
28 | [209; 222) '"hello world"': bool | ||
29 | [229; 231) '10': [unknown] | ||
30 | [229; 235) '10 < 3': bool | ||
31 | [234; 235) '3': [unknown] | ||
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index 9df8ec663..9ab59738f 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -504,7 +504,52 @@ pub enum BinOp { | |||
504 | LesserTest, | 504 | LesserTest, |
505 | /// The `>` operator for comparison | 505 | /// The `>` operator for comparison |
506 | GreaterTest, | 506 | GreaterTest, |
507 | // TODO: lots of others | 507 | /// The `+` operator for addition |
508 | Addition, | ||
509 | /// The `*` operator for multiplication | ||
510 | Multiplication, | ||
511 | /// The `-` operator for subtraction | ||
512 | Subtraction, | ||
513 | /// The `/` operator for division | ||
514 | Division, | ||
515 | /// The `%` operator for remainder after division | ||
516 | Remainder, | ||
517 | /// The `<<` operator for left shift | ||
518 | LeftShift, | ||
519 | /// The `>>` operator for right shift | ||
520 | RightShift, | ||
521 | /// The `^` operator for bitwise XOR | ||
522 | BitwiseXor, | ||
523 | /// The `|` operator for bitwise OR | ||
524 | BitwiseOr, | ||
525 | /// The `&` operator for bitwise AND | ||
526 | BitwiseAnd, | ||
527 | /// The `..` operator for right-open ranges | ||
528 | RangeRightOpen, | ||
529 | /// The `..=` operator for right-closed ranges | ||
530 | RangeRightClosed, | ||
531 | /// The `=` operator for assignment | ||
532 | Assignment, | ||
533 | /// The `+=` operator for assignment after additon | ||
534 | AddAssign, | ||
535 | /// The `/=` operator for assignment after division | ||
536 | DivAssign, | ||
537 | /// The `*=` operator for assignment after multiplication | ||
538 | MulAssign, | ||
539 | /// The `%=` operator for assignment after remainders | ||
540 | RemAssign, | ||
541 | /// The `>>=` operator for assignment after shifting right | ||
542 | ShrAssign, | ||
543 | /// The `<<=` operator for assignment after shifting left | ||
544 | ShlAssign, | ||
545 | /// The `-=` operator for assignment after subtraction | ||
546 | SubAssign, | ||
547 | /// The `|=` operator for assignment after bitwise OR | ||
548 | BitOrAssign, | ||
549 | /// The `&=` operator for assignment after bitwise AND | ||
550 | BitAndAssign, | ||
551 | /// The `^=` operator for assignment after bitwise XOR | ||
552 | BitXorAssign, | ||
508 | } | 553 | } |
509 | 554 | ||
510 | impl<'a> BinExpr<'a> { | 555 | impl<'a> BinExpr<'a> { |
@@ -519,6 +564,29 @@ impl<'a> BinExpr<'a> { | |||
519 | GTEQ => Some(BinOp::GreaterEqualTest), | 564 | GTEQ => Some(BinOp::GreaterEqualTest), |
520 | L_ANGLE => Some(BinOp::LesserTest), | 565 | L_ANGLE => Some(BinOp::LesserTest), |
521 | R_ANGLE => Some(BinOp::GreaterTest), | 566 | R_ANGLE => Some(BinOp::GreaterTest), |
567 | PLUS => Some(BinOp::Addition), | ||
568 | STAR => Some(BinOp::Multiplication), | ||
569 | MINUS => Some(BinOp::Subtraction), | ||
570 | SLASH => Some(BinOp::Division), | ||
571 | PERCENT => Some(BinOp::Remainder), | ||
572 | SHL => Some(BinOp::LeftShift), | ||
573 | SHR => Some(BinOp::RightShift), | ||
574 | CARET => Some(BinOp::BitwiseXor), | ||
575 | PIPE => Some(BinOp::BitwiseOr), | ||
576 | AMP => Some(BinOp::BitwiseAnd), | ||
577 | DOTDOT => Some(BinOp::RangeRightOpen), | ||
578 | DOTDOTEQ => Some(BinOp::RangeRightClosed), | ||
579 | EQ => Some(BinOp::Assignment), | ||
580 | PLUSEQ => Some(BinOp::AddAssign), | ||
581 | SLASHEQ => Some(BinOp::DivAssign), | ||
582 | STAREQ => Some(BinOp::MulAssign), | ||
583 | PERCENTEQ => Some(BinOp::RemAssign), | ||
584 | SHREQ => Some(BinOp::ShrAssign), | ||
585 | SHLEQ => Some(BinOp::ShlAssign), | ||
586 | MINUSEQ => Some(BinOp::SubAssign), | ||
587 | PIPEEQ => Some(BinOp::BitOrAssign), | ||
588 | AMPEQ => Some(BinOp::BitAndAssign), | ||
589 | CARETEQ => Some(BinOp::BitXorAssign), | ||
522 | _ => None, | 590 | _ => None, |
523 | }) | 591 | }) |
524 | .next() | 592 | .next() |
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 3c640ed47..d7505ea06 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron | |||
@@ -49,6 +49,7 @@ Grammar( | |||
49 | ["^=", "CARETEQ"], | 49 | ["^=", "CARETEQ"], |
50 | ["/=", "SLASHEQ"], | 50 | ["/=", "SLASHEQ"], |
51 | ["*=", "STAREQ"], | 51 | ["*=", "STAREQ"], |
52 | ["%=", "PERCENTEQ"], | ||
52 | ["&&", "AMPAMP"], | 53 | ["&&", "AMPAMP"], |
53 | ["||", "PIPEPIPE"], | 54 | ["||", "PIPEPIPE"], |
54 | ["<<", "SHL"], | 55 | ["<<", "SHL"], |
diff --git a/crates/ra_syntax/src/syntax_kinds/generated.rs b/crates/ra_syntax/src/syntax_kinds/generated.rs index ef4588d93..830fac9f4 100644 --- a/crates/ra_syntax/src/syntax_kinds/generated.rs +++ b/crates/ra_syntax/src/syntax_kinds/generated.rs | |||
@@ -58,6 +58,7 @@ pub enum SyntaxKind { | |||
58 | CARETEQ, | 58 | CARETEQ, |
59 | SLASHEQ, | 59 | SLASHEQ, |
60 | STAREQ, | 60 | STAREQ, |
61 | PERCENTEQ, | ||
61 | AMPAMP, | 62 | AMPAMP, |
62 | PIPEPIPE, | 63 | PIPEPIPE, |
63 | SHL, | 64 | SHL, |
@@ -319,6 +320,7 @@ impl SyntaxKind { | |||
319 | CARETEQ => &SyntaxInfo { name: "CARETEQ" }, | 320 | CARETEQ => &SyntaxInfo { name: "CARETEQ" }, |
320 | SLASHEQ => &SyntaxInfo { name: "SLASHEQ" }, | 321 | SLASHEQ => &SyntaxInfo { name: "SLASHEQ" }, |
321 | STAREQ => &SyntaxInfo { name: "STAREQ" }, | 322 | STAREQ => &SyntaxInfo { name: "STAREQ" }, |
323 | PERCENTEQ => &SyntaxInfo { name: "PERCENTEQ" }, | ||
322 | AMPAMP => &SyntaxInfo { name: "AMPAMP" }, | 324 | AMPAMP => &SyntaxInfo { name: "AMPAMP" }, |
323 | PIPEPIPE => &SyntaxInfo { name: "PIPEPIPE" }, | 325 | PIPEPIPE => &SyntaxInfo { name: "PIPEPIPE" }, |
324 | SHL => &SyntaxInfo { name: "SHL" }, | 326 | SHL => &SyntaxInfo { name: "SHL" }, |