aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2020-02-21 15:56:34 +0000
committerFlorian Diebold <[email protected]>2020-02-21 16:01:19 +0000
commitf1f45f9191d60c52dbedec717aee0de4a0580bcc (patch)
treebaebf708dcdc154fbe4fe903906e11e344a09802 /crates/ra_hir_ty
parente3037c2631ecb55996b676ce2c18b9df1858abaa (diff)
Fix handling of const patterns
E.g. in `match x { None => ... }`, `None` is a path pattern (resolving to the option variant), not a binding. To determine this, we need to try to resolve the name during lowering. This isn't too hard since we already need to resolve names for macro expansion anyway (though maybe a bit hacky). Fixes #1618.
Diffstat (limited to 'crates/ra_hir_ty')
-rw-r--r--crates/ra_hir_ty/src/infer/pat.rs4
-rw-r--r--crates/ra_hir_ty/src/tests/patterns.rs46
2 files changed, 48 insertions, 2 deletions
diff --git a/crates/ra_hir_ty/src/infer/pat.rs b/crates/ra_hir_ty/src/infer/pat.rs
index a495ecbfe..bf8ea192b 100644
--- a/crates/ra_hir_ty/src/infer/pat.rs
+++ b/crates/ra_hir_ty/src/infer/pat.rs
@@ -189,7 +189,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
189 }; 189 };
190 // use a new type variable if we got Ty::Unknown here 190 // use a new type variable if we got Ty::Unknown here
191 let ty = self.insert_type_vars_shallow(ty); 191 let ty = self.insert_type_vars_shallow(ty);
192 self.unify(&ty, expected); 192 if !self.unify(&ty, expected) {
193 // FIXME record mismatch, we need to change the type of self.type_mismatches for that
194 }
193 let ty = self.resolve_ty_as_possible(ty); 195 let ty = self.resolve_ty_as_possible(ty);
194 self.write_pat_ty(pat, ty.clone()); 196 self.write_pat_ty(pat, ty.clone());
195 ty 197 ty
diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs
index e25d6dbc4..81d00c2af 100644
--- a/crates/ra_hir_ty/src/tests/patterns.rs
+++ b/crates/ra_hir_ty/src/tests/patterns.rs
@@ -1,4 +1,4 @@
1use super::infer; 1use super::{infer, infer_with_mismatches};
2use insta::assert_snapshot; 2use insta::assert_snapshot;
3use test_utils::covers; 3use test_utils::covers;
4 4
@@ -236,3 +236,47 @@ fn test(a1: A<u32>, o: Option<u64>) {
236 "### 236 "###
237 ); 237 );
238} 238}
239
240#[test]
241fn infer_const_pattern() {
242 assert_snapshot!(
243 infer_with_mismatches(r#"
244enum Option<T> { None }
245use Option::None;
246struct Foo;
247const Bar: usize = 1;
248
249fn test() {
250 let a: Option<u32> = None;
251 let b: Option<i64> = match a {
252 None => None,
253 };
254 let _: () = match () { Foo => Foo }; // Expected mismatch
255 let _: () = match () { Bar => Bar }; // Expected mismatch
256}
257"#, true),
258 @r###"
259 [74; 75) '1': usize
260 [88; 310) '{ ...atch }': ()
261 [98; 99) 'a': Option<u32>
262 [115; 119) 'None': Option<u32>
263 [129; 130) 'b': Option<i64>
264 [146; 183) 'match ... }': Option<i64>
265 [152; 153) 'a': Option<u32>
266 [164; 168) 'None': Option<u32>
267 [172; 176) 'None': Option<i64>
268 [193; 194) '_': ()
269 [201; 224) 'match ... Foo }': Foo
270 [207; 209) '()': ()
271 [212; 215) 'Foo': Foo
272 [219; 222) 'Foo': Foo
273 [255; 256) '_': ()
274 [263; 286) 'match ... Bar }': usize
275 [269; 271) '()': ()
276 [274; 277) 'Bar': usize
277 [281; 284) 'Bar': usize
278 [201; 224): expected (), got Foo
279 [263; 286): expected (), got usize
280 "###
281 );
282}