aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/tests.rs')
-rw-r--r--crates/ra_hir_ty/src/tests.rs26
1 files changed, 26 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index 9f373a8f4..f1b67555f 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -50,6 +50,10 @@ fn type_at(content: &str) -> String {
50} 50}
51 51
52fn infer(content: &str) -> String { 52fn infer(content: &str) -> String {
53 infer_with_mismatches(content, false)
54}
55
56fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
53 let (db, file_id) = TestDB::with_single_file(content); 57 let (db, file_id) = TestDB::with_single_file(content);
54 58
55 let mut acc = String::new(); 59 let mut acc = String::new();
@@ -57,6 +61,7 @@ fn infer(content: &str) -> String {
57 let mut infer_def = |inference_result: Arc<InferenceResult>, 61 let mut infer_def = |inference_result: Arc<InferenceResult>,
58 body_source_map: Arc<BodySourceMap>| { 62 body_source_map: Arc<BodySourceMap>| {
59 let mut types = Vec::new(); 63 let mut types = Vec::new();
64 let mut mismatches = Vec::new();
60 65
61 for (pat, ty) in inference_result.type_of_pat.iter() { 66 for (pat, ty) in inference_result.type_of_pat.iter() {
62 let syntax_ptr = match body_source_map.pat_syntax(pat) { 67 let syntax_ptr = match body_source_map.pat_syntax(pat) {
@@ -76,6 +81,9 @@ fn infer(content: &str) -> String {
76 None => continue, 81 None => continue,
77 }; 82 };
78 types.push((syntax_ptr, ty)); 83 types.push((syntax_ptr, ty));
84 if let Some(mismatch) = inference_result.type_mismatch_for_expr(expr) {
85 mismatches.push((syntax_ptr, mismatch));
86 }
79 } 87 }
80 88
81 // sort ranges for consistency 89 // sort ranges for consistency
@@ -101,6 +109,24 @@ fn infer(content: &str) -> String {
101 ) 109 )
102 .unwrap(); 110 .unwrap();
103 } 111 }
112 if include_mismatches {
113 mismatches.sort_by_key(|(src_ptr, _)| {
114 (src_ptr.value.range().start(), src_ptr.value.range().end())
115 });
116 for (src_ptr, mismatch) in &mismatches {
117 let range = src_ptr.value.range();
118 let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
119 write!(
120 acc,
121 "{}{}: expected {}, got {}\n",
122 macro_prefix,
123 range,
124 mismatch.expected.display(&db),
125 mismatch.actual.display(&db),
126 )
127 .unwrap();
128 }
129 }
104 }; 130 };
105 131
106 let module = db.module_for_file(file_id); 132 let module = db.module_for_file(file_id);