2019-11-15 15:56:51 -06:00
|
|
|
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;
|
2020-04-25 09:57:59 -05:00
|
|
|
mod display_source_code;
|
2019-11-15 15:56:51 -06:00
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use std::sync::Arc;
|
2018-12-20 14:56:28 -06:00
|
|
|
|
2020-07-20 11:38:52 -05:00
|
|
|
use expect::Expect;
|
2019-11-27 08:46:02 -06:00
|
|
|
use hir_def::{
|
2020-03-06 07:44:44 -06:00
|
|
|
body::{BodySourceMap, SyntheticSyntax},
|
|
|
|
child_by_source::ChildBySource,
|
|
|
|
db::DefDatabase,
|
|
|
|
item_scope::ItemScope,
|
|
|
|
keys,
|
|
|
|
nameres::CrateDefMap,
|
2020-06-29 10:22:47 -05:00
|
|
|
AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
|
2019-11-27 08:46:02 -06:00
|
|
|
};
|
2020-04-23 14:23:36 -05:00
|
|
|
use hir_expand::{db::AstDatabase, InFile};
|
2020-07-07 03:14:48 -05:00
|
|
|
use ra_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_syntax::{
|
|
|
|
algo,
|
2020-04-09 16:35:05 -05:00
|
|
|
ast::{self, AstNode},
|
2020-04-23 14:23:36 -05:00
|
|
|
SyntaxNode,
|
2019-07-04 15:05:17 -05:00
|
|
|
};
|
2020-03-28 05:20:34 -05:00
|
|
|
use stdx::format_to;
|
2018-12-20 14:56:28 -06:00
|
|
|
|
2020-04-23 14:23:36 -05:00
|
|
|
use crate::{
|
|
|
|
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
|
|
|
|
};
|
2018-12-20 14:56:28 -06:00
|
|
|
|
2018-12-24 08:36:54 -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.
|
2018-12-24 08:36:54 -06:00
|
|
|
|
2020-07-10 11:30:32 -05:00
|
|
|
fn setup_tracing() -> tracing::subscriber::DefaultGuard {
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
|
|
|
|
use tracing_tree::HierarchicalLayer;
|
|
|
|
let filter = EnvFilter::from_env("CHALK_DEBUG");
|
2020-07-12 08:26:02 -05:00
|
|
|
let layer = HierarchicalLayer::default()
|
|
|
|
.with_indent_lines(true)
|
|
|
|
.with_ansi(false)
|
|
|
|
.with_indent_amount(2)
|
|
|
|
.with_writer(std::io::stderr);
|
2020-07-10 11:30:32 -05:00
|
|
|
let subscriber = Registry::default().with(filter).with(layer);
|
|
|
|
tracing::subscriber::set_default(subscriber)
|
|
|
|
}
|
|
|
|
|
2020-06-29 07:21:57 -05:00
|
|
|
fn check_types(ra_fixture: &str) {
|
2020-06-29 10:22:47 -05:00
|
|
|
check_types_impl(ra_fixture, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_types_source_code(ra_fixture: &str) {
|
|
|
|
check_types_impl(ra_fixture, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_types_impl(ra_fixture: &str, display_source: bool) {
|
2020-07-10 11:30:32 -05:00
|
|
|
let _tracing = setup_tracing();
|
2020-06-29 07:21:57 -05:00
|
|
|
let db = TestDB::with_files(ra_fixture);
|
|
|
|
let mut checked_one = false;
|
2020-06-30 05:14:16 -05:00
|
|
|
for (file_id, annotations) in db.extract_annotations() {
|
2020-06-29 10:22:47 -05:00
|
|
|
for (range, expected) in annotations {
|
|
|
|
let ty = type_at_range(&db, FileRange { file_id, range });
|
|
|
|
let actual = if display_source {
|
|
|
|
let module = db.module_for_file(file_id);
|
|
|
|
ty.display_source_code(&db, module).unwrap()
|
|
|
|
} else {
|
|
|
|
ty.display(&db).to_string()
|
|
|
|
};
|
2020-06-29 07:21:57 -05:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
checked_one = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert!(checked_one, "no `//^` annotations found");
|
|
|
|
}
|
|
|
|
|
2020-06-29 10:22:47 -05:00
|
|
|
fn type_at_range(db: &TestDB, pos: FileRange) -> Ty {
|
2019-05-28 10:07:39 -05:00
|
|
|
let file = db.parse(pos.file_id).ok().unwrap();
|
2020-06-29 10:22:47 -05:00
|
|
|
let expr = algo::find_node_at_range::<ast::Expr>(file.syntax(), pos.range).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 16:34:12 -06:00
|
|
|
let func = *module.child_by_source(db)[keys::FUNCTION]
|
|
|
|
.get(&InFile::new(pos.file_id.into(), fn_def))
|
|
|
|
.unwrap();
|
2019-12-05 14:17:17 -06:00
|
|
|
|
|
|
|
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());
|
2020-06-29 10:22:47 -05:00
|
|
|
return infer[expr_id].clone();
|
2019-11-27 08:46:02 -06:00
|
|
|
}
|
|
|
|
panic!("Can't find expression")
|
2019-02-23 05:32:25 -06:00
|
|
|
}
|
|
|
|
|
2020-02-27 09:05:35 -06:00
|
|
|
fn infer(ra_fixture: &str) -> String {
|
|
|
|
infer_with_mismatches(ra_fixture, false)
|
2019-12-05 16:02:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
|
2020-07-10 11:30:32 -05:00
|
|
|
let _tracing = setup_tracing();
|
2019-11-04 13:21:15 -06:00
|
|
|
let (db, file_id) = TestDB::with_single_file(content);
|
2019-04-18 13:35:47 -05:00
|
|
|
|
2020-03-28 05:20:34 -05:00
|
|
|
let mut buf = String::new();
|
2019-03-30 06:17:31 -05:00
|
|
|
|
|
|
|
let mut infer_def = |inference_result: Arc<InferenceResult>,
|
|
|
|
body_source_map: Arc<BodySourceMap>| {
|
2020-04-23 14:23:36 -05:00
|
|
|
let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
|
|
|
|
let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
|
2019-03-30 06:17:31 -05:00
|
|
|
|
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) {
|
2020-03-06 07:44:44 -06:00
|
|
|
Ok(sp) => {
|
2020-04-23 14:23:36 -05:00
|
|
|
let root = db.parse_or_expand(sp.file_id).unwrap();
|
|
|
|
sp.map(|ptr| {
|
|
|
|
ptr.either(
|
|
|
|
|it| it.to_node(&root).syntax().clone(),
|
|
|
|
|it| it.to_node(&root).syntax().clone(),
|
|
|
|
)
|
|
|
|
})
|
2019-09-03 03:04:38 -05:00
|
|
|
}
|
2020-03-06 07:44:44 -06:00
|
|
|
Err(SyntheticSyntax) => 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-03-30 06:17:31 -05:00
|
|
|
|
2019-01-06 16:57:39 -06:00
|
|
|
for (expr, ty) in inference_result.type_of_expr.iter() {
|
2020-04-23 14:23:36 -05:00
|
|
|
let node = match body_source_map.expr_syntax(expr) {
|
|
|
|
Ok(sp) => {
|
|
|
|
let root = db.parse_or_expand(sp.file_id).unwrap();
|
|
|
|
sp.map(|ptr| ptr.to_node(&root).syntax().clone())
|
|
|
|
}
|
2020-03-06 07:44:44 -06:00
|
|
|
Err(SyntheticSyntax) => continue,
|
2019-01-06 09:47:59 -06:00
|
|
|
};
|
2020-04-23 14:23:36 -05:00
|
|
|
types.push((node.clone(), ty));
|
2019-12-05 16:02:31 -06:00
|
|
|
if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
|
2020-04-23 14:23:36 -05:00
|
|
|
mismatches.push((node, mismatch));
|
2019-12-05 16:02:31 -06:00
|
|
|
}
|
2019-01-06 09:47:59 -06:00
|
|
|
}
|
2019-04-18 13:35:47 -05:00
|
|
|
|
2019-01-06 16:01:33 -06:00
|
|
|
// sort ranges for consistency
|
2020-04-23 14:23:36 -05:00
|
|
|
types.sort_by_key(|(node, _)| {
|
|
|
|
let range = node.value.text_range();
|
|
|
|
(range.start(), range.end())
|
2019-11-20 00:40:36 -06:00
|
|
|
});
|
2020-04-23 14:23:36 -05:00
|
|
|
for (node, ty) in &types {
|
|
|
|
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
|
2020-04-09 16:35:05 -05:00
|
|
|
(self_param.self_token().unwrap().text_range(), "self".to_string())
|
2019-03-30 05:25:53 -05:00
|
|
|
} else {
|
2020-04-23 14:23:36 -05:00
|
|
|
(node.value.text_range(), node.value.text().to_string().replace("\n", " "))
|
2019-03-30 05:25:53 -05:00
|
|
|
};
|
2020-04-23 14:23:36 -05:00
|
|
|
let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
|
2020-03-28 05:20:34 -05:00
|
|
|
format_to!(
|
|
|
|
buf,
|
2020-04-24 16:40:41 -05:00
|
|
|
"{}{:?} '{}': {}\n",
|
2019-09-03 03:04:38 -05:00
|
|
|
macro_prefix,
|
|
|
|
range,
|
|
|
|
ellipsize(text, 15),
|
|
|
|
ty.display(&db)
|
2020-03-28 05:20:34 -05:00
|
|
|
);
|
2018-12-20 14:56:28 -06:00
|
|
|
}
|
2019-12-05 16:02:31 -06:00
|
|
|
if include_mismatches {
|
2020-04-23 14:23:36 -05:00
|
|
|
mismatches.sort_by_key(|(node, _)| {
|
|
|
|
let range = node.value.text_range();
|
|
|
|
(range.start(), range.end())
|
2019-12-05 16:02:31 -06:00
|
|
|
});
|
|
|
|
for (src_ptr, mismatch) in &mismatches {
|
2020-04-23 14:23:36 -05:00
|
|
|
let range = src_ptr.value.text_range();
|
2019-12-05 16:02:31 -06:00
|
|
|
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
|
2020-03-28 05:20:34 -05:00
|
|
|
format_to!(
|
|
|
|
buf,
|
2020-04-24 16:40:41 -05:00
|
|
|
"{}{:?}: expected {}, got {}\n",
|
2019-12-05 16:02:31 -06:00
|
|
|
macro_prefix,
|
|
|
|
range,
|
|
|
|
mismatch.expected.display(&db),
|
|
|
|
mismatch.actual.display(&db),
|
2020-03-28 05:20:34 -05:00
|
|
|
);
|
2019-12-05 16:02:31 -06:00
|
|
|
}
|
|
|
|
}
|
2019-03-30 06:17:31 -05: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) => {
|
2020-06-22 08:07:06 -05:00
|
|
|
let loc = it.lookup(&db);
|
|
|
|
let tree = db.item_tree(loc.id.file_id);
|
2020-06-23 13:20:30 -05:00
|
|
|
tree.source(&db, loc.id).syntax().text_range().start()
|
2019-11-27 08:46:02 -06:00
|
|
|
}
|
|
|
|
DefWithBodyId::ConstId(it) => {
|
2020-06-22 08:07:06 -05:00
|
|
|
let loc = it.lookup(&db);
|
|
|
|
let tree = db.item_tree(loc.id.file_id);
|
2020-06-23 13:20:30 -05:00
|
|
|
tree.source(&db, loc.id).syntax().text_range().start()
|
2019-11-27 08:46:02 -06:00
|
|
|
}
|
|
|
|
DefWithBodyId::StaticId(it) => {
|
2020-06-22 08:07:06 -05:00
|
|
|
let loc = it.lookup(&db);
|
|
|
|
let tree = db.item_tree(loc.id.file_id);
|
2020-06-23 13:20:30 -05:00
|
|
|
tree.source(&db, loc.id).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-30 06:17:31 -05:00
|
|
|
}
|
|
|
|
|
2020-03-28 05:20:34 -05:00
|
|
|
buf.truncate(buf.trim_end().len());
|
|
|
|
buf
|
2018-12-23 05:05:54 -06:00
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
) {
|
2019-12-29 07:46:24 -06:00
|
|
|
visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb);
|
2019-12-20 08:58:20 -06:00
|
|
|
for impl_id in crate_def_map[module_id].scope.impls() {
|
2019-11-27 08:46:02 -06:00
|
|
|
let impl_data = db.impl_data(impl_id);
|
|
|
|
for &item in impl_data.items.iter() {
|
|
|
|
match item {
|
2019-12-29 07:46:24 -06:00
|
|
|
AssocItemId::FunctionId(it) => {
|
|
|
|
let def = it.into();
|
|
|
|
cb(def);
|
|
|
|
let body = db.body(def);
|
|
|
|
visit_scope(db, crate_def_map, &body.item_scope, cb);
|
|
|
|
}
|
|
|
|
AssocItemId::ConstId(it) => {
|
|
|
|
let def = it.into();
|
|
|
|
cb(def);
|
|
|
|
let body = db.body(def);
|
|
|
|
visit_scope(db, crate_def_map, &body.item_scope, cb);
|
|
|
|
}
|
2019-11-27 08:46:02 -06:00
|
|
|
AssocItemId::TypeAliasId(_) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 07:46:24 -06:00
|
|
|
|
|
|
|
fn visit_scope(
|
|
|
|
db: &TestDB,
|
|
|
|
crate_def_map: &CrateDefMap,
|
|
|
|
scope: &ItemScope,
|
|
|
|
cb: &mut dyn FnMut(DefWithBodyId),
|
|
|
|
) {
|
|
|
|
for decl in scope.declarations() {
|
|
|
|
match decl {
|
|
|
|
ModuleDefId::FunctionId(it) => {
|
|
|
|
let def = it.into();
|
|
|
|
cb(def);
|
|
|
|
let body = db.body(def);
|
|
|
|
visit_scope(db, crate_def_map, &body.item_scope, cb);
|
|
|
|
}
|
|
|
|
ModuleDefId::ConstId(it) => {
|
|
|
|
let def = it.into();
|
|
|
|
cb(def);
|
|
|
|
let body = db.body(def);
|
|
|
|
visit_scope(db, crate_def_map, &body.item_scope, cb);
|
|
|
|
}
|
|
|
|
ModuleDefId::StaticId(it) => {
|
|
|
|
let def = it.into();
|
|
|
|
cb(def);
|
|
|
|
let body = db.body(def);
|
|
|
|
visit_scope(db, crate_def_map, &body.item_scope, cb);
|
|
|
|
}
|
|
|
|
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(_) => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-27 08:46:02 -06:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-05 06:42:47 -06:00
|
|
|
#[test]
|
|
|
|
fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
|
2019-11-04 13:21:15 -06:00
|
|
|
let (mut db, pos) = TestDB::with_position(
|
2019-01-05 06:42:47 -06:00
|
|
|
"
|
|
|
|
//- /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);
|
|
|
|
});
|
2019-01-05 06:42:47 -06:00
|
|
|
});
|
|
|
|
assert!(format!("{:?}", events).contains("infer"))
|
|
|
|
}
|
|
|
|
|
|
|
|
let new_text = "
|
|
|
|
fn foo() -> i32 {
|
|
|
|
1
|
|
|
|
+
|
|
|
|
1
|
|
|
|
}
|
|
|
|
"
|
|
|
|
.to_string();
|
|
|
|
|
2020-07-07 03:14:48 -05:00
|
|
|
db.set_file_text(pos.file_id, Arc::new(new_text));
|
2019-01-05 06:42:47 -06:00
|
|
|
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
});
|
2019-01-05 06:42:47 -06:00
|
|
|
});
|
|
|
|
assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 11:38:52 -05:00
|
|
|
|
|
|
|
// Infer with some common definitions and impls.
|
|
|
|
fn check_infer(ra_fixture: &str, expect: Expect) {
|
|
|
|
let defs = r#"
|
|
|
|
#[lang = "sized"]
|
|
|
|
pub trait Sized {}
|
|
|
|
#[lang = "unsize"]
|
|
|
|
pub trait Unsize<T: ?Sized> {}
|
|
|
|
#[lang = "coerce_unsized"]
|
|
|
|
pub trait CoerceUnsized<T> {}
|
|
|
|
|
|
|
|
impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
|
|
|
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
|
|
|
|
"#;
|
|
|
|
|
|
|
|
// Append to the end to keep positions unchanged.
|
|
|
|
let mut actual = infer(&format!("{}{}", ra_fixture, defs));
|
|
|
|
actual.push('\n');
|
|
|
|
expect.assert_eq(&actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
|
|
|
|
let mut actual = infer_with_mismatches(ra_fixture, true);
|
|
|
|
actual.push('\n');
|
|
|
|
expect.assert_eq(&actual);
|
|
|
|
}
|