9 mod display_source_code;
12 use std::{collections::HashMap, env, sync::Arc};
14 use base_db::{fixture::WithFixture, FileRange, SourceDatabaseExt};
15 use expect_test::Expect;
17 body::{Body, BodySourceMap, SyntheticSyntax},
19 expr::{ExprId, PatId},
20 item_scope::ItemScope,
23 AssocItemId, DefWithBodyId, HasModule, LocalModuleId, Lookup, ModuleDefId,
25 use hir_expand::{db::AstDatabase, InFile};
26 use once_cell::race::OnceBool;
29 ast::{self, AstNode, NameOwner},
32 use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
33 use tracing_tree::HierarchicalLayer;
36 db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
39 // These tests compare the inference results for all expressions in a file
40 // against snapshots of the expected results using expect. Use
41 // `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
43 fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
44 static ENABLE: OnceBool = OnceBool::new();
45 if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
49 let filter = EnvFilter::from_env("CHALK_DEBUG");
50 let layer = HierarchicalLayer::default()
51 .with_indent_lines(true)
53 .with_indent_amount(2)
54 .with_writer(std::io::stderr);
55 let subscriber = Registry::default().with(filter).with(layer);
56 Some(tracing::subscriber::set_default(subscriber))
59 fn check_types(ra_fixture: &str) {
60 check_impl(ra_fixture, false, true, false)
63 fn check_types_source_code(ra_fixture: &str) {
64 check_impl(ra_fixture, false, true, true)
67 fn check_no_mismatches(ra_fixture: &str) {
68 check_impl(ra_fixture, true, false, false)
71 fn check(ra_fixture: &str) {
72 check_impl(ra_fixture, false, false, false)
75 fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_source: bool) {
76 let _tracing = setup_tracing();
77 let (db, files) = TestDB::with_many_files(ra_fixture);
79 let mut had_annotations = false;
80 let mut mismatches = HashMap::new();
81 let mut types = HashMap::new();
82 for (file_id, annotations) in db.extract_annotations() {
83 for (range, expected) in annotations {
84 let file_range = FileRange { file_id, range };
86 types.insert(file_range, expected);
87 } else if expected.starts_with("type: ") {
88 types.insert(file_range, expected.trim_start_matches("type: ").to_string());
89 } else if expected.starts_with("expected") {
90 mismatches.insert(file_range, expected);
92 panic!("unexpected annotation: {}", expected);
94 had_annotations = true;
97 assert!(had_annotations || allow_none, "no `//^` annotations found");
99 let mut defs: Vec<DefWithBodyId> = Vec::new();
100 for file_id in files {
101 let module = db.module_for_file_opt(file_id);
102 let module = match module {
106 let def_map = module.def_map(&db);
107 visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
109 defs.sort_by_key(|def| match def {
110 DefWithBodyId::FunctionId(it) => {
111 let loc = it.lookup(&db);
112 loc.source(&db).value.syntax().text_range().start()
114 DefWithBodyId::ConstId(it) => {
115 let loc = it.lookup(&db);
116 loc.source(&db).value.syntax().text_range().start()
118 DefWithBodyId::StaticId(it) => {
119 let loc = it.lookup(&db);
120 loc.source(&db).value.syntax().text_range().start()
123 let mut unexpected_type_mismatches = String::new();
125 let (_body, body_source_map) = db.body_with_source_map(def);
126 let inference_result = db.infer(def);
128 for (pat, ty) in inference_result.type_of_pat.iter() {
129 let node = match pat_node(&body_source_map, pat, &db) {
130 Some(value) => value,
133 let range = node.as_ref().original_file_range(&db);
134 if let Some(expected) = types.remove(&range) {
135 let actual = if display_source {
136 ty.display_source_code(&db, def.module(&db)).unwrap()
138 ty.display_test(&db).to_string()
140 assert_eq!(actual, expected);
144 for (expr, ty) in inference_result.type_of_expr.iter() {
145 let node = match expr_node(&body_source_map, expr, &db) {
146 Some(value) => value,
149 let range = node.as_ref().original_file_range(&db);
150 if let Some(expected) = types.remove(&range) {
151 let actual = if display_source {
152 ty.display_source_code(&db, def.module(&db)).unwrap()
154 ty.display_test(&db).to_string()
156 assert_eq!(actual, expected);
160 for (pat, mismatch) in inference_result.pat_type_mismatches() {
161 let node = match pat_node(&body_source_map, pat, &db) {
162 Some(value) => value,
165 let range = node.as_ref().original_file_range(&db);
166 let actual = format!(
167 "expected {}, got {}",
168 mismatch.expected.display_test(&db),
169 mismatch.actual.display_test(&db)
171 if let Some(annotation) = mismatches.remove(&range) {
172 assert_eq!(actual, annotation);
174 format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
177 for (expr, mismatch) in inference_result.expr_type_mismatches() {
178 let node = match body_source_map.expr_syntax(expr) {
180 let root = db.parse_or_expand(sp.file_id).unwrap();
181 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
183 Err(SyntheticSyntax) => continue,
185 let range = node.as_ref().original_file_range(&db);
186 let actual = format!(
187 "expected {}, got {}",
188 mismatch.expected.display_test(&db),
189 mismatch.actual.display_test(&db)
191 if let Some(annotation) = mismatches.remove(&range) {
192 assert_eq!(actual, annotation);
194 format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual);
199 let mut buf = String::new();
200 if !unexpected_type_mismatches.is_empty() {
201 format_to!(buf, "Unexpected type mismatches:\n{}", unexpected_type_mismatches);
203 if !mismatches.is_empty() {
204 format_to!(buf, "Unchecked mismatch annotations:\n");
205 for m in mismatches {
206 format_to!(buf, "{:?}: {}\n", m.0.range, m.1);
209 if !types.is_empty() {
210 format_to!(buf, "Unchecked type annotations:\n");
212 format_to!(buf, "{:?}: type {}\n", t.0.range, t.1);
215 assert!(buf.is_empty(), "{}", buf);
219 body_source_map: &BodySourceMap,
222 ) -> Option<InFile<SyntaxNode>> {
223 Some(match body_source_map.expr_syntax(expr) {
225 let root = db.parse_or_expand(sp.file_id).unwrap();
226 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
228 Err(SyntheticSyntax) => return None,
233 body_source_map: &BodySourceMap,
236 ) -> Option<InFile<SyntaxNode>> {
237 Some(match body_source_map.pat_syntax(pat) {
239 let root = db.parse_or_expand(sp.file_id).unwrap();
242 |it| it.to_node(&root).syntax().clone(),
243 |it| it.to_node(&root).syntax().clone(),
247 Err(SyntheticSyntax) => return None,
251 fn infer(ra_fixture: &str) -> String {
252 infer_with_mismatches(ra_fixture, false)
255 fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
256 let _tracing = setup_tracing();
257 let (db, file_id) = TestDB::with_single_file(content);
259 let mut buf = String::new();
261 let mut infer_def = |inference_result: Arc<InferenceResult>,
262 body_source_map: Arc<BodySourceMap>| {
263 let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
264 let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
266 for (pat, ty) in inference_result.type_of_pat.iter() {
267 let syntax_ptr = match body_source_map.pat_syntax(pat) {
269 let root = db.parse_or_expand(sp.file_id).unwrap();
272 |it| it.to_node(&root).syntax().clone(),
273 |it| it.to_node(&root).syntax().clone(),
277 Err(SyntheticSyntax) => continue,
279 types.push((syntax_ptr.clone(), ty));
280 if let Some(mismatch) = inference_result.type_mismatch_for_pat(pat) {
281 mismatches.push((syntax_ptr, mismatch));
285 for (expr, ty) in inference_result.type_of_expr.iter() {
286 let node = match body_source_map.expr_syntax(expr) {
288 let root = db.parse_or_expand(sp.file_id).unwrap();
289 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
291 Err(SyntheticSyntax) => continue,
293 types.push((node.clone(), ty));
294 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
295 mismatches.push((node, mismatch));
299 // sort ranges for consistency
300 types.sort_by_key(|(node, _)| {
301 let range = node.value.text_range();
302 (range.start(), range.end())
304 for (node, ty) in &types {
305 let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
306 (self_param.name().unwrap().syntax().text_range(), "self".to_string())
308 (node.value.text_range(), node.value.text().to_string().replace("\n", " "))
310 let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
320 if include_mismatches {
321 mismatches.sort_by_key(|(node, _)| {
322 let range = node.value.text_range();
323 (range.start(), range.end())
325 for (src_ptr, mismatch) in &mismatches {
326 let range = src_ptr.value.text_range();
327 let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
330 "{}{:?}: expected {}, got {}\n",
333 mismatch.expected.display_test(&db),
334 mismatch.actual.display_test(&db),
340 let module = db.module_for_file(file_id);
341 let def_map = module.def_map(&db);
343 let mut defs: Vec<DefWithBodyId> = Vec::new();
344 visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
345 defs.sort_by_key(|def| match def {
346 DefWithBodyId::FunctionId(it) => {
347 let loc = it.lookup(&db);
348 loc.source(&db).value.syntax().text_range().start()
350 DefWithBodyId::ConstId(it) => {
351 let loc = it.lookup(&db);
352 loc.source(&db).value.syntax().text_range().start()
354 DefWithBodyId::StaticId(it) => {
355 let loc = it.lookup(&db);
356 loc.source(&db).value.syntax().text_range().start()
360 let (_body, source_map) = db.body_with_source_map(def);
361 let infer = db.infer(def);
362 infer_def(infer, source_map);
365 buf.truncate(buf.trim_end().len());
371 crate_def_map: &DefMap,
372 module_id: LocalModuleId,
373 cb: &mut dyn FnMut(DefWithBodyId),
375 visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb);
376 for impl_id in crate_def_map[module_id].scope.impls() {
377 let impl_data = db.impl_data(impl_id);
378 for &item in impl_data.items.iter() {
380 AssocItemId::FunctionId(it) => {
383 let body = db.body(def);
384 visit_body(db, &body, cb);
386 AssocItemId::ConstId(it) => {
389 let body = db.body(def);
390 visit_body(db, &body, cb);
392 AssocItemId::TypeAliasId(_) => (),
399 crate_def_map: &DefMap,
401 cb: &mut dyn FnMut(DefWithBodyId),
403 for decl in scope.declarations() {
405 ModuleDefId::FunctionId(it) => {
408 let body = db.body(def);
409 visit_body(db, &body, cb);
411 ModuleDefId::ConstId(it) => {
414 let body = db.body(def);
415 visit_body(db, &body, cb);
417 ModuleDefId::StaticId(it) => {
420 let body = db.body(def);
421 visit_body(db, &body, cb);
423 ModuleDefId::TraitId(it) => {
424 let trait_data = db.trait_data(it);
425 for &(_, item) in trait_data.items.iter() {
427 AssocItemId::FunctionId(it) => cb(it.into()),
428 AssocItemId::ConstId(it) => cb(it.into()),
429 AssocItemId::TypeAliasId(_) => (),
433 ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
439 fn visit_body(db: &TestDB, body: &Body, cb: &mut dyn FnMut(DefWithBodyId)) {
440 for (_, def_map) in body.blocks(db) {
441 for (mod_id, _) in def_map.modules() {
442 visit_module(db, &def_map, mod_id, cb);
448 fn ellipsize(mut text: String, max_len: usize) -> String {
449 if text.len() <= max_len {
452 let ellipsis = "...";
453 let e_len = ellipsis.len();
454 let mut prefix_len = (max_len - e_len) / 2;
455 while !text.is_char_boundary(prefix_len) {
458 let mut suffix_len = max_len - e_len - prefix_len;
459 while !text.is_char_boundary(text.len() - suffix_len) {
462 text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
466 fn check_infer(ra_fixture: &str, expect: Expect) {
467 let mut actual = infer(ra_fixture);
469 expect.assert_eq(&actual);
472 fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
473 let mut actual = infer_with_mismatches(ra_fixture, true);
475 expect.assert_eq(&actual);
480 let (mut db, pos) = TestDB::with_position(
487 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
489 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
492 fn len(&self) -> usize;
495 pub trait UnificationStoreMut: UnificationStoreBase {
496 fn push(&mut self, value: Self::Key);
506 let module = db.module_for_file(pos.file_id);
507 let crate_def_map = module.def_map(&db);
508 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
518 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
520 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
523 fn len(&self) -> usize;
526 pub trait UnificationStoreMut: UnificationStoreBase {
527 fn push(&mut self, value: Self::Key);
538 db.set_file_text(pos.file_id, Arc::new(new_text));
540 let module = db.module_for_file(pos.file_id);
541 let crate_def_map = module.def_map(&db);
542 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {