diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-06 14:19:06 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-06 14:19:06 +0000 |
commit | a25e8cff6755c1a0c46ee3690dcd75b641c0b3e4 (patch) | |
tree | 891cf4be4893c79598800928e7c0b669d9d2a9c2 /crates/ra_hir_ty/src/tests.rs | |
parent | d2b210a02e3e8ee1cf38909411fa8945aec99f4e (diff) | |
parent | f86fe3d891ab295e9e394a1338da86524a6205d3 (diff) |
Merge #2487
2487: Don't unify within a reference r=matklad a=flodiebold
If we are expecting a `&Foo` and get a `&something`, when checking the `something`, we are *expecting* a `Foo`, but we shouldn't try to unify whatever we get with that expectation, because it could actually be a `&Foo`, and `&&Foo` coerces to `&Foo`. So this fixes quite a few false type mismatches.
Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src/tests.rs')
-rw-r--r-- | crates/ra_hir_ty/src/tests.rs | 26 |
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 | ||
52 | fn infer(content: &str) -> String { | 52 | fn infer(content: &str) -> String { |
53 | infer_with_mismatches(content, false) | ||
54 | } | ||
55 | |||
56 | fn 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); |