From 4fc233a02e8dc07619a969400c445ec47c2b1a9d Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Sat, 5 Jan 2019 21:28:30 +0100 Subject: Implement type inference for boolean operators --- crates/ra_hir/src/ty/tests.rs | 17 +++++++++++++++++ crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt | 10 ++++++++++ 2 files changed, 27 insertions(+) create mode 100644 crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index fb53fcf0b..97c466890 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -153,6 +153,23 @@ impl S { ); } +#[test] +fn infer_boolean_op() { + check_inference( + r#" +fn test() { + let x = a && b; + let y = true || false; + let z = x == y; + let h = CONST_1 <= CONST_2; + + 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); diff --git a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt new file mode 100644 index 000000000..cc07cdccb --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt @@ -0,0 +1,10 @@ +[21; 22) 'x': bool +[68; 69) 'z': bool +[72; 78) 'x == y': bool +[45; 58) 'true || false': bool +[11; 125) '{ ... < 3 }': bool +[117; 123) '10 < 3': bool +[88; 89) 'h': bool +[41; 42) 'y': bool +[92; 110) 'CONST_...ONST_2': bool +[25; 31) 'a && b': bool -- cgit v1.2.3 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_hir/src/ty/tests.rs | 7 ++++ .../ra_hir/src/ty/tests/data/0008_boolean_op.txt | 41 ++++++++++++++++------ 2 files changed, 38 insertions(+), 10 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 97c466890..1650606b7 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -157,11 +157,18 @@ impl S { 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 } diff --git a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt index cc07cdccb..ca01ad159 100644 --- a/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt +++ b/crates/ra_hir/src/ty/tests/data/0008_boolean_op.txt @@ -1,10 +1,31 @@ -[21; 22) 'x': bool -[68; 69) 'z': bool -[72; 78) 'x == y': bool -[45; 58) 'true || false': bool -[11; 125) '{ ... < 3 }': bool -[117; 123) '10 < 3': bool -[88; 89) 'h': bool -[41; 42) 'y': bool -[92; 110) 'CONST_...ONST_2': bool -[25; 31) 'a && b': bool +[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 -- cgit v1.2.3