From 3ac605e6876056fa56098231cc2f96553faab8f0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Thu, 20 Dec 2018 21:56:28 +0100 Subject: Add beginnings of type infrastructure --- crates/ra_hir/src/ty/primitive.rs | 98 +++++++++++++++++++++++++++++++++++++++ crates/ra_hir/src/ty/tests.rs | 45 ++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 crates/ra_hir/src/ty/primitive.rs create mode 100644 crates/ra_hir/src/ty/tests.rs (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 new file mode 100644 index 000000000..4a5ce5a97 --- /dev/null +++ b/crates/ra_hir/src/ty/primitive.rs @@ -0,0 +1,98 @@ +use std::fmt; + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +pub enum IntTy { + Isize, + I8, + I16, + I32, + I64, + I128, +} + +impl fmt::Debug for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for IntTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +impl IntTy { + pub fn ty_to_string(&self) -> &'static str { + match *self { + IntTy::Isize => "isize", + IntTy::I8 => "i8", + IntTy::I16 => "i16", + IntTy::I32 => "i32", + IntTy::I64 => "i64", + IntTy::I128 => "i128", + } + } +} + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +pub enum UintTy { + Usize, + U8, + U16, + U32, + U64, + U128, +} + +impl UintTy { + pub fn ty_to_string(&self) -> &'static str { + match *self { + UintTy::Usize => "usize", + UintTy::U8 => "u8", + UintTy::U16 => "u16", + UintTy::U32 => "u32", + UintTy::U64 => "u64", + UintTy::U128 => "u128", + } + } +} + +impl fmt::Debug for UintTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for UintTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +#[derive(Clone, PartialEq, Eq, Hash, Copy, PartialOrd, Ord)] +pub enum FloatTy { + F32, + F64, +} + +impl fmt::Debug for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::Display::fmt(self, f) + } +} + +impl fmt::Display for FloatTy { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.ty_to_string()) + } +} + +impl FloatTy { + pub fn ty_to_string(self) -> &'static str { + match self { + FloatTy::F32 => "f32", + FloatTy::F64 => "f64", + } + } +} 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 @@ +use std::sync::Arc; + +use salsa::Database; +use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase}; +use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}}; +use relative_path::RelativePath; + +use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode}; + +use crate::{ + self as hir, + db::HirDatabase, + mock::MockDatabase, +}; + +fn infer_all_fns(fixture: &str) -> () { + let (db, source_root) = MockDatabase::with_files(fixture); + for &file_id in source_root.files.values() { + let source_file = db.source_file(file_id); + for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { + let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); + let inference_result = func.infer(&db); + for (syntax_ptr, ty) in &inference_result.type_for { + let node = syntax_ptr.resolve(&source_file); + eprintln!("{} '{}': {:?}", syntax_ptr.range(), node.text(), ty); + } + } + } +} + +#[test] +fn infer_smoke_test() { + let text = " + //- /lib.rs + fn foo(x: u32, y: !) -> i128 { + x; + y; + return 1; + \"hello\"; + 0 + } + "; + + infer_all_fns(text); +} -- cgit v1.2.3 From 3899898d75176ce3cd87f9e2acecd7e3a987dda5 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 22 Dec 2018 22:17:55 +0100 Subject: Parse integer / float types --- crates/ra_hir/src/ty/primitive.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (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 4a5ce5a97..ad79b17e4 100644 --- a/crates/ra_hir/src/ty/primitive.rs +++ b/crates/ra_hir/src/ty/primitive.rs @@ -33,6 +33,18 @@ impl IntTy { IntTy::I128 => "i128", } } + + pub fn from_string(s: &str) -> Option { + match s { + "isize" => Some(IntTy::Isize), + "i8" => Some(IntTy::I8), + "i16" => Some(IntTy::I16), + "i32" => Some(IntTy::I32), + "i64" => Some(IntTy::I64), + "i128" => Some(IntTy::I128), + _ => None, + } + } } #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] @@ -56,6 +68,18 @@ impl UintTy { UintTy::U128 => "u128", } } + + pub fn from_string(s: &str) -> Option { + match s { + "usize" => Some(UintTy::Usize), + "u8" => Some(UintTy::U8), + "u16" => Some(UintTy::U16), + "u32" => Some(UintTy::U32), + "u64" => Some(UintTy::U64), + "u128" => Some(UintTy::U128), + _ => None, + } + } } impl fmt::Debug for UintTy { @@ -95,4 +119,12 @@ impl FloatTy { FloatTy::F64 => "f64", } } + + pub fn from_string(s: &str) -> Option { + match s { + "f32" => Some(FloatTy::F32), + "f64" => Some(FloatTy::F64), + _ => None, + } + } } -- cgit v1.2.3 From 7348f7883fa2bd571fff036c82e98c102d05c362 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 12:05:54 +0100 Subject: Add testing infrastructure for type inference - move dir_tests to test_utils for that. --- crates/ra_hir/src/ty/tests.rs | 62 ++++++++++++++++--------- crates/ra_hir/src/ty/tests/data/0001_basics.rs | 11 +++++ crates/ra_hir/src/ty/tests/data/0001_basics.txt | 13 ++++++ 3 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 crates/ra_hir/src/ty/tests/data/0001_basics.rs create mode 100644 crates/ra_hir/src/ty/tests/data/0001_basics.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 f2466dd51..98eedaa3f 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -1,8 +1,11 @@ +use std::fmt::Write; use std::sync::Arc; +use std::path::{Path, PathBuf}; use salsa::Database; use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase}; use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}}; +use test_utils::{project_dir, dir_tests}; use relative_path::RelativePath; use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode}; @@ -13,33 +16,46 @@ use crate::{ mock::MockDatabase, }; -fn infer_all_fns(fixture: &str) -> () { - let (db, source_root) = MockDatabase::with_files(fixture); - for &file_id in source_root.files.values() { - let source_file = db.source_file(file_id); - for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { - let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); - let inference_result = func.infer(&db); - for (syntax_ptr, ty) in &inference_result.type_for { - let node = syntax_ptr.resolve(&source_file); - eprintln!("{} '{}': {:?}", syntax_ptr.range(), node.text(), ty); - } +fn infer_file(content: &str) -> String { + let (db, source_root, file_id) = MockDatabase::with_single_file(content); + let source_file = db.source_file(file_id); + let mut acc = String::new(); + for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { + let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); + let inference_result = func.infer(&db); + for (syntax_ptr, ty) in &inference_result.type_for { + let node = syntax_ptr.resolve(&source_file); + write!(acc, "{} '{}': {}\n", syntax_ptr.range(), ellipsize(node.text().to_string().replace("\n", " "), 15), ty); } } + acc +} + +fn ellipsize(mut text: String, max_len: usize) -> String { + if text.len() <= max_len { + return text; + } + let ellipsis = "..."; + let e_len = ellipsis.len(); + let mut prefix_len = (max_len - e_len) / 2; + while !text.is_char_boundary(prefix_len) { + prefix_len += 1; + } + let mut suffix_len = max_len - e_len - prefix_len; + while !text.is_char_boundary(text.len() - suffix_len) { + suffix_len += 1; + } + text.replace_range(prefix_len..text.len() - suffix_len, ellipsis); + text } #[test] -fn infer_smoke_test() { - let text = " - //- /lib.rs - fn foo(x: u32, y: !) -> i128 { - x; - y; - return 1; - \"hello\"; - 0 - } - "; +pub fn infer_tests() { + dir_tests(&test_data_dir(), &["."], |text, _path| { + infer_file(text) + }); +} - infer_all_fns(text); +fn test_data_dir() -> PathBuf { + project_dir().join("crates/ra_hir/src/ty/tests/data") } diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.rs b/crates/ra_hir/src/ty/tests/data/0001_basics.rs new file mode 100644 index 000000000..59a60d031 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0001_basics.rs @@ -0,0 +1,11 @@ + +fn test(a: u32, b: isize, c: !, d: &str) { + a; + b; + c; + d; + 1usize; + 1isize; + "test"; + 1.0f32; +} diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.txt b/crates/ra_hir/src/ty/tests/data/0001_basics.txt new file mode 100644 index 000000000..0c46f243a --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0001_basics.txt @@ -0,0 +1,13 @@ +[33; 34) 'd': [unknown] +[88; 94) '1isize': [unknown] +[48; 49) 'a': u32 +[55; 56) 'b': isize +[112; 118) '1.0f32': [unknown] +[76; 82) '1usize': [unknown] +[9; 10) 'a': u32 +[27; 28) 'c': ! +[62; 63) 'c': ! +[17; 18) 'b': isize +[100; 106) '"test"': [unknown] +[42; 121) '{ ...f32; }': () +[69; 70) 'd': [unknown] -- cgit v1.2.3 From 515c3bc59bfc227cbbb82f80b53c5c125be4fc30 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 12:15:46 +0100 Subject: Cleanup --- crates/ra_hir/src/ty/tests.rs | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 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 98eedaa3f..0880b51bc 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -1,31 +1,38 @@ use std::fmt::Write; -use std::sync::Arc; -use std::path::{Path, PathBuf}; +use std::path::{PathBuf}; -use salsa::Database; -use ra_db::{FilesDatabase, CrateGraph, SyntaxDatabase}; -use ra_syntax::{SmolStr, algo::visit::{visitor, Visitor}, ast::{self, AstNode}}; +use ra_db::{SyntaxDatabase}; +use ra_syntax::ast::{self, AstNode}; use test_utils::{project_dir, dir_tests}; -use relative_path::RelativePath; - -use crate::{source_binder, mock::WORKSPACE, module::ModuleSourceNode}; use crate::{ - self as hir, - db::HirDatabase, + source_binder, mock::MockDatabase, }; fn infer_file(content: &str) -> String { - let (db, source_root, file_id) = MockDatabase::with_single_file(content); + let (db, _, file_id) = MockDatabase::with_single_file(content); let source_file = db.source_file(file_id); let mut acc = String::new(); - for fn_def in source_file.syntax().descendants().filter_map(ast::FnDef::cast) { - let func = source_binder::function_from_source(&db, file_id, fn_def).unwrap().unwrap(); + for fn_def in source_file + .syntax() + .descendants() + .filter_map(ast::FnDef::cast) + { + let func = source_binder::function_from_source(&db, file_id, fn_def) + .unwrap() + .unwrap(); let inference_result = func.infer(&db); for (syntax_ptr, ty) in &inference_result.type_for { let node = syntax_ptr.resolve(&source_file); - write!(acc, "{} '{}': {}\n", syntax_ptr.range(), ellipsize(node.text().to_string().replace("\n", " "), 15), ty); + write!( + acc, + "{} '{}': {}\n", + syntax_ptr.range(), + ellipsize(node.text().to_string().replace("\n", " "), 15), + ty + ) + .unwrap(); } } acc @@ -51,9 +58,7 @@ fn ellipsize(mut text: String, max_len: usize) -> String { #[test] pub fn infer_tests() { - dir_tests(&test_data_dir(), &["."], |text, _path| { - infer_file(text) - }); + dir_tests(&test_data_dir(), &["."], |text, _path| infer_file(text)); } fn test_data_dir() -> PathBuf { -- cgit v1.2.3 From 93ffbf80c632a7d38fc8bbdf6357bfd26a96a35a Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 13:22:29 +0100 Subject: Make let statements kind of work --- crates/ra_hir/src/ty/tests/data/0002_let.rs | 5 +++++ crates/ra_hir/src/ty/tests/data/0002_let.txt | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 crates/ra_hir/src/ty/tests/data/0002_let.rs create mode 100644 crates/ra_hir/src/ty/tests/data/0002_let.txt (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.rs b/crates/ra_hir/src/ty/tests/data/0002_let.rs new file mode 100644 index 000000000..5641da75b --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0002_let.rs @@ -0,0 +1,5 @@ +fn test() { + let a = 1isize; + let b: usize = 1; + let c = b; +} diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.txt b/crates/ra_hir/src/ty/tests/data/0002_let.txt new file mode 100644 index 000000000..5f515ee59 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0002_let.txt @@ -0,0 +1,7 @@ +[51; 52) '1': [unknown] +[10; 70) '{ ...= b; }': () +[24; 30) '1isize': [unknown] +[20; 21) 'a': [unknown] +[62; 63) 'c': usize +[66; 67) 'b': usize +[40; 41) 'b': usize -- cgit v1.2.3 From ef67581104eb00a0c199f0b2a3b558da8a6f90a2 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 17:13:11 +0100 Subject: Resolve paths to defs (functions currently) during type inference --- crates/ra_hir/src/ty/tests.rs | 7 ++++++- crates/ra_hir/src/ty/tests/data/0003_paths.rs | 10 ++++++++++ crates/ra_hir/src/ty/tests/data/0003_paths.txt | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 crates/ra_hir/src/ty/tests/data/0003_paths.rs create mode 100644 crates/ra_hir/src/ty/tests/data/0003_paths.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 0880b51bc..e0458327a 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -1,5 +1,8 @@ use std::fmt::Write; use std::path::{PathBuf}; +use std::sync::Once; + +use flexi_logger::Logger; use ra_db::{SyntaxDatabase}; use ra_syntax::ast::{self, AstNode}; @@ -22,7 +25,7 @@ fn infer_file(content: &str) -> String { let func = source_binder::function_from_source(&db, file_id, fn_def) .unwrap() .unwrap(); - let inference_result = func.infer(&db); + let inference_result = func.infer(&db).unwrap(); for (syntax_ptr, ty) in &inference_result.type_for { let node = syntax_ptr.resolve(&source_file); write!( @@ -58,6 +61,8 @@ fn ellipsize(mut text: String, max_len: usize) -> String { #[test] pub fn infer_tests() { + static INIT: Once = Once::new(); + INIT.call_once(|| Logger::with_env().start().unwrap()); dir_tests(&test_data_dir(), &["."], |text, _path| infer_file(text)); } diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.rs b/crates/ra_hir/src/ty/tests/data/0003_paths.rs new file mode 100644 index 000000000..e8b11198b --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0003_paths.rs @@ -0,0 +1,10 @@ +fn a() -> u32 { 1 } + +mod b { + fn c() -> u32 { 1 } +} + +fn test() { + a(); + b::c(); +} diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.txt b/crates/ra_hir/src/ty/tests/data/0003_paths.txt new file mode 100644 index 000000000..3a53370a2 --- /dev/null +++ b/crates/ra_hir/src/ty/tests/data/0003_paths.txt @@ -0,0 +1,9 @@ +[16; 17) '1': [unknown] +[14; 19) '{ 1 }': [unknown] +[47; 52) '{ 1 }': [unknown] +[49; 50) '1': [unknown] +[81; 87) 'b::c()': [unknown] +[66; 90) '{ ...c(); }': () +[72; 73) 'a': fn() -> u32 +[72; 75) 'a()': [unknown] +[81; 85) 'b::c': fn() -> u32 -- cgit v1.2.3 From c85748f5fbe308610261da91aeb978eca751a0ff Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 23 Dec 2018 17:16:47 +0100 Subject: Type the return values of call expressions --- crates/ra_hir/src/ty/tests/data/0003_paths.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty') 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 3a53370a2..acde9b7ad 100644 --- a/crates/ra_hir/src/ty/tests/data/0003_paths.txt +++ b/crates/ra_hir/src/ty/tests/data/0003_paths.txt @@ -2,8 +2,8 @@ [14; 19) '{ 1 }': [unknown] [47; 52) '{ 1 }': [unknown] [49; 50) '1': [unknown] -[81; 87) 'b::c()': [unknown] +[81; 87) 'b::c()': u32 [66; 90) '{ ...c(); }': () [72; 73) 'a': fn() -> u32 -[72; 75) 'a()': [unknown] +[72; 75) 'a()': u32 [81; 85) 'b::c': fn() -> u32 -- cgit v1.2.3 From 655f5bc26190b94e237dcc485e405de0d192e6ab Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 24 Dec 2018 15:19:49 +0100 Subject: Rename a variable for consistency --- crates/ra_hir/src/ty/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (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 e0458327a..021227749 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -26,7 +26,7 @@ fn infer_file(content: &str) -> String { .unwrap() .unwrap(); let inference_result = func.infer(&db).unwrap(); - for (syntax_ptr, ty) in &inference_result.type_for { + for (syntax_ptr, ty) in &inference_result.type_of { let node = syntax_ptr.resolve(&source_file); write!( acc, -- cgit v1.2.3 From 4befde1eee5b1e2b7ddc9bf764b77f82b792c318 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Mon, 24 Dec 2018 15:36:54 +0100 Subject: Change inference tests to have one per file --- crates/ra_hir/src/ty/tests.rs | 89 ++++++++++++++++++++++---- crates/ra_hir/src/ty/tests/data/0001_basics.rs | 11 ---- crates/ra_hir/src/ty/tests/data/0002_let.rs | 5 -- crates/ra_hir/src/ty/tests/data/0002_let.txt | 14 ++-- crates/ra_hir/src/ty/tests/data/0003_paths.rs | 10 --- crates/ra_hir/src/ty/tests/data/0003_paths.txt | 18 +++--- 6 files changed, 92 insertions(+), 55 deletions(-) delete mode 100644 crates/ra_hir/src/ty/tests/data/0001_basics.rs delete mode 100644 crates/ra_hir/src/ty/tests/data/0002_let.rs delete mode 100644 crates/ra_hir/src/ty/tests/data/0003_paths.rs (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 021227749..b6c02cd80 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -1,19 +1,74 @@ use std::fmt::Write; -use std::path::{PathBuf}; -use std::sync::Once; - -use flexi_logger::Logger; +use std::path::{PathBuf, Path}; +use std::fs; use ra_db::{SyntaxDatabase}; use ra_syntax::ast::{self, AstNode}; -use test_utils::{project_dir, dir_tests}; +use test_utils::{project_dir, assert_eq_text, read_text}; use crate::{ source_binder, mock::MockDatabase, }; -fn infer_file(content: &str) -> String { +// These tests compare the inference results for all expressions in a file +// against snapshots of the current results. If you change something and these +// tests fail expectedly, you can update the comparison files by deleting them +// and running the tests again. Similarly, to add a new test, just write the +// test here in the same pattern and it will automatically write the snapshot. + +#[test] +fn infer_basics() { + check_inference( + r#" +fn test(a: u32, b: isize, c: !, d: &str) { + a; + b; + c; + d; + 1usize; + 1isize; + "test"; + 1.0f32; +}"#, + "0001_basics.txt", + ); +} + +#[test] +fn infer_let() { + check_inference( + r#" +fn test() { + let a = 1isize; + let b: usize = 1; + let c = b; +} +}"#, + "0002_let.txt", + ); +} + +#[test] +fn infer_paths() { + check_inference( + r#" +fn a() -> u32 { 1 } + +mod b { + fn c() -> u32 { 1 } +} + +fn test() { + a(); + b::c(); +} +}"#, + "0003_paths.txt", + ); +} + +fn infer(content: &str) -> String { let (db, _, file_id) = MockDatabase::with_single_file(content); let source_file = db.source_file(file_id); let mut acc = String::new(); @@ -41,6 +96,21 @@ fn infer_file(content: &str) -> String { acc } +fn check_inference(content: &str, data_file: impl AsRef) { + let data_file_path = test_data_dir().join(data_file); + let result = infer(content); + + if !data_file_path.exists() { + println!("File with expected result doesn't exist, creating...\n"); + println!("{}\n{}", content, result); + fs::write(&data_file_path, &result).unwrap(); + panic!("File {:?} with expected result was created", data_file_path); + } + + let expected = read_text(&data_file_path); + assert_eq_text!(&expected, &result); +} + fn ellipsize(mut text: String, max_len: usize) -> String { if text.len() <= max_len { return text; @@ -59,13 +129,6 @@ fn ellipsize(mut text: String, max_len: usize) -> String { text } -#[test] -pub fn infer_tests() { - static INIT: Once = Once::new(); - INIT.call_once(|| Logger::with_env().start().unwrap()); - dir_tests(&test_data_dir(), &["."], |text, _path| infer_file(text)); -} - fn test_data_dir() -> PathBuf { project_dir().join("crates/ra_hir/src/ty/tests/data") } diff --git a/crates/ra_hir/src/ty/tests/data/0001_basics.rs b/crates/ra_hir/src/ty/tests/data/0001_basics.rs deleted file mode 100644 index 59a60d031..000000000 --- a/crates/ra_hir/src/ty/tests/data/0001_basics.rs +++ /dev/null @@ -1,11 +0,0 @@ - -fn test(a: u32, b: isize, c: !, d: &str) { - a; - b; - c; - d; - 1usize; - 1isize; - "test"; - 1.0f32; -} diff --git a/crates/ra_hir/src/ty/tests/data/0002_let.rs b/crates/ra_hir/src/ty/tests/data/0002_let.rs deleted file mode 100644 index 5641da75b..000000000 --- a/crates/ra_hir/src/ty/tests/data/0002_let.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn test() { - let a = 1isize; - let b: usize = 1; - let c = b; -} 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 5f515ee59..2d0d1f57b 100644 --- a/crates/ra_hir/src/ty/tests/data/0002_let.txt +++ b/crates/ra_hir/src/ty/tests/data/0002_let.txt @@ -1,7 +1,7 @@ -[51; 52) '1': [unknown] -[10; 70) '{ ...= b; }': () -[24; 30) '1isize': [unknown] -[20; 21) 'a': [unknown] -[62; 63) 'c': usize -[66; 67) 'b': usize -[40; 41) 'b': usize +[21; 22) 'a': [unknown] +[52; 53) '1': [unknown] +[11; 71) '{ ...= b; }': () +[63; 64) 'c': usize +[25; 31) '1isize': [unknown] +[41; 42) 'b': usize +[67; 68) 'b': usize diff --git a/crates/ra_hir/src/ty/tests/data/0003_paths.rs b/crates/ra_hir/src/ty/tests/data/0003_paths.rs deleted file mode 100644 index e8b11198b..000000000 --- a/crates/ra_hir/src/ty/tests/data/0003_paths.rs +++ /dev/null @@ -1,10 +0,0 @@ -fn a() -> u32 { 1 } - -mod b { - fn c() -> u32 { 1 } -} - -fn test() { - a(); - b::c(); -} 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 acde9b7ad..dcb5456ae 100644 --- a/crates/ra_hir/src/ty/tests/data/0003_paths.txt +++ b/crates/ra_hir/src/ty/tests/data/0003_paths.txt @@ -1,9 +1,9 @@ -[16; 17) '1': [unknown] -[14; 19) '{ 1 }': [unknown] -[47; 52) '{ 1 }': [unknown] -[49; 50) '1': [unknown] -[81; 87) 'b::c()': u32 -[66; 90) '{ ...c(); }': () -[72; 73) 'a': fn() -> u32 -[72; 75) 'a()': u32 -[81; 85) 'b::c': fn() -> u32 +[15; 20) '{ 1 }': [unknown] +[17; 18) '1': [unknown] +[50; 51) '1': [unknown] +[48; 53) '{ 1 }': [unknown] +[82; 88) 'b::c()': u32 +[67; 91) '{ ...c(); }': () +[73; 74) 'a': fn() -> u32 +[73; 76) 'a()': u32 +[82; 86) 'b::c': fn() -> u32 -- cgit v1.2.3