rust/crates/ra_hir_ty/src/tests.rs

256 lines
8.0 KiB
Rust
Raw Normal View History

mod never_type;
mod coercion;
2019-12-03 06:38:54 -06:00
mod regression;
mod simple;
mod patterns;
mod traits;
mod method_resolution;
mod macros;
use std::fmt::Write;
use std::sync::Arc;
2018-12-20 14:56:28 -06:00
2019-11-27 08:46:02 -06:00
use hir_def::{
2019-12-05 14:17:17 -06:00
body::BodySourceMap, child_from_source::ChildFromSource, db::DefDatabase, nameres::CrateDefMap,
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
2019-11-27 08:46:02 -06:00
};
2019-11-28 03:50:26 -06:00
use hir_expand::InFile;
2019-08-29 08:49:10 -05:00
use insta::assert_snapshot;
use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
use ra_syntax::{
algo,
ast::{self, AstNode},
};
2018-12-20 14:56:28 -06:00
2019-11-27 08:46:02 -06:00
use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult};
2018-12-20 14:56:28 -06:00
// These tests compare the inference results for all expressions in a file
2019-03-03 05:40:36 -06:00
// against snapshots of the expected results using insta. Use cargo-insta to
// update the snapshots.
fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
2019-05-28 10:07:39 -05:00
let file = db.parse(pos.file_id).ok().unwrap();
2019-04-12 17:32:43 -05:00
let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
2019-12-05 14:17:17 -06:00
let fn_def = expr.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
2019-11-27 08:46:02 -06:00
let module = db.module_for_file(pos.file_id);
2019-12-05 14:17:17 -06:00
let func = module.child_from_source(db, InFile::new(pos.file_id.into(), fn_def)).unwrap();
let (_body, source_map) = db.body_with_source_map(func.into());
if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) {
let infer = db.infer(func.into());
let ty = &infer[expr_id];
return ty.display(db).to_string();
2019-11-27 08:46:02 -06:00
}
panic!("Can't find expression")
}
fn type_at(content: &str) -> String {
let (db, file_pos) = TestDB::with_position(content);
type_at_pos(&db, file_pos)
}
fn infer(content: &str) -> String {
let (db, file_id) = TestDB::with_single_file(content);
let mut acc = String::new();
let mut infer_def = |inference_result: Arc<InferenceResult>,
body_source_map: Arc<BodySourceMap>| {
let mut types = Vec::new();
2019-01-06 16:57:39 -06:00
for (pat, ty) in inference_result.type_of_pat.iter() {
2019-03-02 06:14:37 -06:00
let syntax_ptr = match body_source_map.pat_syntax(pat) {
Some(sp) => {
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
}
2019-01-06 18:10:29 -06:00
None => continue,
};
types.push((syntax_ptr, ty));
}
2019-01-06 16:57:39 -06:00
for (expr, ty) in inference_result.type_of_expr.iter() {
2019-03-02 06:14:37 -06:00
let syntax_ptr = match body_source_map.expr_syntax(expr) {
Some(sp) => {
sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
}
2019-01-06 18:10:29 -06:00
None => continue,
};
types.push((syntax_ptr, ty));
}
// sort ranges for consistency
2019-11-20 00:40:36 -06:00
types.sort_by_key(|(src_ptr, _)| {
(src_ptr.value.range().start(), src_ptr.value.range().end())
});
for (src_ptr, ty) in &types {
2019-11-20 00:40:36 -06:00
let node = src_ptr.value.to_node(&src_ptr.file_syntax(&db));
2019-07-19 02:43:01 -05:00
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
2019-07-20 04:58:27 -05:00
(self_param.self_kw_token().text_range(), "self".to_string())
2019-03-30 05:25:53 -05:00
} else {
2019-11-20 00:40:36 -06:00
(src_ptr.value.range(), node.text().to_string().replace("\n", " "))
2019-03-30 05:25:53 -05:00
};
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
write!(
acc,
"{}{} '{}': {}\n",
macro_prefix,
range,
ellipsize(text, 15),
ty.display(&db)
)
.unwrap();
2018-12-20 14:56:28 -06:00
}
};
2019-11-27 08:46:02 -06:00
let module = db.module_for_file(file_id);
let crate_def_map = db.crate_def_map(module.krate);
let mut defs: Vec<DefWithBodyId> = Vec::new();
2019-11-27 12:31:51 -06:00
visit_module(&db, &crate_def_map, module.local_id, &mut |it| defs.push(it));
2019-11-27 08:46:02 -06:00
defs.sort_by_key(|def| match def {
DefWithBodyId::FunctionId(it) => {
it.lookup(&db).ast_id.to_node(&db).syntax().text_range().start()
}
DefWithBodyId::ConstId(it) => {
it.lookup(&db).ast_id.to_node(&db).syntax().text_range().start()
}
DefWithBodyId::StaticId(it) => {
it.lookup(&db).ast_id.to_node(&db).syntax().text_range().start()
2019-04-12 16:56:57 -05:00
}
2019-11-27 08:46:02 -06:00
});
for def in defs {
let (_body, source_map) = db.body_with_source_map(def);
let infer = db.infer(def);
infer_def(infer, source_map);
}
2019-03-03 05:40:36 -06:00
acc.truncate(acc.trim_end().len());
acc
}
2019-11-27 08:46:02 -06:00
fn visit_module(
db: &TestDB,
crate_def_map: &CrateDefMap,
module_id: LocalModuleId,
cb: &mut dyn FnMut(DefWithBodyId),
) {
for decl in crate_def_map[module_id].scope.declarations() {
match decl {
ModuleDefId::FunctionId(it) => cb(it.into()),
ModuleDefId::ConstId(it) => cb(it.into()),
ModuleDefId::StaticId(it) => cb(it.into()),
ModuleDefId::TraitId(it) => {
let trait_data = db.trait_data(it);
for &(_, item) in trait_data.items.iter() {
match item {
AssocItemId::FunctionId(it) => cb(it.into()),
AssocItemId::ConstId(it) => cb(it.into()),
AssocItemId::TypeAliasId(_) => (),
}
}
}
2019-11-27 12:31:51 -06:00
ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
2019-11-27 08:46:02 -06:00
_ => (),
}
}
for &impl_id in crate_def_map[module_id].impls.iter() {
let impl_data = db.impl_data(impl_id);
for &item in impl_data.items.iter() {
match item {
AssocItemId::FunctionId(it) => cb(it.into()),
AssocItemId::ConstId(it) => cb(it.into()),
AssocItemId::TypeAliasId(_) => (),
}
}
}
}
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
}
#[test]
fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
let (mut db, pos) = TestDB::with_position(
"
//- /lib.rs
fn foo() -> i32 {
<|>1 + 1
}
",
);
{
let events = db.log_executed(|| {
2019-11-27 08:46:02 -06:00
let module = db.module_for_file(pos.file_id);
let crate_def_map = db.crate_def_map(module.krate);
2019-11-27 12:31:51 -06:00
visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
2019-11-27 08:46:02 -06:00
db.infer(def);
});
});
assert!(format!("{:?}", events).contains("infer"))
}
let new_text = "
fn foo() -> i32 {
1
+
1
}
"
.to_string();
2019-02-08 05:49:43 -06:00
db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text));
{
let events = db.log_executed(|| {
2019-11-27 08:46:02 -06:00
let module = db.module_for_file(pos.file_id);
let crate_def_map = db.crate_def_map(module.krate);
2019-11-27 12:31:51 -06:00
visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
2019-11-27 08:46:02 -06:00
db.infer(def);
});
});
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
}
}
2019-03-24 03:39:47 -05:00
#[test]
fn no_such_field_diagnostics() {
let diagnostics = TestDB::with_files(
2019-03-24 03:39:47 -05:00
r"
//- /lib.rs
struct S { foo: i32, bar: () }
impl S {
fn new() -> S {
S {
foo: 92,
baz: 62,
}
}
}
",
)
.diagnostics();
2019-08-29 08:49:10 -05:00
assert_snapshot!(diagnostics, @r###"
"baz: 62": no such field
"{\n foo: 92,\n baz: 62,\n }": Missing structure fields:
- bar
2019-08-29 08:49:10 -05:00
"###
2019-03-24 03:39:47 -05:00
);
}