aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/ty
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r--crates/ra_hir/src/ty/primitive.rs42
-rw-r--r--crates/ra_hir/src/ty/tests.rs21
-rw-r--r--crates/ra_hir/src/ty/tests/data/0002_let.txt2
-rw-r--r--crates/ra_hir/src/ty/tests/data/0003_paths.txt8
-rw-r--r--crates/ra_hir/src/ty/tests/data/0004_struct.txt4
-rw-r--r--crates/ra_hir/src/ty/tests/data/0006_backwards.txt20
6 files changed, 70 insertions, 27 deletions
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs
index ad79b17e4..498d42d52 100644
--- a/crates/ra_hir/src/ty/primitive.rs
+++ b/crates/ra_hir/src/ty/primitive.rs
@@ -1,5 +1,7 @@
1use std::fmt; 1use std::fmt;
2 2
3use crate::{Name, KnownName};
4
3#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] 5#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
4pub enum IntTy { 6pub enum IntTy {
5 Isize, 7 Isize,
@@ -34,14 +36,14 @@ impl IntTy {
34 } 36 }
35 } 37 }
36 38
37 pub fn from_string(s: &str) -> Option<IntTy> { 39 pub fn from_name(name: &Name) -> Option<IntTy> {
38 match s { 40 match name.as_known_name()? {
39 "isize" => Some(IntTy::Isize), 41 KnownName::Isize => Some(IntTy::Isize),
40 "i8" => Some(IntTy::I8), 42 KnownName::I8 => Some(IntTy::I8),
41 "i16" => Some(IntTy::I16), 43 KnownName::I16 => Some(IntTy::I16),
42 "i32" => Some(IntTy::I32), 44 KnownName::I32 => Some(IntTy::I32),
43 "i64" => Some(IntTy::I64), 45 KnownName::I64 => Some(IntTy::I64),
44 "i128" => Some(IntTy::I128), 46 KnownName::I128 => Some(IntTy::I128),
45 _ => None, 47 _ => None,
46 } 48 }
47 } 49 }
@@ -69,14 +71,14 @@ impl UintTy {
69 } 71 }
70 } 72 }
71 73
72 pub fn from_string(s: &str) -> Option<UintTy> { 74 pub fn from_name(name: &Name) -> Option<UintTy> {
73 match s { 75 match name.as_known_name()? {
74 "usize" => Some(UintTy::Usize), 76 KnownName::Usize => Some(UintTy::Usize),
75 "u8" => Some(UintTy::U8), 77 KnownName::U8 => Some(UintTy::U8),
76 "u16" => Some(UintTy::U16), 78 KnownName::U16 => Some(UintTy::U16),
77 "u32" => Some(UintTy::U32), 79 KnownName::U32 => Some(UintTy::U32),
78 "u64" => Some(UintTy::U64), 80 KnownName::U64 => Some(UintTy::U64),
79 "u128" => Some(UintTy::U128), 81 KnownName::U128 => Some(UintTy::U128),
80 _ => None, 82 _ => None,
81 } 83 }
82 } 84 }
@@ -120,10 +122,10 @@ impl FloatTy {
120 } 122 }
121 } 123 }
122 124
123 pub fn from_string(s: &str) -> Option<FloatTy> { 125 pub fn from_name(name: &Name) -> Option<FloatTy> {
124 match s { 126 match name.as_known_name()? {
125 "f32" => Some(FloatTy::F32), 127 KnownName::F32 => Some(FloatTy::F32),
126 "f64" => Some(FloatTy::F64), 128 KnownName::F64 => Some(FloatTy::F64),
127 _ => None, 129 _ => None,
128 } 130 }
129 } 131 }
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs
index a76925b58..93bf431c4 100644
--- a/crates/ra_hir/src/ty/tests.rs
+++ b/crates/ra_hir/src/ty/tests.rs
@@ -113,6 +113,27 @@ fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
113 ); 113 );
114} 114}
115 115
116#[test]
117fn infer_backwards() {
118 check_inference(
119 r#"
120fn takes_u32(x: u32) {}
121
122struct S { i32_field: i32 }
123
124fn test() -> &mut &f64 {
125 let a = unknown_function();
126 takes_u32(a);
127 let b = unknown_function();
128 S { i32_field: b };
129 let c = unknown_function();
130 &mut &c
131}
132"#,
133 "0006_backwards.txt",
134 );
135}
136
116fn infer(content: &str) -> String { 137fn infer(content: &str) -> String {
117 let (db, _, file_id) = MockDatabase::with_single_file(content); 138 let (db, _, file_id) = MockDatabase::with_single_file(content);
118 let source_file = db.source_file(file_id); 139 let source_file = db.source_file(file_id);
diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.txt b/crates/ra_hir/src/ty/tests/data/0002_let.txt
index 2d0d1f57b..916ca25a1 100644
--- a/crates/ra_hir/src/ty/tests/data/0002_let.txt
+++ b/crates/ra_hir/src/ty/tests/data/0002_let.txt
@@ -1,5 +1,5 @@
1[21; 22) 'a': [unknown] 1[21; 22) 'a': [unknown]
2[52; 53) '1': [unknown] 2[52; 53) '1': usize
3[11; 71) '{ ...= b; }': () 3[11; 71) '{ ...= b; }': ()
4[63; 64) 'c': usize 4[63; 64) 'c': usize
5[25; 31) '1isize': [unknown] 5[25; 31) '1isize': [unknown]
diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.txt b/crates/ra_hir/src/ty/tests/data/0003_paths.txt
index dcb5456ae..2a12d264f 100644
--- a/crates/ra_hir/src/ty/tests/data/0003_paths.txt
+++ b/crates/ra_hir/src/ty/tests/data/0003_paths.txt
@@ -1,7 +1,7 @@
1[15; 20) '{ 1 }': [unknown] 1[15; 20) '{ 1 }': u32
2[17; 18) '1': [unknown] 2[17; 18) '1': u32
3[50; 51) '1': [unknown] 3[50; 51) '1': u32
4[48; 53) '{ 1 }': [unknown] 4[48; 53) '{ 1 }': u32
5[82; 88) 'b::c()': u32 5[82; 88) 'b::c()': u32
6[67; 91) '{ ...c(); }': () 6[67; 91) '{ ...c(); }': ()
7[73; 74) 'a': fn() -> u32 7[73; 74) 'a': fn() -> u32
diff --git a/crates/ra_hir/src/ty/tests/data/0004_struct.txt b/crates/ra_hir/src/ty/tests/data/0004_struct.txt
index cc8f3665b..b4af18b87 100644
--- a/crates/ra_hir/src/ty/tests/data/0004_struct.txt
+++ b/crates/ra_hir/src/ty/tests/data/0004_struct.txt
@@ -1,5 +1,5 @@
1[86; 90) 'C(1)': [unknown] 1[86; 90) 'C(1)': [unknown]
2[121; 122) 'B': [unknown] 2[121; 122) 'B': B
3[86; 87) 'C': [unknown] 3[86; 87) 'C': [unknown]
4[129; 130) '1': [unknown] 4[129; 130) '1': [unknown]
5[107; 108) 'a': A 5[107; 108) 'a': A
@@ -13,4 +13,4 @@
13[96; 97) 'B': [unknown] 13[96; 97) 'B': [unknown]
14[88; 89) '1': [unknown] 14[88; 89) '1': [unknown]
15[82; 83) 'c': [unknown] 15[82; 83) 'c': [unknown]
16[127; 131) 'C(1)': [unknown] 16[127; 131) 'C(1)': C
diff --git a/crates/ra_hir/src/ty/tests/data/0006_backwards.txt b/crates/ra_hir/src/ty/tests/data/0006_backwards.txt
new file mode 100644
index 000000000..120069401
--- /dev/null
+++ b/crates/ra_hir/src/ty/tests/data/0006_backwards.txt
@@ -0,0 +1,20 @@
1[22; 24) '{}': ()
2[14; 15) 'x': u32
3[142; 158) 'unknow...nction': [unknown]
4[126; 127) 'a': u32
5[198; 216) 'unknow...tion()': f64
6[228; 229) 'c': f64
7[198; 214) 'unknow...nction': [unknown]
8[166; 184) 'S { i3...d: b }': S
9[222; 229) '&mut &c': &mut &f64
10[194; 195) 'c': f64
11[92; 110) 'unknow...tion()': u32
12[142; 160) 'unknow...tion()': i32
13[92; 108) 'unknow...nction': [unknown]
14[116; 128) 'takes_u32(a)': ()
15[78; 231) '{ ...t &c }': &mut &f64
16[227; 229) '&c': &f64
17[88; 89) 'a': u32
18[181; 182) 'b': i32
19[116; 125) 'takes_u32': fn(u32,) -> ()
20[138; 139) 'b': i32