]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/tests.rs
Merge #8884
[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
11 use std::{env, sync::Arc};
12
13 use base_db::{fixture::WithFixture, FileRange, SourceDatabase, SourceDatabaseExt};
14 use expect_test::Expect;
15 use hir_def::{
16     body::{Body, BodySourceMap, SyntheticSyntax},
17     child_by_source::ChildBySource,
18     db::DefDatabase,
19     item_scope::ItemScope,
20     keys,
21     nameres::DefMap,
22     src::HasSource,
23     AssocItemId, DefWithBodyId, 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     algo,
30     ast::{self, AstNode, NameOwner},
31     SyntaxNode,
32 };
33 use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
34 use tracing_tree::HierarchicalLayer;
35
36 use crate::{
37     db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
38 };
39
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.
43
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()) {
47         return None;
48     }
49
50     let filter = EnvFilter::from_env("CHALK_DEBUG");
51     let layer = HierarchicalLayer::default()
52         .with_indent_lines(true)
53         .with_ansi(false)
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))
58 }
59
60 fn check_types(ra_fixture: &str) {
61     check_types_impl(ra_fixture, false)
62 }
63
64 fn check_types_source_code(ra_fixture: &str) {
65     check_types_impl(ra_fixture, true)
66 }
67
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()
78             } else {
79                 ty.display_test(&db).to_string()
80             };
81             assert_eq!(expected, actual);
82             checked_one = true;
83         }
84     }
85     assert!(checked_one, "no `//^` annotations found");
86 }
87
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))
95         .unwrap();
96
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();
101     }
102     panic!("Can't find expression")
103 }
104
105 fn infer(ra_fixture: &str) -> String {
106     infer_with_mismatches(ra_fixture, false)
107 }
108
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);
112
113     let mut buf = String::new();
114
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();
119
120         for (pat, ty) in inference_result.type_of_pat.iter() {
121             let syntax_ptr = match body_source_map.pat_syntax(pat) {
122                 Ok(sp) => {
123                     let root = db.parse_or_expand(sp.file_id).unwrap();
124                     sp.map(|ptr| {
125                         ptr.either(
126                             |it| it.to_node(&root).syntax().clone(),
127                             |it| it.to_node(&root).syntax().clone(),
128                         )
129                     })
130                 }
131                 Err(SyntheticSyntax) => continue,
132             };
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));
136             }
137         }
138
139         for (expr, ty) in inference_result.type_of_expr.iter() {
140             let node = match body_source_map.expr_syntax(expr) {
141                 Ok(sp) => {
142                     let root = db.parse_or_expand(sp.file_id).unwrap();
143                     sp.map(|ptr| ptr.to_node(&root).syntax().clone())
144                 }
145                 Err(SyntheticSyntax) => continue,
146             };
147             types.push((node.clone(), ty));
148             if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
149                 mismatches.push((node, mismatch));
150             }
151         }
152
153         // sort ranges for consistency
154         types.sort_by_key(|(node, _)| {
155             let range = node.value.text_range();
156             (range.start(), range.end())
157         });
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())
161             } else {
162                 (node.value.text_range(), node.value.text().to_string().replace("\n", " "))
163             };
164             let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
165             format_to!(
166                 buf,
167                 "{}{:?} '{}': {}\n",
168                 macro_prefix,
169                 range,
170                 ellipsize(text, 15),
171                 ty.display_test(&db)
172             );
173         }
174         if include_mismatches {
175             mismatches.sort_by_key(|(node, _)| {
176                 let range = node.value.text_range();
177                 (range.start(), range.end())
178             });
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 { "" };
182                 format_to!(
183                     buf,
184                     "{}{:?}: expected {}, got {}\n",
185                     macro_prefix,
186                     range,
187                     mismatch.expected.display_test(&db),
188                     mismatch.actual.display_test(&db),
189                 );
190             }
191         }
192     };
193
194     let module = db.module_for_file(file_id);
195     let def_map = module.def_map(&db);
196
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()
203         }
204         DefWithBodyId::ConstId(it) => {
205             let loc = it.lookup(&db);
206             loc.source(&db).value.syntax().text_range().start()
207         }
208         DefWithBodyId::StaticId(it) => {
209             let loc = it.lookup(&db);
210             loc.source(&db).value.syntax().text_range().start()
211         }
212     });
213     for def in defs {
214         let (_body, source_map) = db.body_with_source_map(def);
215         let infer = db.infer(def);
216         infer_def(infer, source_map);
217     }
218
219     buf.truncate(buf.trim_end().len());
220     buf
221 }
222
223 fn visit_module(
224     db: &TestDB,
225     crate_def_map: &DefMap,
226     module_id: LocalModuleId,
227     cb: &mut dyn FnMut(DefWithBodyId),
228 ) {
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() {
233             match item {
234                 AssocItemId::FunctionId(it) => {
235                     let def = it.into();
236                     cb(def);
237                     let body = db.body(def);
238                     visit_body(db, &body, cb);
239                 }
240                 AssocItemId::ConstId(it) => {
241                     let def = it.into();
242                     cb(def);
243                     let body = db.body(def);
244                     visit_body(db, &body, cb);
245                 }
246                 AssocItemId::TypeAliasId(_) => (),
247             }
248         }
249     }
250
251     fn visit_scope(
252         db: &TestDB,
253         crate_def_map: &DefMap,
254         scope: &ItemScope,
255         cb: &mut dyn FnMut(DefWithBodyId),
256     ) {
257         for decl in scope.declarations() {
258             match decl {
259                 ModuleDefId::FunctionId(it) => {
260                     let def = it.into();
261                     cb(def);
262                     let body = db.body(def);
263                     visit_body(db, &body, cb);
264                 }
265                 ModuleDefId::ConstId(it) => {
266                     let def = it.into();
267                     cb(def);
268                     let body = db.body(def);
269                     visit_body(db, &body, cb);
270                 }
271                 ModuleDefId::StaticId(it) => {
272                     let def = it.into();
273                     cb(def);
274                     let body = db.body(def);
275                     visit_body(db, &body, cb);
276                 }
277                 ModuleDefId::TraitId(it) => {
278                     let trait_data = db.trait_data(it);
279                     for &(_, item) in trait_data.items.iter() {
280                         match item {
281                             AssocItemId::FunctionId(it) => cb(it.into()),
282                             AssocItemId::ConstId(it) => cb(it.into()),
283                             AssocItemId::TypeAliasId(_) => (),
284                         }
285                     }
286                 }
287                 ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
288                 _ => (),
289             }
290         }
291     }
292
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);
297             }
298         }
299     }
300 }
301
302 fn ellipsize(mut text: String, max_len: usize) -> String {
303     if text.len() <= max_len {
304         return text;
305     }
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) {
310         prefix_len += 1;
311     }
312     let mut suffix_len = max_len - e_len - prefix_len;
313     while !text.is_char_boundary(text.len() - suffix_len) {
314         suffix_len += 1;
315     }
316     text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
317     text
318 }
319
320 #[test]
321 fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
322     let (mut db, pos) = TestDB::with_position(
323         "
324         //- /lib.rs
325         fn foo() -> i32 {
326             $01 + 1
327         }
328     ",
329     );
330     {
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| {
335                 db.infer(def);
336             });
337         });
338         assert!(format!("{:?}", events).contains("infer"))
339     }
340
341     let new_text = "
342         fn foo() -> i32 {
343             1
344             +
345             1
346         }
347     "
348     .to_string();
349
350     db.set_file_text(pos.file_id, Arc::new(new_text));
351
352     {
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| {
357                 db.infer(def);
358             });
359         });
360         assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
361     }
362 }
363
364 fn check_infer(ra_fixture: &str, expect: Expect) {
365     let mut actual = infer(ra_fixture);
366     actual.push('\n');
367     expect.assert_eq(&actual);
368 }
369
370 fn check_infer_with_mismatches(ra_fixture: &str, expect: Expect) {
371     let mut actual = infer_with_mismatches(ra_fixture, true);
372     actual.push('\n');
373     expect.assert_eq(&actual);
374 }
375
376 #[test]
377 fn salsa_bug() {
378     let (mut db, pos) = TestDB::with_position(
379         "
380         //- /lib.rs
381         trait Index {
382             type Output;
383         }
384
385         type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
386
387         pub trait UnificationStoreBase: Index<Output = Key<Self>> {
388             type Key;
389
390             fn len(&self) -> usize;
391         }
392
393         pub trait UnificationStoreMut: UnificationStoreBase {
394             fn push(&mut self, value: Self::Key);
395         }
396
397         fn main() {
398             let x = 1;
399             x.push(1);$0
400         }
401     ",
402     );
403
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| {
407         db.infer(def);
408     });
409
410     let new_text = "
411         //- /lib.rs
412         trait Index {
413             type Output;
414         }
415
416         type Key<S: UnificationStoreBase> = <S as UnificationStoreBase>::Key;
417
418         pub trait UnificationStoreBase: Index<Output = Key<Self>> {
419             type Key;
420
421             fn len(&self) -> usize;
422         }
423
424         pub trait UnificationStoreMut: UnificationStoreBase {
425             fn push(&mut self, value: Self::Key);
426         }
427
428         fn main() {
429
430             let x = 1;
431             x.push(1);
432         }
433     "
434     .to_string();
435
436     db.set_file_text(pos.file_id, Arc::new(new_text));
437
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| {
441         db.infer(def);
442     });
443 }