9 mod display_source_code;
13 use std::{collections::HashMap, env, sync::Arc};
15 use base_db::{fixture::WithFixture, FileRange, SourceDatabaseExt};
16 use expect_test::Expect;
18 body::{Body, BodySourceMap, SyntheticSyntax},
19 db::{DefDatabase, InternDatabase},
20 expr::{ExprId, PatId},
21 item_scope::ItemScope,
24 AssocItemId, DefWithBodyId, HasModule, LocalModuleId, Lookup, ModuleDefId,
26 use hir_expand::{db::AstDatabase, InFile};
27 use once_cell::race::OnceBool;
30 ast::{self, AstNode, HasName},
33 use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
34 use tracing_tree::HierarchicalLayer;
39 infer::{Adjustment, TypeMismatch},
44 // These tests compare the inference results for all expressions in a file
45 // against snapshots of the expected results using expect. Use
46 // `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
48 fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
49 static ENABLE: OnceBool = OnceBool::new();
50 if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
54 let filter = EnvFilter::from_env("CHALK_DEBUG");
55 let layer = HierarchicalLayer::default()
56 .with_indent_lines(true)
58 .with_indent_amount(2)
59 .with_writer(std::io::stderr);
60 let subscriber = Registry::default().with(filter).with(layer);
61 Some(tracing::subscriber::set_default(subscriber))
64 fn check_types(ra_fixture: &str) {
65 check_impl(ra_fixture, false, true, false)
68 fn check_types_source_code(ra_fixture: &str) {
69 check_impl(ra_fixture, false, true, true)
72 fn check_no_mismatches(ra_fixture: &str) {
73 check_impl(ra_fixture, true, false, false)
76 fn check(ra_fixture: &str) {
77 check_impl(ra_fixture, false, false, false)
80 fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_source: bool) {
81 let _tracing = setup_tracing();
82 let (db, files) = TestDB::with_many_files(ra_fixture);
84 let mut had_annotations = false;
85 let mut mismatches = HashMap::new();
86 let mut types = HashMap::new();
87 let mut adjustments = HashMap::<_, Vec<_>>::new();
88 for (file_id, annotations) in db.extract_annotations() {
89 for (range, expected) in annotations {
90 let file_range = FileRange { file_id, range };
92 types.insert(file_range, expected);
93 } else if expected.starts_with("type: ") {
94 types.insert(file_range, expected.trim_start_matches("type: ").to_string());
95 } else if expected.starts_with("expected") {
96 mismatches.insert(file_range, expected);
97 } else if expected.starts_with("adjustments: ") {
101 .trim_start_matches("adjustments: ")
103 .map(|it| it.trim().to_string())
104 .filter(|it| !it.is_empty())
108 panic!("unexpected annotation: {}", expected);
110 had_annotations = true;
113 assert!(had_annotations || allow_none, "no `//^` annotations found");
115 let mut defs: Vec<DefWithBodyId> = Vec::new();
116 for file_id in files {
117 let module = db.module_for_file_opt(file_id);
118 let module = match module {
122 let def_map = module.def_map(&db);
123 visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
125 defs.sort_by_key(|def| match def {
126 DefWithBodyId::FunctionId(it) => {
127 let loc = it.lookup(&db);
128 loc.source(&db).value.syntax().text_range().start()
130 DefWithBodyId::ConstId(it) => {
131 let loc = it.lookup(&db);
132 loc.source(&db).value.syntax().text_range().start()
134 DefWithBodyId::StaticId(it) => {
135 let loc = it.lookup(&db);
136 loc.source(&db).value.syntax().text_range().start()
138 DefWithBodyId::VariantId(it) => {
139 let loc = db.lookup_intern_enum(it.parent);
140 loc.source(&db).value.syntax().text_range().start()
143 let mut unexpected_type_mismatches = String::new();
145 let (_body, body_source_map) = db.body_with_source_map(def);
146 let inference_result = db.infer(def);
148 for (pat, ty) in inference_result.type_of_pat.iter() {
149 let node = match pat_node(&body_source_map, pat, &db) {
150 Some(value) => value,
153 let range = node.as_ref().original_file_range(&db);
154 if let Some(expected) = types.remove(&range) {
155 let actual = if display_source {
156 ty.display_source_code(&db, def.module(&db)).unwrap()
158 ty.display_test(&db).to_string()
160 assert_eq!(actual, expected);
164 for (expr, ty) in inference_result.type_of_expr.iter() {
165 let node = match expr_node(&body_source_map, expr, &db) {
166 Some(value) => value,
169 let range = node.as_ref().original_file_range(&db);
170 if let Some(expected) = types.remove(&range) {
171 let actual = if display_source {
172 ty.display_source_code(&db, def.module(&db)).unwrap()
174 ty.display_test(&db).to_string()
176 assert_eq!(actual, expected);
178 if let Some(expected) = adjustments.remove(&range) {
179 if let Some(adjustments) = inference_result.expr_adjustments.get(&expr) {
184 .map(|Adjustment { kind, .. }| format!("{:?}", kind))
188 panic!("expected {:?} adjustments, found none", expected);
193 for (pat, mismatch) in inference_result.pat_type_mismatches() {
194 let node = match pat_node(&body_source_map, pat, &db) {
195 Some(value) => value,
198 let range = node.as_ref().original_file_range(&db);
199 let actual = format!(
200 "expected {}, got {}",
201 mismatch.expected.display_test(&db),
202 mismatch.actual.display_test(&db)
204 match mismatches.remove(&range) {
205 Some(annotation) => assert_eq!(actual, annotation),
206 None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
209 for (expr, mismatch) in inference_result.expr_type_mismatches() {
210 let node = match body_source_map.expr_syntax(expr) {
212 let root = db.parse_or_expand(sp.file_id).unwrap();
213 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
215 Err(SyntheticSyntax) => continue,
217 let range = node.as_ref().original_file_range(&db);
218 let actual = format!(
219 "expected {}, got {}",
220 mismatch.expected.display_test(&db),
221 mismatch.actual.display_test(&db)
223 match mismatches.remove(&range) {
224 Some(annotation) => assert_eq!(actual, annotation),
225 None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
230 let mut buf = String::new();
231 if !unexpected_type_mismatches.is_empty() {
232 format_to!(buf, "Unexpected type mismatches:\n{}", unexpected_type_mismatches);
234 if !mismatches.is_empty() {
235 format_to!(buf, "Unchecked mismatch annotations:\n");
236 for m in mismatches {
237 format_to!(buf, "{:?}: {}\n", m.0.range, m.1);
240 if !types.is_empty() {
241 format_to!(buf, "Unchecked type annotations:\n");
243 format_to!(buf, "{:?}: type {}\n", t.0.range, t.1);
246 if !adjustments.is_empty() {
247 format_to!(buf, "Unchecked adjustments annotations:\n");
248 for t in adjustments {
249 format_to!(buf, "{:?}: type {:?}\n", t.0.range, t.1);
252 assert!(buf.is_empty(), "{}", buf);
256 body_source_map: &BodySourceMap,
259 ) -> Option<InFile<SyntaxNode>> {
260 Some(match body_source_map.expr_syntax(expr) {
262 let root = db.parse_or_expand(sp.file_id).unwrap();
263 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
265 Err(SyntheticSyntax) => return None,
270 body_source_map: &BodySourceMap,
273 ) -> Option<InFile<SyntaxNode>> {
274 Some(match body_source_map.pat_syntax(pat) {
276 let root = db.parse_or_expand(sp.file_id).unwrap();
279 |it| it.to_node(&root).syntax().clone(),
280 |it| it.to_node(&root).syntax().clone(),
284 Err(SyntheticSyntax) => return None,
288 fn infer(ra_fixture: &str) -> String {
289 infer_with_mismatches(ra_fixture, false)
292 fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
293 let _tracing = setup_tracing();
294 let (db, file_id) = TestDB::with_single_file(content);
296 let mut buf = String::new();
298 let mut infer_def = |inference_result: Arc<InferenceResult>,
299 body_source_map: Arc<BodySourceMap>| {
300 let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
301 let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
303 for (pat, ty) in inference_result.type_of_pat.iter() {
304 let syntax_ptr = match body_source_map.pat_syntax(pat) {
306 let root = db.parse_or_expand(sp.file_id).unwrap();
309 |it| it.to_node(&root).syntax().clone(),
310 |it| it.to_node(&root).syntax().clone(),
314 Err(SyntheticSyntax) => continue,
316 types.push((syntax_ptr.clone(), ty));
317 if let Some(mismatch) = inference_result.type_mismatch_for_pat(pat) {
318 mismatches.push((syntax_ptr, mismatch));
322 for (expr, ty) in inference_result.type_of_expr.iter() {
323 let node = match body_source_map.expr_syntax(expr) {
325 let root = db.parse_or_expand(sp.file_id).unwrap();
326 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
328 Err(SyntheticSyntax) => continue,
330 types.push((node.clone(), ty));
331 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
332 mismatches.push((node, mismatch));
336 // sort ranges for consistency
337 types.sort_by_key(|(node, _)| {
338 let range = node.value.text_range();
339 (range.start(), range.end())
341 for (node, ty) in &types {
342 let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
343 (self_param.name().unwrap().syntax().text_range(), "self".to_string())
345 (node.value.text_range(), node.value.text().to_string().replace('\n', " "))
347 let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
357 if include_mismatches {
358 mismatches.sort_by_key(|(node, _)| {
359 let range = node.value.text_range();
360 (range.start(), range.end())
362 for (src_ptr, mismatch) in &mismatches {
363 let range = src_ptr.value.text_range();
364 let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
367 "{}{:?}: expected {}, got {}\n",
370 mismatch.expected.display_test(&db),
371 mismatch.actual.display_test(&db),
377 let module = db.module_for_file(file_id);
378 let def_map = module.def_map(&db);
380 let mut defs: Vec<DefWithBodyId> = Vec::new();
381 visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
382 defs.sort_by_key(|def| match def {
383 DefWithBodyId::FunctionId(it) => {
384 let loc = it.lookup(&db);
385 loc.source(&db).value.syntax().text_range().start()
387 DefWithBodyId::ConstId(it) => {
388 let loc = it.lookup(&db);
389 loc.source(&db).value.syntax().text_range().start()
391 DefWithBodyId::StaticId(it) => {
392 let loc = it.lookup(&db);
393 loc.source(&db).value.syntax().text_range().start()
395 DefWithBodyId::VariantId(it) => {
396 let loc = db.lookup_intern_enum(it.parent);
397 loc.source(&db).value.syntax().text_range().start()
401 let (_body, source_map) = db.body_with_source_map(def);
402 let infer = db.infer(def);
403 infer_def(infer, source_map);
406 buf.truncate(buf.trim_end().len());
412 crate_def_map: &DefMap,
413 module_id: LocalModuleId,
414 cb: &mut dyn FnMut(DefWithBodyId),
416 visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb);
417 for impl_id in crate_def_map[module_id].scope.impls() {
418 let impl_data = db.impl_data(impl_id);
419 for &item in impl_data.items.iter() {
421 AssocItemId::FunctionId(it) => {
424 let body = db.body(def);
425 visit_body(db, &body, cb);
427 AssocItemId::ConstId(it) => {
430 let body = db.body(def);
431 visit_body(db, &body, cb);
433 AssocItemId::TypeAliasId(_) => (),
440 crate_def_map: &DefMap,
442 cb: &mut dyn FnMut(DefWithBodyId),
444 for decl in scope.declarations() {
446 ModuleDefId::FunctionId(it) => {
449 let body = db.body(def);
450 visit_body(db, &body, cb);
452 ModuleDefId::ConstId(it) => {
455 let body = db.body(def);
456 visit_body(db, &body, cb);
458 ModuleDefId::StaticId(it) => {
461 let body = db.body(def);
462 visit_body(db, &body, cb);
464 ModuleDefId::AdtId(hir_def::AdtId::EnumId(it)) => {
468 .map(|(id, _)| hir_def::EnumVariantId { parent: it, local_id: id })
472 let body = db.body(def);
473 visit_body(db, &body, cb);
476 ModuleDefId::TraitId(it) => {
477 let trait_data = db.trait_data(it);
478 for &(_, item) in trait_data.items.iter() {
480 AssocItemId::FunctionId(it) => cb(it.into()),
481 AssocItemId::ConstId(it) => cb(it.into()),
482 AssocItemId::TypeAliasId(_) => (),
486 ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
492 fn visit_body(db: &TestDB, body: &Body, cb: &mut dyn FnMut(DefWithBodyId)) {
493 for (_, def_map) in body.blocks(db) {
494 for (mod_id, _) in def_map.modules() {
495 visit_module(db, &def_map, mod_id, cb);
501 fn ellipsize(mut text: String, max_len: usize) -> String {
502 if text.len() <= max_len {
505 let ellipsis = "...";
506 let e_len = ellipsis.len();
507 let mut prefix_len = (max_len - e_len) / 2;
508 while !text.is_char_boundary(prefix_len) {
511 let mut suffix_len = max_len - e_len - prefix_len;
512 while !text.is_char_boundary(text.len() - suffix_len) {
515 text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
519 fn check_infer(ra_fixture: &str, expect: Expect) {
520 let mut actual = infer(ra_fixture);
522 expect.assert_eq(&actual);
525 fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
526 let mut actual = infer_with_mismatches(ra_fixture, true);
528 expect.assert_eq(&actual);
533 let (mut db, pos) = TestDB::with_position(
540 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
542 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
545 fn len(&self) -> usize;
548 pub trait UnificationStoreMut: UnificationStoreBase {
549 fn push(&mut self, value: Self::Key);
559 let module = db.module_for_file(pos.file_id);
560 let crate_def_map = module.def_map(&db);
561 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
571 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
573 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
576 fn len(&self) -> usize;
579 pub trait UnificationStoreMut: UnificationStoreBase {
580 fn push(&mut self, value: Self::Key);
591 db.set_file_text(pos.file_id, Arc::new(new_text));
593 let module = db.module_for_file(pos.file_id);
594 let crate_def_map = module.def_map(&db);
595 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {