]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests.rs
Merge #11538
[rust.git] / crates / hir_ty / src / tests.rs
1 mod never_type;
2 mod coercion;
3 mod regression;
4 mod simple;
5 mod patterns;
6 mod traits;
7 mod method_resolution;
8 mod macros;
9 mod display_source_code;
10 mod incremental;
11
12 use std::{collections::HashMap, env, sync::Arc};
13
14 use base_db::{fixture::WithFixture, FileRange, SourceDatabaseExt};
15 use expect_test::Expect;
16 use hir_def::{
17     body::{Body, BodySourceMap, SyntheticSyntax},
18     db::DefDatabase,
19     expr::{ExprId, PatId},
20     item_scope::ItemScope,
21     nameres::DefMap,
22     src::HasSource,
23     AssocItemId, DefWithBodyId, HasModule, LocalModuleId, Lookup, ModuleDefId,
24 };
25 use hir_expand::{db::AstDatabase, InFile};
26 use once_cell::race::OnceBool;
27 use stdx::format_to;
28 use syntax::{
29     ast::{self, AstNode, HasName},
30     SyntaxNode,
31 };
32 use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
33 use tracing_tree::HierarchicalLayer;
34
35 use crate::{
36     db::HirDatabase,
37     display::HirDisplay,
38     infer::{Adjustment, TypeMismatch},
39     test_db::TestDB,
40     InferenceResult, Ty,
41 };
42
43 // These tests compare the inference results for all expressions in a file
44 // against snapshots of the expected results using expect. Use
45 // `env UPDATE_EXPECT=1 cargo test -p hir_ty` to update the snapshots.
46
47 fn setup_tracing() -> Option<tracing::subscriber::DefaultGuard> {
48     static ENABLE: OnceBool = OnceBool::new();
49     if !ENABLE.get_or_init(|| env::var("CHALK_DEBUG").is_ok()) {
50         return None;
51     }
52
53     let filter = EnvFilter::from_env("CHALK_DEBUG");
54     let layer = HierarchicalLayer::default()
55         .with_indent_lines(true)
56         .with_ansi(false)
57         .with_indent_amount(2)
58         .with_writer(std::io::stderr);
59     let subscriber = Registry::default().with(filter).with(layer);
60     Some(tracing::subscriber::set_default(subscriber))
61 }
62
63 fn check_types(ra_fixture: &str) {
64     check_impl(ra_fixture, false, true, false)
65 }
66
67 fn check_types_source_code(ra_fixture: &str) {
68     check_impl(ra_fixture, false, true, true)
69 }
70
71 fn check_no_mismatches(ra_fixture: &str) {
72     check_impl(ra_fixture, true, false, false)
73 }
74
75 fn check(ra_fixture: &str) {
76     check_impl(ra_fixture, false, false, false)
77 }
78
79 fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_source: bool) {
80     let _tracing = setup_tracing();
81     let (db, files) = TestDB::with_many_files(ra_fixture);
82
83     let mut had_annotations = false;
84     let mut mismatches = HashMap::new();
85     let mut types = HashMap::new();
86     let mut adjustments = HashMap::<_, Vec<_>>::new();
87     for (file_id, annotations) in db.extract_annotations() {
88         for (range, expected) in annotations {
89             let file_range = FileRange { file_id, range };
90             if only_types {
91                 types.insert(file_range, expected);
92             } else if expected.starts_with("type: ") {
93                 types.insert(file_range, expected.trim_start_matches("type: ").to_string());
94             } else if expected.starts_with("expected") {
95                 mismatches.insert(file_range, expected);
96             } else if expected.starts_with("adjustments: ") {
97                 adjustments.insert(
98                     file_range,
99                     expected
100                         .trim_start_matches("adjustments: ")
101                         .split(',')
102                         .map(|it| it.trim().to_string())
103                         .filter(|it| !it.is_empty())
104                         .collect(),
105                 );
106             } else {
107                 panic!("unexpected annotation: {}", expected);
108             }
109             had_annotations = true;
110         }
111     }
112     assert!(had_annotations || allow_none, "no `//^` annotations found");
113
114     let mut defs: Vec<DefWithBodyId> = Vec::new();
115     for file_id in files {
116         let module = db.module_for_file_opt(file_id);
117         let module = match module {
118             Some(m) => m,
119             None => continue,
120         };
121         let def_map = module.def_map(&db);
122         visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
123     }
124     defs.sort_by_key(|def| match def {
125         DefWithBodyId::FunctionId(it) => {
126             let loc = it.lookup(&db);
127             loc.source(&db).value.syntax().text_range().start()
128         }
129         DefWithBodyId::ConstId(it) => {
130             let loc = it.lookup(&db);
131             loc.source(&db).value.syntax().text_range().start()
132         }
133         DefWithBodyId::StaticId(it) => {
134             let loc = it.lookup(&db);
135             loc.source(&db).value.syntax().text_range().start()
136         }
137     });
138     let mut unexpected_type_mismatches = String::new();
139     for def in defs {
140         let (_body, body_source_map) = db.body_with_source_map(def);
141         let inference_result = db.infer(def);
142
143         for (pat, ty) in inference_result.type_of_pat.iter() {
144             let node = match pat_node(&body_source_map, pat, &db) {
145                 Some(value) => value,
146                 None => continue,
147             };
148             let range = node.as_ref().original_file_range(&db);
149             if let Some(expected) = types.remove(&range) {
150                 let actual = if display_source {
151                     ty.display_source_code(&db, def.module(&db)).unwrap()
152                 } else {
153                     ty.display_test(&db).to_string()
154                 };
155                 assert_eq!(actual, expected);
156             }
157         }
158
159         for (expr, ty) in inference_result.type_of_expr.iter() {
160             let node = match expr_node(&body_source_map, expr, &db) {
161                 Some(value) => value,
162                 None => continue,
163             };
164             let range = node.as_ref().original_file_range(&db);
165             if let Some(expected) = types.remove(&range) {
166                 let actual = if display_source {
167                     ty.display_source_code(&db, def.module(&db)).unwrap()
168                 } else {
169                     ty.display_test(&db).to_string()
170                 };
171                 assert_eq!(actual, expected);
172             }
173             if let Some(expected) = adjustments.remove(&range) {
174                 if let Some(adjustments) = inference_result.expr_adjustments.get(&expr) {
175                     assert_eq!(
176                         expected,
177                         adjustments
178                             .iter()
179                             .map(|Adjustment { kind, .. }| format!("{:?}", kind))
180                             .collect::<Vec<_>>()
181                     );
182                 } else {
183                     panic!("expected {:?} adjustments, found none", expected);
184                 }
185             }
186         }
187
188         for (pat, mismatch) in inference_result.pat_type_mismatches() {
189             let node = match pat_node(&body_source_map, pat, &db) {
190                 Some(value) => value,
191                 None => continue,
192             };
193             let range = node.as_ref().original_file_range(&db);
194             let actual = format!(
195                 "expected {}, got {}",
196                 mismatch.expected.display_test(&db),
197                 mismatch.actual.display_test(&db)
198             );
199             match mismatches.remove(&range) {
200                 Some(annotation) => assert_eq!(actual, annotation),
201                 None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
202             }
203         }
204         for (expr, mismatch) in inference_result.expr_type_mismatches() {
205             let node = match body_source_map.expr_syntax(expr) {
206                 Ok(sp) => {
207                     let root = db.parse_or_expand(sp.file_id).unwrap();
208                     sp.map(|ptr| ptr.to_node(&root).syntax().clone())
209                 }
210                 Err(SyntheticSyntax) => continue,
211             };
212             let range = node.as_ref().original_file_range(&db);
213             let actual = format!(
214                 "expected {}, got {}",
215                 mismatch.expected.display_test(&db),
216                 mismatch.actual.display_test(&db)
217             );
218             match mismatches.remove(&range) {
219                 Some(annotation) => assert_eq!(actual, annotation),
220                 None => format_to!(unexpected_type_mismatches, "{:?}: {}\n", range.range, actual),
221             }
222         }
223     }
224
225     let mut buf = String::new();
226     if !unexpected_type_mismatches.is_empty() {
227         format_to!(buf, "Unexpected type mismatches:\n{}", unexpected_type_mismatches);
228     }
229     if !mismatches.is_empty() {
230         format_to!(buf, "Unchecked mismatch annotations:\n");
231         for m in mismatches {
232             format_to!(buf, "{:?}: {}\n", m.0.range, m.1);
233         }
234     }
235     if !types.is_empty() {
236         format_to!(buf, "Unchecked type annotations:\n");
237         for t in types {
238             format_to!(buf, "{:?}: type {}\n", t.0.range, t.1);
239         }
240     }
241     if !adjustments.is_empty() {
242         format_to!(buf, "Unchecked adjustments annotations:\n");
243         for t in adjustments {
244             format_to!(buf, "{:?}: type {:?}\n", t.0.range, t.1);
245         }
246     }
247     assert!(buf.is_empty(), "{}", buf);
248 }
249
250 fn expr_node(
251     body_source_map: &BodySourceMap,
252     expr: ExprId,
253     db: &TestDB,
254 ) -> Option<InFile<SyntaxNode>> {
255     Some(match body_source_map.expr_syntax(expr) {
256         Ok(sp) => {
257             let root = db.parse_or_expand(sp.file_id).unwrap();
258             sp.map(|ptr| ptr.to_node(&root).syntax().clone())
259         }
260         Err(SyntheticSyntax) => return None,
261     })
262 }
263
264 fn pat_node(
265     body_source_map: &BodySourceMap,
266     pat: PatId,
267     db: &TestDB,
268 ) -> Option<InFile<SyntaxNode>> {
269     Some(match body_source_map.pat_syntax(pat) {
270         Ok(sp) => {
271             let root = db.parse_or_expand(sp.file_id).unwrap();
272             sp.map(|ptr| {
273                 ptr.either(
274                     |it| it.to_node(&root).syntax().clone(),
275                     |it| it.to_node(&root).syntax().clone(),
276                 )
277             })
278         }
279         Err(SyntheticSyntax) => return None,
280     })
281 }
282
283 fn infer(ra_fixture: &str) -> String {
284     infer_with_mismatches(ra_fixture, false)
285 }
286
287 fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
288     let _tracing = setup_tracing();
289     let (db, file_id) = TestDB::with_single_file(content);
290
291     let mut buf = String::new();
292
293     let mut infer_def = |inference_result: Arc<InferenceResult>,
294                          body_source_map: Arc<BodySourceMap>| {
295         let mut types: Vec<(InFile<SyntaxNode>, &Ty)> = Vec::new();
296         let mut mismatches: Vec<(InFile<SyntaxNode>, &TypeMismatch)> = Vec::new();
297
298         for (pat, ty) in inference_result.type_of_pat.iter() {
299             let syntax_ptr = match body_source_map.pat_syntax(pat) {
300                 Ok(sp) => {
301                     let root = db.parse_or_expand(sp.file_id).unwrap();
302                     sp.map(|ptr| {
303                         ptr.either(
304                             |it| it.to_node(&root).syntax().clone(),
305                             |it| it.to_node(&root).syntax().clone(),
306                         )
307                     })
308                 }
309                 Err(SyntheticSyntax) => continue,
310             };
311             types.push((syntax_ptr.clone(), ty));
312             if let Some(mismatch) = inference_result.type_mismatch_for_pat(pat) {
313                 mismatches.push((syntax_ptr, mismatch));
314             }
315         }
316
317         for (expr, ty) in inference_result.type_of_expr.iter() {
318             let node = match body_source_map.expr_syntax(expr) {
319                 Ok(sp) => {
320                     let root = db.parse_or_expand(sp.file_id).unwrap();
321                     sp.map(|ptr| ptr.to_node(&root).syntax().clone())
322                 }
323                 Err(SyntheticSyntax) => continue,
324             };
325             types.push((node.clone(), ty));
326             if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
327                 mismatches.push((node, mismatch));
328             }
329         }
330
331         // sort ranges for consistency
332         types.sort_by_key(|(node, _)| {
333             let range = node.value.text_range();
334             (range.start(), range.end())
335         });
336         for (node, ty) in &types {
337             let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
338                 (self_param.name().unwrap().syntax().text_range(), "self".to_string())
339             } else {
340                 (node.value.text_range(), node.value.text().to_string().replace("\n", " "))
341             };
342             let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
343             format_to!(
344                 buf,
345                 "{}{:?} '{}': {}\n",
346                 macro_prefix,
347                 range,
348                 ellipsize(text, 15),
349                 ty.display_test(&db)
350             );
351         }
352         if include_mismatches {
353             mismatches.sort_by_key(|(node, _)| {
354                 let range = node.value.text_range();
355                 (range.start(), range.end())
356             });
357             for (src_ptr, mismatch) in &mismatches {
358                 let range = src_ptr.value.text_range();
359                 let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
360                 format_to!(
361                     buf,
362                     "{}{:?}: expected {}, got {}\n",
363                     macro_prefix,
364                     range,
365                     mismatch.expected.display_test(&db),
366                     mismatch.actual.display_test(&db),
367                 );
368             }
369         }
370     };
371
372     let module = db.module_for_file(file_id);
373     let def_map = module.def_map(&db);
374
375     let mut defs: Vec<DefWithBodyId> = Vec::new();
376     visit_module(&db, &def_map, module.local_id, &mut |it| defs.push(it));
377     defs.sort_by_key(|def| match def {
378         DefWithBodyId::FunctionId(it) => {
379             let loc = it.lookup(&db);
380             loc.source(&db).value.syntax().text_range().start()
381         }
382         DefWithBodyId::ConstId(it) => {
383             let loc = it.lookup(&db);
384             loc.source(&db).value.syntax().text_range().start()
385         }
386         DefWithBodyId::StaticId(it) => {
387             let loc = it.lookup(&db);
388             loc.source(&db).value.syntax().text_range().start()
389         }
390     });
391     for def in defs {
392         let (_body, source_map) = db.body_with_source_map(def);
393         let infer = db.infer(def);
394         infer_def(infer, source_map);
395     }
396
397     buf.truncate(buf.trim_end().len());
398     buf
399 }
400
401 fn visit_module(
402     db: &TestDB,
403     crate_def_map: &DefMap,
404     module_id: LocalModuleId,
405     cb: &mut dyn FnMut(DefWithBodyId),
406 ) {
407     visit_scope(db, crate_def_map, &crate_def_map[module_id].scope, cb);
408     for impl_id in crate_def_map[module_id].scope.impls() {
409         let impl_data = db.impl_data(impl_id);
410         for &item in impl_data.items.iter() {
411             match item {
412                 AssocItemId::FunctionId(it) => {
413                     let def = it.into();
414                     cb(def);
415                     let body = db.body(def);
416                     visit_body(db, &body, cb);
417                 }
418                 AssocItemId::ConstId(it) => {
419                     let def = it.into();
420                     cb(def);
421                     let body = db.body(def);
422                     visit_body(db, &body, cb);
423                 }
424                 AssocItemId::TypeAliasId(_) => (),
425             }
426         }
427     }
428
429     fn visit_scope(
430         db: &TestDB,
431         crate_def_map: &DefMap,
432         scope: &ItemScope,
433         cb: &mut dyn FnMut(DefWithBodyId),
434     ) {
435         for decl in scope.declarations() {
436             match decl {
437                 ModuleDefId::FunctionId(it) => {
438                     let def = it.into();
439                     cb(def);
440                     let body = db.body(def);
441                     visit_body(db, &body, cb);
442                 }
443                 ModuleDefId::ConstId(it) => {
444                     let def = it.into();
445                     cb(def);
446                     let body = db.body(def);
447                     visit_body(db, &body, cb);
448                 }
449                 ModuleDefId::StaticId(it) => {
450                     let def = it.into();
451                     cb(def);
452                     let body = db.body(def);
453                     visit_body(db, &body, cb);
454                 }
455                 ModuleDefId::TraitId(it) => {
456                     let trait_data = db.trait_data(it);
457                     for &(_, item) in trait_data.items.iter() {
458                         match item {
459                             AssocItemId::FunctionId(it) => cb(it.into()),
460                             AssocItemId::ConstId(it) => cb(it.into()),
461                             AssocItemId::TypeAliasId(_) => (),
462                         }
463                     }
464                 }
465                 ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
466                 _ => (),
467             }
468         }
469     }
470
471     fn visit_body(db: &TestDB, body: &Body, cb: &mut dyn FnMut(DefWithBodyId)) {
472         for (_, def_map) in body.blocks(db) {
473             for (mod_id, _) in def_map.modules() {
474                 visit_module(db, &def_map, mod_id, cb);
475             }
476         }
477     }
478 }
479
480 fn ellipsize(mut text: String, max_len: usize) -> String {
481     if text.len() <= max_len {
482         return text;
483     }
484     let ellipsis = "...";
485     let e_len = ellipsis.len();
486     let mut prefix_len = (max_len - e_len) / 2;
487     while !text.is_char_boundary(prefix_len) {
488         prefix_len += 1;
489     }
490     let mut suffix_len = max_len - e_len - prefix_len;
491     while !text.is_char_boundary(text.len() - suffix_len) {
492         suffix_len += 1;
493     }
494     text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
495     text
496 }
497
498 fn check_infer(ra_fixture: &str, expect: Expect) {
499     let mut actual = infer(ra_fixture);
500     actual.push('\n');
501     expect.assert_eq(&actual);
502 }
503
504 fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
505     let mut actual = infer_with_mismatches(ra_fixture, true);
506     actual.push('\n');
507     expect.assert_eq(&actual);
508 }
509
510 #[test]
511 fn salsa_bug() {
512     let (mut db, pos) = TestDB::with_position(
513         "
514         //- /lib.rs
515         trait Index {
516             type Output;
517         }
518
519         type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
520
521         pub trait UnificationStoreBase: Index<Output = Key<Self>> {
522             type Key;
523
524             fn len(&self) -> usize;
525         }
526
527         pub trait UnificationStoreMut: UnificationStoreBase {
528             fn push(&mut self, value: Self::Key);
529         }
530
531         fn main() {
532             let x = 1;
533             x.push(1);$0
534         }
535     ",
536     );
537
538     let module = db.module_for_file(pos.file_id);
539     let crate_def_map = module.def_map(&db);
540     visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
541         db.infer(def);
542     });
543
544     let new_text = "
545         //- /lib.rs
546         trait Index {
547             type Output;
548         }
549
550         type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
551
552         pub trait UnificationStoreBase: Index<Output = Key<Self>> {
553             type Key;
554
555             fn len(&self) -> usize;
556         }
557
558         pub trait UnificationStoreMut: UnificationStoreBase {
559             fn push(&mut self, value: Self::Key);
560         }
561
562         fn main() {
563
564             let x = 1;
565             x.push(1);
566         }
567     "
568     .to_string();
569
570     db.set_file_text(pos.file_id, Arc::new(new_text));
571
572     let module = db.module_for_file(pos.file_id);
573     let crate_def_map = module.def_map(&db);
574     visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
575         db.infer(def);
576     });
577 }