2019-01-05 06:42:47 -06:00
|
|
|
use std::sync::Arc;
|
2018-12-23 05:05:54 -06:00
|
|
|
use std::fmt::Write;
|
2018-12-24 08:36:54 -06:00
|
|
|
use std::path::{PathBuf, Path};
|
|
|
|
use std::fs;
|
2018-12-20 14:56:28 -06:00
|
|
|
|
2019-01-05 06:42:47 -06:00
|
|
|
use salsa::Database;
|
|
|
|
|
2019-01-06 09:47:59 -06:00
|
|
|
use ra_db::SyntaxDatabase;
|
2018-12-23 05:15:46 -06:00
|
|
|
use ra_syntax::ast::{self, AstNode};
|
2018-12-24 08:36:54 -06:00
|
|
|
use test_utils::{project_dir, assert_eq_text, read_text};
|
2018-12-20 14:56:28 -06:00
|
|
|
|
|
|
|
use crate::{
|
2018-12-23 05:15:46 -06:00
|
|
|
source_binder,
|
2018-12-20 14:56:28 -06:00
|
|
|
mock::MockDatabase,
|
|
|
|
};
|
|
|
|
|
2018-12-24 08:36:54 -06:00
|
|
|
// 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;
|
|
|
|
}"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"basics.txt",
|
2018-12-24 08:36:54 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_let() {
|
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
fn test() {
|
|
|
|
let a = 1isize;
|
|
|
|
let b: usize = 1;
|
|
|
|
let c = b;
|
|
|
|
}
|
|
|
|
}"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"let.txt",
|
2018-12-24 08:36:54 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn infer_paths() {
|
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
fn a() -> u32 { 1 }
|
|
|
|
|
|
|
|
mod b {
|
|
|
|
fn c() -> u32 { 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
a();
|
|
|
|
b::c();
|
|
|
|
}
|
|
|
|
}"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"paths.txt",
|
2018-12-24 08:36:54 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-24 12:07:48 -06:00
|
|
|
#[test]
|
|
|
|
fn infer_struct() {
|
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
struct A {
|
|
|
|
b: B,
|
|
|
|
c: C,
|
|
|
|
}
|
|
|
|
struct B;
|
|
|
|
struct C(usize);
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
let c = C(1);
|
|
|
|
B;
|
2018-12-24 14:00:14 -06:00
|
|
|
let a: A = A { b: B, c: C(1) };
|
2018-12-24 12:07:48 -06:00
|
|
|
a.b;
|
|
|
|
a.c;
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"struct.txt",
|
2018-12-24 12:07:48 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-25 10:17:39 -06:00
|
|
|
#[test]
|
2019-01-06 12:51:42 -06:00
|
|
|
fn infer_refs() {
|
2018-12-25 10:17:39 -06:00
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
|
|
|
|
a;
|
|
|
|
*a;
|
|
|
|
&a;
|
|
|
|
&mut a;
|
|
|
|
b;
|
|
|
|
*b;
|
|
|
|
&b;
|
|
|
|
c;
|
|
|
|
*c;
|
|
|
|
d;
|
|
|
|
*d;
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"refs_and_ptrs.txt",
|
2018-12-25 10:17:39 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-26 10:00:42 -06:00
|
|
|
#[test]
|
|
|
|
fn infer_backwards() {
|
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
fn takes_u32(x: u32) {}
|
|
|
|
|
|
|
|
struct S { i32_field: i32 }
|
|
|
|
|
|
|
|
fn test() -> &mut &f64 {
|
|
|
|
let a = unknown_function();
|
|
|
|
takes_u32(a);
|
|
|
|
let b = unknown_function();
|
|
|
|
S { i32_field: b };
|
|
|
|
let c = unknown_function();
|
|
|
|
&mut &c
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"backwards.txt",
|
2018-12-26 10:00:42 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-26 14:28:05 -06:00
|
|
|
#[test]
|
|
|
|
fn infer_self() {
|
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
struct S;
|
|
|
|
|
|
|
|
impl S {
|
|
|
|
fn test(&self) {
|
|
|
|
self;
|
|
|
|
}
|
|
|
|
fn test2(self: &Self) {
|
|
|
|
self;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2019-01-06 16:01:33 -06:00
|
|
|
"self.txt",
|
2018-12-26 14:28:05 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-05 14:28:30 -06:00
|
|
|
#[test]
|
2019-01-07 13:11:31 -06:00
|
|
|
fn infer_binary_op() {
|
2019-01-05 14:28:30 -06:00
|
|
|
check_inference(
|
|
|
|
r#"
|
2019-01-06 14:39:36 -06:00
|
|
|
fn f(x: bool) -> i32 {
|
|
|
|
0i32
|
|
|
|
}
|
|
|
|
|
2019-01-05 14:28:30 -06:00
|
|
|
fn test() {
|
|
|
|
let x = a && b;
|
|
|
|
let y = true || false;
|
|
|
|
let z = x == y;
|
2019-01-07 13:11:31 -06:00
|
|
|
let minus_forty: isize = -40isize;
|
|
|
|
let h = minus_forty <= CONST_2;
|
2019-01-06 14:39:36 -06:00
|
|
|
let c = f(z || y) + 5;
|
|
|
|
let d = b;
|
2019-01-07 13:11:31 -06:00
|
|
|
let g = minus_forty ^= i;
|
|
|
|
let ten: usize = 10;
|
|
|
|
let ten_is_eleven = ten == some_num;
|
2019-01-05 14:28:30 -06:00
|
|
|
|
2019-01-07 13:11:31 -06:00
|
|
|
ten < 3
|
2019-01-05 14:28:30 -06:00
|
|
|
}
|
|
|
|
"#,
|
2019-01-07 13:11:31 -06:00
|
|
|
"binary_op.txt",
|
2019-01-05 14:28:30 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-06 12:51:42 -06:00
|
|
|
#[test]
|
|
|
|
fn infer_field_autoderef() {
|
|
|
|
check_inference(
|
|
|
|
r#"
|
|
|
|
struct A {
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
struct B;
|
|
|
|
|
|
|
|
fn test1(a: A) {
|
|
|
|
let a1 = a;
|
|
|
|
a1.b;
|
|
|
|
let a2 = &a;
|
|
|
|
a2.b;
|
|
|
|
let a3 = &mut a;
|
|
|
|
a3.b;
|
|
|
|
let a4 = &&&&&&&a;
|
|
|
|
a4.b;
|
|
|
|
let a5 = &mut &&mut &&mut a;
|
|
|
|
a5.b;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test2(a1: *const A, a2: *mut A) {
|
|
|
|
a1.b;
|
|
|
|
a2.b;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"field_autoderef.txt",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-24 08:36:54 -06:00
|
|
|
fn infer(content: &str) -> String {
|
2018-12-23 05:15:46 -06:00
|
|
|
let (db, _, file_id) = MockDatabase::with_single_file(content);
|
2018-12-23 05:05:54 -06:00
|
|
|
let source_file = db.source_file(file_id);
|
|
|
|
let mut acc = String::new();
|
2018-12-23 05:15:46 -06:00
|
|
|
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();
|
2018-12-23 10:13:11 -06:00
|
|
|
let inference_result = func.infer(&db).unwrap();
|
2019-01-06 09:47:59 -06:00
|
|
|
let body_syntax_mapping = func.body_syntax_mapping(&db).unwrap();
|
2019-01-06 16:01:33 -06:00
|
|
|
let mut types = Vec::new();
|
2019-01-06 16:57:39 -06:00
|
|
|
for (pat, ty) in inference_result.type_of_pat.iter() {
|
2019-01-06 18:10:29 -06:00
|
|
|
let syntax_ptr = match body_syntax_mapping.pat_syntax(pat) {
|
|
|
|
Some(sp) => sp,
|
|
|
|
None => continue,
|
2019-01-06 09:47:59 -06:00
|
|
|
};
|
2019-01-06 16:01:33 -06:00
|
|
|
types.push((syntax_ptr, ty));
|
2019-01-06 09:47:59 -06:00
|
|
|
}
|
2019-01-06 16:57:39 -06:00
|
|
|
for (expr, ty) in inference_result.type_of_expr.iter() {
|
2019-01-06 18:10:29 -06:00
|
|
|
let syntax_ptr = match body_syntax_mapping.expr_syntax(expr) {
|
|
|
|
Some(sp) => sp,
|
|
|
|
None => continue,
|
2019-01-06 09:47:59 -06:00
|
|
|
};
|
2019-01-06 16:01:33 -06:00
|
|
|
types.push((syntax_ptr, ty));
|
2019-01-06 09:47:59 -06:00
|
|
|
}
|
2019-01-06 16:01:33 -06:00
|
|
|
// sort ranges for consistency
|
|
|
|
types.sort_by_key(|(ptr, _)| (ptr.range().start(), ptr.range().end()));
|
2019-01-06 09:47:59 -06:00
|
|
|
for (syntax_ptr, ty) in &types {
|
2018-12-23 05:05:54 -06:00
|
|
|
let node = syntax_ptr.resolve(&source_file);
|
2018-12-23 05:15:46 -06:00
|
|
|
write!(
|
|
|
|
acc,
|
|
|
|
"{} '{}': {}\n",
|
|
|
|
syntax_ptr.range(),
|
|
|
|
ellipsize(node.text().to_string().replace("\n", " "), 15),
|
|
|
|
ty
|
|
|
|
)
|
|
|
|
.unwrap();
|
2018-12-20 14:56:28 -06:00
|
|
|
}
|
|
|
|
}
|
2018-12-23 05:05:54 -06:00
|
|
|
acc
|
|
|
|
}
|
|
|
|
|
2018-12-24 08:36:54 -06:00
|
|
|
fn check_inference(content: &str, data_file: impl AsRef<Path>) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-12-23 05:05:54 -06:00
|
|
|
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
|
2018-12-20 14:56:28 -06:00
|
|
|
}
|
|
|
|
|
2018-12-23 05:05:54 -06:00
|
|
|
fn test_data_dir() -> PathBuf {
|
|
|
|
project_dir().join("crates/ra_hir/src/ty/tests/data")
|
2018-12-20 14:56:28 -06:00
|
|
|
}
|
2019-01-05 06:42:47 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
|
|
|
|
let (mut db, pos) = MockDatabase::with_position(
|
|
|
|
"
|
|
|
|
//- /lib.rs
|
|
|
|
fn foo() -> i32 {
|
|
|
|
<|>1 + 1
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
let func = source_binder::function_from_position(&db, pos)
|
|
|
|
.unwrap()
|
|
|
|
.unwrap();
|
|
|
|
{
|
|
|
|
let events = db.log_executed(|| {
|
|
|
|
func.infer(&db).unwrap();
|
|
|
|
});
|
|
|
|
assert!(format!("{:?}", events).contains("infer"))
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_text = "
|
|
|
|
fn foo() -> i32 {
|
|
|
|
1
|
|
|
|
+
|
|
|
|
1
|
|
|
|
}
|
|
|
|
"
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
db.query_mut(ra_db::FileTextQuery)
|
|
|
|
.set(pos.file_id, Arc::new(new_text));
|
|
|
|
|
|
|
|
{
|
|
|
|
let events = db.log_executed(|| {
|
|
|
|
func.infer(&db).unwrap();
|
|
|
|
});
|
|
|
|
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
|
|
|
|
}
|
|
|
|
}
|