From a6146d35b1615cf5fb908b29f34e58bfde3bf96d Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Thu, 10 Jan 2019 13:54:58 +0100 Subject: Implement type inference for literals (WIP) --- crates/ra_hir/src/ty/tests.rs | 20 ++++++++++++++++++++ crates/ra_hir/src/ty/tests/data/basics.txt | 2 +- crates/ra_hir/src/ty/tests/data/literals.txt | 10 ++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 crates/ra_hir/src/ty/tests/data/literals.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 920188fc9..97d3d222f 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -132,6 +132,26 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { ); } +#[test] +fn infer_literals() { + check_inference( + r#" +fn test() { + 5i32; + "hello"; + b"bytes"; + 'c'; + b'b'; + 3.14; + 5000; + (0u32, -5isize); + [true, true, false] +} +"#, + "literals.txt", + ); +} + #[test] fn infer_backwards() { check_inference( diff --git a/crates/ra_hir/src/ty/tests/data/basics.txt b/crates/ra_hir/src/ty/tests/data/basics.txt index ac7faae0a..e02947ba8 100644 --- a/crates/ra_hir/src/ty/tests/data/basics.txt +++ b/crates/ra_hir/src/ty/tests/data/basics.txt @@ -9,5 +9,5 @@ [69; 70) 'd': &str [76; 82) '1usize': [unknown] [88; 94) '1isize': [unknown] -[100; 106) '"test"': [unknown] +[100; 106) '"test"': &str [112; 118) '1.0f32': [unknown] diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt new file mode 100644 index 000000000..8b22eee31 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -0,0 +1,10 @@ +[11; 135) '{ ...lse] }': () +[17; 21) '5i32': [unknown] +[27; 34) '"hello"': &str +[40; 48) 'b"bytes"': &[u8] +[54; 57) ''c'': char +[63; 67) 'b'b'': u8 +[73; 77) '3.14': [unknown] +[83; 87) '5000': [unknown] +[93; 108) '(0u32, -5isize)': [unknown] +[114; 133) '[true,...false]': () -- cgit v1.2.3 From 5f5dc20d85dead5fbd51d163451f796255c9faea Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Thu, 10 Jan 2019 16:03:15 +0100 Subject: Try implementing integer type inference (WIP) --- crates/ra_hir/src/ty/primitive.rs | 50 +++++++++++++++++++++++++++ crates/ra_hir/src/ty/tests/data/basics.txt | 4 +-- crates/ra_hir/src/ty/tests/data/binary_op.txt | 2 +- crates/ra_hir/src/ty/tests/data/let.txt | 4 +-- crates/ra_hir/src/ty/tests/data/literals.txt | 2 +- 5 files changed, 56 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs index 498d42d52..5741ca90d 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs @@ -2,6 +2,56 @@ use std::fmt; use crate::{Name, KnownName}; +#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] +pub enum UncertainIntTy { + Unknown, + Unsigned(UintTy), + Signed(IntTy), +} + +impl UncertainIntTy { + pub fn ty_to_string(&self) -> &'static str { + match *self { + UncertainIntTy::Unknown => "{integer}", + UncertainIntTy::Signed(ty) => ty.ty_to_string(), + UncertainIntTy::Unsigned(ty) => ty.ty_to_string(), + } + } + + pub fn from_name(name: &Name) -> Option { + if let Some(ty) = IntTy::from_name(name) { + Some(UncertainIntTy::Signed(ty)) + } else if let Some(ty) = UintTy::from_name(name) { + Some(UncertainIntTy::Unsigned(ty)) + } else { + None + } + } +} + +#[derive(Debug, Clone, Eq, PartialEq, Hash, Copy)] +pub enum UncertainFloatTy { + Unknown, + Known(FloatTy), +} + +impl UncertainFloatTy { + pub fn ty_to_string(&self) -> &'static str { + match *self { + UncertainFloatTy::Unknown => "{float}", + UncertainFloatTy::Known(ty) => ty.ty_to_string(), + } + } + + pub fn from_name(name: &Name) -> Option { + if let Some(ty) = FloatTy::from_name(name) { + Some(UncertainFloatTy::Known(ty)) + } else { + None + } + } +} + #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] pub enum IntTy { Isize, diff --git a/crates/ra_hir/src/ty/tests/data/basics.txt b/crates/ra_hir/src/ty/tests/data/basics.txt index e02947ba8..4a3b69b7e 100644 --- a/crates/ra_hir/src/ty/tests/data/basics.txt +++ b/crates/ra_hir/src/ty/tests/data/basics.txt @@ -7,7 +7,7 @@ [55; 56) 'b': isize [62; 63) 'c': ! [69; 70) 'd': &str -[76; 82) '1usize': [unknown] -[88; 94) '1isize': [unknown] +[76; 82) '1usize': usize +[88; 94) '1isize': isize [100; 106) '"test"': &str [112; 118) '1.0f32': [unknown] diff --git a/crates/ra_hir/src/ty/tests/data/binary_op.txt b/crates/ra_hir/src/ty/tests/data/binary_op.txt index 8a515ac5e..7fdb8a900 100644 --- a/crates/ra_hir/src/ty/tests/data/binary_op.txt +++ b/crates/ra_hir/src/ty/tests/data/binary_op.txt @@ -16,7 +16,7 @@ [112; 113) 'y': bool [123; 134) 'minus_forty': isize [144; 152) '-40isize': isize -[145; 152) '40isize': [unknown] +[145; 152) '40isize': isize [162; 163) 'h': bool [166; 177) 'minus_forty': isize [166; 188) 'minus_...ONST_2': bool diff --git a/crates/ra_hir/src/ty/tests/data/let.txt b/crates/ra_hir/src/ty/tests/data/let.txt index 30f4a2cf5..8815dba41 100644 --- a/crates/ra_hir/src/ty/tests/data/let.txt +++ b/crates/ra_hir/src/ty/tests/data/let.txt @@ -1,6 +1,6 @@ [11; 71) '{ ...= b; }': () -[21; 22) 'a': [unknown] -[25; 31) '1isize': [unknown] +[21; 22) 'a': isize +[25; 31) '1isize': isize [41; 42) 'b': usize [52; 53) '1': usize [63; 64) 'c': usize diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt index 8b22eee31..e139d57a8 100644 --- a/crates/ra_hir/src/ty/tests/data/literals.txt +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -1,5 +1,5 @@ [11; 135) '{ ...lse] }': () -[17; 21) '5i32': [unknown] +[17; 21) '5i32': i32 [27; 34) '"hello"': &str [40; 48) 'b"bytes"': &[u8] [54; 57) ''c'': char -- cgit v1.2.3 From 1574715be5d3fc7e07160708810dcbc9c1b01733 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Thu, 10 Jan 2019 18:08:54 +0100 Subject: Use type variables to determine exact type for ambiguous numeric literals --- crates/ra_hir/src/ty/tests.rs | 1 + crates/ra_hir/src/ty/tests/data/basics.txt | 2 +- crates/ra_hir/src/ty/tests/data/literals.txt | 9 +++++---- crates/ra_hir/src/ty/tests/data/struct.txt | 4 ++-- 4 files changed, 9 insertions(+), 7 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 97d3d222f..0c43415a6 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -145,6 +145,7 @@ fn test() { 3.14; 5000; (0u32, -5isize); + false; [true, true, false] } "#, diff --git a/crates/ra_hir/src/ty/tests/data/basics.txt b/crates/ra_hir/src/ty/tests/data/basics.txt index 4a3b69b7e..e65fe07aa 100644 --- a/crates/ra_hir/src/ty/tests/data/basics.txt +++ b/crates/ra_hir/src/ty/tests/data/basics.txt @@ -10,4 +10,4 @@ [76; 82) '1usize': usize [88; 94) '1isize': isize [100; 106) '"test"': &str -[112; 118) '1.0f32': [unknown] +[112; 118) '1.0f32': f32 diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt index e139d57a8..df435edd7 100644 --- a/crates/ra_hir/src/ty/tests/data/literals.txt +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -1,10 +1,11 @@ -[11; 135) '{ ...lse] }': () +[11; 146) '{ ...lse] }': () [17; 21) '5i32': i32 [27; 34) '"hello"': &str [40; 48) 'b"bytes"': &[u8] [54; 57) ''c'': char [63; 67) 'b'b'': u8 -[73; 77) '3.14': [unknown] -[83; 87) '5000': [unknown] +[73; 77) '3.14': {float} +[83; 87) '5000': {integer} [93; 108) '(0u32, -5isize)': [unknown] -[114; 133) '[true,...false]': () +[114; 119) 'false': bool +[125; 144) '[true,...false]': () diff --git a/crates/ra_hir/src/ty/tests/data/struct.txt b/crates/ra_hir/src/ty/tests/data/struct.txt index 7b324c82f..dcdf61363 100644 --- a/crates/ra_hir/src/ty/tests/data/struct.txt +++ b/crates/ra_hir/src/ty/tests/data/struct.txt @@ -2,14 +2,14 @@ [82; 83) 'c': [unknown] [86; 87) 'C': [unknown] [86; 90) 'C(1)': [unknown] -[88; 89) '1': [unknown] +[88; 89) '1': {integer} [96; 97) 'B': [unknown] [107; 108) 'a': A [114; 133) 'A { b:...C(1) }': A [121; 122) 'B': B [127; 128) 'C': [unknown] [127; 131) 'C(1)': C -[129; 130) '1': [unknown] +[129; 130) '1': {integer} [139; 140) 'a': A [139; 142) 'a.b': B [148; 149) 'a': A -- cgit v1.2.3 From 81bc8e4973fefd0ff31d08206c374fb58aa8b6e0 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Fri, 11 Jan 2019 09:47:31 +0100 Subject: don't try to treat arrays and tuples as literals --- crates/ra_hir/src/ty/tests.rs | 2 -- crates/ra_hir/src/ty/tests/data/literals.txt | 10 ++++------ crates/ra_hir/src/ty/tests/data/struct.txt | 4 ++-- 3 files changed, 6 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 0c43415a6..53ea99874 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -144,9 +144,7 @@ fn test() { b'b'; 3.14; 5000; - (0u32, -5isize); false; - [true, true, false] } "#, "literals.txt", diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt index df435edd7..8d6079c98 100644 --- a/crates/ra_hir/src/ty/tests/data/literals.txt +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -1,11 +1,9 @@ -[11; 146) '{ ...lse] }': () +[11; 101) '{ ...lse; }': () [17; 21) '5i32': i32 [27; 34) '"hello"': &str [40; 48) 'b"bytes"': &[u8] [54; 57) ''c'': char [63; 67) 'b'b'': u8 -[73; 77) '3.14': {float} -[83; 87) '5000': {integer} -[93; 108) '(0u32, -5isize)': [unknown] -[114; 119) 'false': bool -[125; 144) '[true,...false]': () +[73; 77) '3.14': f64 +[83; 87) '5000': i32 +[93; 98) 'false': bool diff --git a/crates/ra_hir/src/ty/tests/data/struct.txt b/crates/ra_hir/src/ty/tests/data/struct.txt index dcdf61363..be9e12d02 100644 --- a/crates/ra_hir/src/ty/tests/data/struct.txt +++ b/crates/ra_hir/src/ty/tests/data/struct.txt @@ -2,14 +2,14 @@ [82; 83) 'c': [unknown] [86; 87) 'C': [unknown] [86; 90) 'C(1)': [unknown] -[88; 89) '1': {integer} +[88; 89) '1': i32 [96; 97) 'B': [unknown] [107; 108) 'a': A [114; 133) 'A { b:...C(1) }': A [121; 122) 'B': B [127; 128) 'C': [unknown] [127; 131) 'C(1)': C -[129; 130) '1': {integer} +[129; 130) '1': i32 [139; 140) 'a': A [139; 142) 'a.b': B [148; 149) 'a': A -- cgit v1.2.3 From a9a6a50c759302e8a8d59bf6c53c72ec804324b3 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 14 Jan 2019 19:30:21 +0100 Subject: Fixup tests --- crates/ra_hir/src/ty/tests.rs | 5 +- crates/ra_hir/src/ty/tests/data/binary_op.txt | 86 +++++++++++++-------------- crates/ra_hir/src/ty/tests/data/literals.txt | 3 +- crates/ra_hir/src/ty/tests/data/tuple.txt | 18 +++--- 4 files changed, 56 insertions(+), 56 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 53ea99874..cbdb2a4b7 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -145,6 +145,7 @@ fn test() { 3.14; 5000; false; + true; } "#, "literals.txt", @@ -199,7 +200,7 @@ fn f(x: bool) -> i32 { 0i32 } -fn test() { +fn test() -> bool { let x = a && b; let y = true || false; let z = x == y; @@ -296,8 +297,6 @@ fn test(x: &str, y: isize) { let b = (a, x); let c = (y, x); let d = (c, x); - - // we have not infered these case yet. let e = (1, "e"); let f = (e, "d"); } diff --git a/crates/ra_hir/src/ty/tests/data/binary_op.txt b/crates/ra_hir/src/ty/tests/data/binary_op.txt index 7fdb8a900..58a727691 100644 --- a/crates/ra_hir/src/ty/tests/data/binary_op.txt +++ b/crates/ra_hir/src/ty/tests/data/binary_op.txt @@ -1,46 +1,46 @@ [6; 7) 'x': bool [22; 34) '{ 0i32 }': i32 [28; 32) '0i32': i32 -[46; 342) '{ ... < 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; 134) 'minus_forty': isize -[144; 152) '-40isize': isize -[145; 152) '40isize': isize -[162; 163) 'h': bool -[166; 177) 'minus_forty': isize -[166; 188) 'minus_...ONST_2': bool -[181; 188) 'CONST_2': isize -[198; 199) 'c': i32 -[202; 203) 'f': fn(bool) -> i32 -[202; 211) 'f(z || y)': i32 -[202; 215) 'f(z || y) + 5': i32 -[204; 205) 'z': bool -[204; 210) 'z || y': bool -[209; 210) 'y': bool -[214; 215) '5': i32 -[225; 226) 'd': [unknown] -[229; 230) 'b': [unknown] -[240; 241) 'g': () -[244; 255) 'minus_forty': isize -[244; 260) 'minus_...y ^= i': () -[259; 260) 'i': isize -[270; 273) 'ten': usize -[283; 285) '10': usize -[295; 308) 'ten_is_eleven': bool -[311; 314) 'ten': usize -[311; 326) 'ten == some_num': bool -[318; 326) 'some_num': usize -[333; 336) 'ten': usize -[333; 340) 'ten < 3': bool -[339; 340) '3': usize +[54; 350) '{ ... < 3 }': bool +[64; 65) 'x': bool +[68; 69) 'a': bool +[68; 74) 'a && b': bool +[73; 74) 'b': bool +[84; 85) 'y': bool +[88; 92) 'true': bool +[88; 101) 'true || false': bool +[96; 101) 'false': bool +[111; 112) 'z': bool +[115; 116) 'x': bool +[115; 121) 'x == y': bool +[120; 121) 'y': bool +[131; 142) 'minus_forty': isize +[152; 160) '-40isize': isize +[153; 160) '40isize': isize +[170; 171) 'h': bool +[174; 185) 'minus_forty': isize +[174; 196) 'minus_...ONST_2': bool +[189; 196) 'CONST_2': isize +[206; 207) 'c': i32 +[210; 211) 'f': fn(bool) -> i32 +[210; 219) 'f(z || y)': i32 +[210; 223) 'f(z || y) + 5': i32 +[212; 213) 'z': bool +[212; 218) 'z || y': bool +[217; 218) 'y': bool +[222; 223) '5': i32 +[233; 234) 'd': [unknown] +[237; 238) 'b': [unknown] +[248; 249) 'g': () +[252; 263) 'minus_forty': isize +[252; 268) 'minus_...y ^= i': () +[267; 268) 'i': isize +[278; 281) 'ten': usize +[291; 293) '10': usize +[303; 316) 'ten_is_eleven': bool +[319; 322) 'ten': usize +[319; 334) 'ten == some_num': bool +[326; 334) 'some_num': usize +[341; 344) 'ten': usize +[341; 348) 'ten < 3': bool +[347; 348) '3': usize diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt index 8d6079c98..6e82f458f 100644 --- a/crates/ra_hir/src/ty/tests/data/literals.txt +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -1,4 +1,4 @@ -[11; 101) '{ ...lse; }': () +[11; 111) '{ ...rue; }': () [17; 21) '5i32': i32 [27; 34) '"hello"': &str [40; 48) 'b"bytes"': &[u8] @@ -7,3 +7,4 @@ [73; 77) '3.14': f64 [83; 87) '5000': i32 [93; 98) 'false': bool +[104; 108) 'true': bool diff --git a/crates/ra_hir/src/ty/tests/data/tuple.txt b/crates/ra_hir/src/ty/tests/data/tuple.txt index 96169180d..a95d3c286 100644 --- a/crates/ra_hir/src/ty/tests/data/tuple.txt +++ b/crates/ra_hir/src/ty/tests/data/tuple.txt @@ -1,6 +1,6 @@ [9; 10) 'x': &str [18; 19) 'y': isize -[28; 214) '{ ...d"); }': () +[28; 170) '{ ...d"); }': () [38; 39) 'a': (u32, &str) [55; 63) '(1, "a")': (u32, &str) [56; 57) '1': u32 @@ -17,11 +17,11 @@ [117; 123) '(c, x)': ((isize, &str), &str) [118; 119) 'c': (isize, &str) [121; 122) 'x': &str -[177; 178) 'e': ([unknown], [unknown]) -[181; 189) '(1, "e")': ([unknown], [unknown]) -[182; 183) '1': [unknown] -[185; 188) '"e"': [unknown] -[199; 200) 'f': (([unknown], [unknown]), [unknown]) -[203; 211) '(e, "d")': (([unknown], [unknown]), [unknown]) -[204; 205) 'e': ([unknown], [unknown]) -[207; 210) '"d"': [unknown] +[133; 134) 'e': (i32, &str) +[137; 145) '(1, "e")': (i32, &str) +[138; 139) '1': i32 +[141; 144) '"e"': &str +[155; 156) 'f': ((i32, &str), &str) +[159; 167) '(e, "d")': ((i32, &str), &str) +[160; 161) 'e': (i32, &str) +[163; 166) '"d"': &str -- cgit v1.2.3 From d67eabb512a08a451a649c7f20e4e9ae1860a8a0 Mon Sep 17 00:00:00 2001 From: Marcus Klaas de Vries Date: Mon, 14 Jan 2019 20:56:14 +0100 Subject: Fix type inference for raw (byte) strings --- crates/ra_hir/src/ty/tests.rs | 12 +++++++++--- crates/ra_hir/src/ty/tests/data/literals.txt | 4 +++- 2 files changed, 12 insertions(+), 4 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 cbdb2a4b7..8aacb1a7f 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -135,7 +135,7 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) { #[test] fn infer_literals() { check_inference( - r#" + r##" fn test() { 5i32; "hello"; @@ -146,8 +146,14 @@ fn test() { 5000; false; true; -} -"#, + r#" + //! doc + // non-doc + mod foo {} + "#; + br#"yolo"#; +} +"##, "literals.txt", ); } diff --git a/crates/ra_hir/src/ty/tests/data/literals.txt b/crates/ra_hir/src/ty/tests/data/literals.txt index 6e82f458f..84ee2c11b 100644 --- a/crates/ra_hir/src/ty/tests/data/literals.txt +++ b/crates/ra_hir/src/ty/tests/data/literals.txt @@ -1,4 +1,4 @@ -[11; 111) '{ ...rue; }': () +[11; 201) '{ ...o"#; }': () [17; 21) '5i32': i32 [27; 34) '"hello"': &str [40; 48) 'b"bytes"': &[u8] @@ -8,3 +8,5 @@ [83; 87) '5000': i32 [93; 98) 'false': bool [104; 108) 'true': bool +[114; 182) 'r#" ... "#': &str +[188; 198) 'br#"yolo"#': &[u8] -- cgit v1.2.3