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