9 mod display_source_code;
11 use std::{env, sync::Arc};
13 use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
14 use expect_test::Expect;
16 body::{Body, BodySourceMap, SyntheticSyntax},
17 child_by_source::ChildBySource,
19 item_scope::ItemScope,
23 AssocItemId, DefWithBodyId, LocalModuleId, Lookup, ModuleDefId,
25 use hir_expand::{db::AstDatabase, InFile};
26 use once_cell::race::OnceBool;
30 ast::{self, AstNode, NameOwner},
33 use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
34 use tracing_tree::HierarchicalLayer;
37 db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
40 // These tests compare the inference results for all expressions in a file
41 // against snapshots of the expected results using expect. Use
42 // `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
44 fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
45 static ENABLE: OnceBool = OnceBool::new();
46 if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
50 let filter = EnvFilter::from_env("CHALK_DEBUG");
51 let layer = HierarchicalLayer::default()
52 .with_indent_lines(true)
54 .with_indent_amount(2)
55 .with_writer(std::io::stderr);
56 let subscriber = Registry::default().with(filter).with(layer);
57 Some(tracing::subscriber::set_default(subscriber))
60 fn check_types(ra_fixture: &str) {
61 check_types_impl(ra_fixture, false)
64 fn check_types_source_code(ra_fixture: &str) {
65 check_types_impl(ra_fixture, true)
68 fn check_types_impl(ra_fixture: &str, display_source: bool) {
69 let _tracing = setup_tracing();
70 let db = TestDB::with_files(ra_fixture);
71 let mut checked_one = false;
72 for (file_id, annotations) in db.extract_annotations() {
73 for (range, expected) in annotations {
74 let ty = type_at_range(&db, FileRange { file_id, range });
75 let actual = if display_source {
76 let module = db.module_for_file(file_id);
77 ty.display_source_code(&db, module).unwrap()
79 ty.display_test(&db).to_string()
81 assert_eq!(expected, actual);
85 assert!(checked_one, "no `//^` annotations found");
88 fn type_at_range(db: &TestDB, pos: FileRange) -> Ty {
89 let file = db.parse(pos.file_id).ok().unwrap();
90 let expr = algo::find_node_at_range::<ast::Expr>(file.syntax(), pos.range).unwrap();
91 let fn_def = expr.syntax().ancestors().find_map(ast::Fn::cast).unwrap();
92 let module = db.module_for_file(pos.file_id);
93 let func = *module.child_by_source(db)[keys::FUNCTION]
94 .get(&InFile::new(pos.file_id.into(), fn_def))
97 let (_body, source_map) = db.body_with_source_map(func.into());
98 if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) {
99 let infer = db.infer(func.into());
100 return infer[expr_id].clone();
102 panic!("Can't find expression")
105 fn infer(ra_fixture: &str) -> String {
106 infer_with_mismatches(ra_fixture, false)
109 fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
110 let _tracing = setup_tracing();
111 let (db, file_id) = TestDB::with_single_file(content);
113 let mut buf = String::new();
115 let mut infer_def = |inference_result: Arc<InferenceResult>,
116 body_source_map: Arc<BodySourceMap>| {
117 let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
118 let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
120 for (pat, ty) in inference_result.type_of_pat.iter() {
121 let syntax_ptr = match body_source_map.pat_syntax(pat) {
123 let root = db.parse_or_expand(sp.file_id).unwrap();
126 |it| it.to_node(&root).syntax().clone(),
127 |it| it.to_node(&root).syntax().clone(),
131 Err(SyntheticSyntax) => continue,
133 types.push((syntax_ptr.clone(), ty));
134 if let Some(mismatch) = inference_result.type_mismatch_for_pat(pat) {
135 mismatches.push((syntax_ptr, mismatch));
139 for (expr, ty) in inference_result.type_of_expr.iter() {
140 let node = match body_source_map.expr_syntax(expr) {
142 let root = db.parse_or_expand(sp.file_id).unwrap();
143 sp.map(|ptr| ptr.to_node(&root).syntax().clone())
145 Err(SyntheticSyntax) => continue,
147 types.push((node.clone(), ty));
148 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
149 mismatches.push((node, mismatch));
153 // sort ranges for consistency
154 types.sort_by_key(|(node, _)| {
155 let range = node.value.text_range();
156 (range.start(), range.end())
158 for (node, ty) in &types {
159 let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
160 (self_param.name().unwrap().syntax().text_range(), "self".to_string())
162 (node.value.text_range(), node.value.text().to_string().replace("\n", " "))
164 let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
174 if include_mismatches {
175 mismatches.sort_by_key(|(node, _)| {
176 let range = node.value.text_range();
177 (range.start(), range.end())
179 for (src_ptr, mismatch) in &mismatches {
180 let range = src_ptr.value.text_range();
181 let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
184 "{}{:?}: expected {}, got {}\n",
187 mismatch.expected.display_test(&db),
188 mismatch.actual.display_test(&db),
194 let module = db.module_for_file(file_id);
195 let def_map = module.def_map(&db);
197 let mut defs: Vec<DefWithBodyId> = Vec::new();
198 visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
199 defs.sort_by_key(|def| match def {
200 DefWithBodyId::FunctionId(it) => {
201 let loc = it.lookup(&db);
202 loc.source(&db).value.syntax().text_range().start()
204 DefWithBodyId::ConstId(it) => {
205 let loc = it.lookup(&db);
206 loc.source(&db).value.syntax().text_range().start()
208 DefWithBodyId::StaticId(it) => {
209 let loc = it.lookup(&db);
210 loc.source(&db).value.syntax().text_range().start()
214 let (_body, source_map) = db.body_with_source_map(def);
215 let infer = db.infer(def);
216 infer_def(infer, source_map);
219 buf.truncate(buf.trim_end().len());
225 crate_def_map: &DefMap,
226 module_id: LocalModuleId,
227 cb: &mut dyn FnMut(DefWithBodyId),
229 visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb);
230 for impl_id in crate_def_map[module_id].scope.impls() {
231 let impl_data = db.impl_data(impl_id);
232 for &item in impl_data.items.iter() {
234 AssocItemId::FunctionId(it) => {
237 let body = db.body(def);
238 visit_body(db, &body, cb);
240 AssocItemId::ConstId(it) => {
243 let body = db.body(def);
244 visit_body(db, &body, cb);
246 AssocItemId::TypeAliasId(_) => (),
253 crate_def_map: &DefMap,
255 cb: &mut dyn FnMut(DefWithBodyId),
257 for decl in scope.declarations() {
259 ModuleDefId::FunctionId(it) => {
262 let body = db.body(def);
263 visit_body(db, &body, cb);
265 ModuleDefId::ConstId(it) => {
268 let body = db.body(def);
269 visit_body(db, &body, cb);
271 ModuleDefId::StaticId(it) => {
274 let body = db.body(def);
275 visit_body(db, &body, cb);
277 ModuleDefId::TraitId(it) => {
278 let trait_data = db.trait_data(it);
279 for &(_, item) in trait_data.items.iter() {
281 AssocItemId::FunctionId(it) => cb(it.into()),
282 AssocItemId::ConstId(it) => cb(it.into()),
283 AssocItemId::TypeAliasId(_) => (),
287 ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
293 fn visit_body(db: &TestDB, body: &Body, cb: &mut dyn FnMut(DefWithBodyId)) {
294 for (_, def_map) in body.blocks(db) {
295 for (mod_id, _) in def_map.modules() {
296 visit_module(db, &def_map, mod_id, cb);
302 fn ellipsize(mut text: String, max_len: usize) -> String {
303 if text.len() <= max_len {
306 let ellipsis = "...";
307 let e_len = ellipsis.len();
308 let mut prefix_len = (max_len - e_len) / 2;
309 while !text.is_char_boundary(prefix_len) {
312 let mut suffix_len = max_len - e_len - prefix_len;
313 while !text.is_char_boundary(text.len() - suffix_len) {
316 text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
321 fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
322 let (mut db, pos) = TestDB::with_position(
331 let events = db.log_executed(|| {
332 let module = db.module_for_file(pos.file_id);
333 let crate_def_map = module.def_map(&db);
334 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
338 assert!(format!("{:?}", events).contains("infer"))
350 db.set_file_text(pos.file_id, Arc::new(new_text));
353 let events = db.log_executed(|| {
354 let module = db.module_for_file(pos.file_id);
355 let crate_def_map = module.def_map(&db);
356 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
360 assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
364 fn check_infer(ra_fixture: &str, expect: Expect) {
365 let mut actual = infer(ra_fixture);
367 expect.assert_eq(&actual);
370 fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
371 let mut actual = infer_with_mismatches(ra_fixture, true);
373 expect.assert_eq(&actual);
378 let (mut db, pos) = TestDB::with_position(
385 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
387 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
390 fn len(&self) -> usize;
393 pub trait UnificationStoreMut: UnificationStoreBase {
394 fn push(&mut self, value: Self::Key);
404 let module = db.module_for_file(pos.file_id);
405 let crate_def_map = module.def_map(&db);
406 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
416 type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
418 pub trait UnificationStoreBase: Index<Output = Key<Self>> {
421 fn len(&self) -> usize;
424 pub trait UnificationStoreMut: UnificationStoreBase {
425 fn push(&mut self, value: Self::Key);
436 db.set_file_text(pos.file_id, Arc::new(new_text));
438 let module = db.module_for_file(pos.file_id);
439 let crate_def_map = module.def_map(&db);
440 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {