diff options
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/primitive.rs | 98 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 45 |
2 files changed, 143 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/primitive.rs b/crates/ra_hir/src/ty/primitive.rs new file mode 100644 index 000000000..4a5ce5a97 --- /dev/null +++ b/crates/ra_hir/src/ty/primitive.rs | |||
@@ -0,0 +1,98 @@ | |||
1 | use std::fmt; | ||
2 | |||
3 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] | ||
4 | pub enum IntTy { | ||
5 | Isize, | ||
6 | I8, | ||
7 | I16, | ||
8 | I32, | ||
9 | I64, | ||
10 | I128, | ||
11 | } | ||
12 | |||
13 | impl fmt::Debug for IntTy { | ||
14 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
15 | fmt::Display::fmt(self, f) | ||
16 | } | ||
17 | } | ||
18 | |||
19 | impl fmt::Display for IntTy { | ||
20 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
21 | write!(f, "{}", self.ty_to_string()) | ||
22 | } | ||
23 | } | ||
24 | |||
25 | impl IntTy { | ||
26 | pub fn ty_to_string(&self) -> &'static str { | ||
27 | match *self { | ||
28 | IntTy::Isize => "isize", | ||
29 | IntTy::I8 => "i8", | ||
30 | IntTy::I16 => "i16", | ||
31 | IntTy::I32 => "i32", | ||
32 | IntTy::I64 => "i64", | ||
33 | IntTy::I128 => "i128", | ||
34 | } | ||
35 | } | ||
36 | } | ||
37 | |||
38 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] | ||
39 | pub enum UintTy { | ||
40 | Usize, | ||
41 | U8, | ||
42 | U16, | ||
43 | U32, | ||
44 | U64, | ||
45 | U128, | ||
46 | } | ||
47 | |||
48 | impl UintTy { | ||
49 | pub fn ty_to_string(&self) -> &'static str { | ||
50 | match *self { | ||
51 | UintTy::Usize => "usize", | ||
52 | UintTy::U8 => "u8", | ||
53 | UintTy::U16 => "u16", | ||
54 | UintTy::U32 => "u32", | ||
55 | UintTy::U64 => "u64", | ||
56 | UintTy::U128 => "u128", | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | |||
61 | impl fmt::Debug for UintTy { | ||
62 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
63 | fmt::Display::fmt(self, f) | ||
64 | } | ||
65 | } | ||
66 | |||
67 | impl fmt::Display for UintTy { | ||
68 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
69 | write!(f, "{}", self.ty_to_string()) | ||
70 | } | ||
71 | } | ||
72 | |||
73 | #[derive(Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)] | ||
74 | pub enum FloatTy { | ||
75 | F32, | ||
76 | F64, | ||
77 | } | ||
78 | |||
79 | impl fmt::Debug for FloatTy { | ||
80 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
81 | fmt::Display::fmt(self, f) | ||
82 | } | ||
83 | } | ||
84 | |||
85 | impl fmt::Display for FloatTy { | ||
86 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
87 | write!(f, "{}", self.ty_to_string()) | ||
88 | } | ||
89 | } | ||
90 | |||
91 | impl FloatTy { | ||
92 | pub fn ty_to_string(self) -> &'static str { | ||
93 | match self { | ||
94 | FloatTy::F32 => "f32", | ||
95 | FloatTy::F64 => "f64", | ||
96 | } | ||
97 | } | ||
98 | } | ||
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs new file mode 100644 index 000000000..f2466dd51 --- /dev/null +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use salsa::Database; | ||
4 | use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase}; | ||
5 | use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}}; | ||
6 | use relative_path::RelativePath; | ||
7 | |||
8 | use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode}; | ||
9 | |||
10 | use crate::{ | ||
11 | self as hir, | ||
12 | db::HirDatabase, | ||
13 | mock::MockDatabase, | ||
14 | }; | ||
15 | |||
16 | fn infer_all_fns(fixture: &str) -> () { | ||
17 | let (db, source_root) = MockDatabase::with_files(fixture); | ||
18 | for &file_id in source_root.files.values() { | ||
19 | let source_file = db.source_file(file_id); | ||
20 | for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { | ||
21 | let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); | ||
22 | let inference_result = func.infer(&db); | ||
23 | for (syntax_ptr, ty) in &inference_result.type_for { | ||
24 | let node = syntax_ptr.resolve(&source_file); | ||
25 | eprintln!("{} '{}': {:?}", syntax_ptr.range(), node.text(), ty); | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | } | ||
30 | |||
31 | #[test] | ||
32 | fn infer_smoke_test() { | ||
33 | let text = " | ||
34 | //- /lib.rs | ||
35 | fn foo(x: u32, y: !) -> i128 { | ||
36 | x; | ||
37 | y; | ||
38 | return 1; | ||
39 | \"hello\"; | ||
40 | 0 | ||
41 | } | ||
42 | "; | ||
43 | |||
44 | infer_all_fns(text); | ||
45 | } | ||