diff options
author | Josh Mcguigan <[email protected]> | 2020-04-06 23:38:20 +0100 |
---|---|---|
committer | Josh Mcguigan <[email protected]> | 2020-04-07 13:12:08 +0100 |
commit | a208de15b7846911856e6c069f7df03676c18a03 (patch) | |
tree | 9622890aebe6e35ad2e813b44dc8d6fdeff0559b /crates/ra_hir_ty/src/_match.rs | |
parent | da6752d5f9a18ba58adb6a2e72d30a83532ec8a6 (diff) |
PR feedback implementation
Diffstat (limited to 'crates/ra_hir_ty/src/_match.rs')
-rw-r--r-- | crates/ra_hir_ty/src/_match.rs | 425 |
1 files changed, 331 insertions, 94 deletions
diff --git a/crates/ra_hir_ty/src/_match.rs b/crates/ra_hir_ty/src/_match.rs index 02472e0c0..f29a25505 100644 --- a/crates/ra_hir_ty/src/_match.rs +++ b/crates/ra_hir_ty/src/_match.rs | |||
@@ -2,7 +2,191 @@ | |||
2 | //! for match arms. | 2 | //! for match arms. |
3 | //! | 3 | //! |
4 | //! It is modeled on the rustc module `librustc_mir_build::hair::pattern::_match`, which | 4 | //! It is modeled on the rustc module `librustc_mir_build::hair::pattern::_match`, which |
5 | //! contains very detailed documentation about the algorithms used here. | 5 | //! contains very detailed documentation about the algorithms used here. I've duplicated |
6 | //! most of that documentation below. | ||
7 | //! | ||
8 | //! This file includes the logic for exhaustiveness and usefulness checking for | ||
9 | //! pattern-matching. Specifically, given a list of patterns for a type, we can | ||
10 | //! tell whether: | ||
11 | //! (a) the patterns cover every possible constructor for the type [exhaustiveness] | ||
12 | //! (b) each pattern is necessary [usefulness] | ||
13 | //! | ||
14 | //! The algorithm implemented here is a modified version of the one described in: | ||
15 | //! http://moscova.inria.fr/~maranget/papers/warn/index.html | ||
16 | //! However, to save future implementors from reading the original paper, we | ||
17 | //! summarise the algorithm here to hopefully save time and be a little clearer | ||
18 | //! (without being so rigorous). | ||
19 | //! | ||
20 | //! The core of the algorithm revolves about a "usefulness" check. In particular, we | ||
21 | //! are trying to compute a predicate `U(P, p)` where `P` is a list of patterns (we refer to this as | ||
22 | //! a matrix). `U(P, p)` represents whether, given an existing list of patterns | ||
23 | //! `P_1 ..= P_m`, adding a new pattern `p` will be "useful" (that is, cover previously- | ||
24 | //! uncovered values of the type). | ||
25 | //! | ||
26 | //! If we have this predicate, then we can easily compute both exhaustiveness of an | ||
27 | //! entire set of patterns and the individual usefulness of each one. | ||
28 | //! (a) the set of patterns is exhaustive iff `U(P, _)` is false (i.e., adding a wildcard | ||
29 | //! match doesn't increase the number of values we're matching) | ||
30 | //! (b) a pattern `P_i` is not useful if `U(P[0..=(i-1), P_i)` is false (i.e., adding a | ||
31 | //! pattern to those that have come before it doesn't increase the number of values | ||
32 | //! we're matching). | ||
33 | //! | ||
34 | //! During the course of the algorithm, the rows of the matrix won't just be individual patterns, | ||
35 | //! but rather partially-deconstructed patterns in the form of a list of patterns. The paper | ||
36 | //! calls those pattern-vectors, and we will call them pattern-stacks. The same holds for the | ||
37 | //! new pattern `p`. | ||
38 | //! | ||
39 | //! For example, say we have the following: | ||
40 | //! ``` | ||
41 | //! // x: (Option<bool>, Result<()>) | ||
42 | //! match x { | ||
43 | //! (Some(true), _) => {} | ||
44 | //! (None, Err(())) => {} | ||
45 | //! (None, Err(_)) => {} | ||
46 | //! } | ||
47 | //! ``` | ||
48 | //! Here, the matrix `P` starts as: | ||
49 | //! [ | ||
50 | //! [(Some(true), _)], | ||
51 | //! [(None, Err(()))], | ||
52 | //! [(None, Err(_))], | ||
53 | //! ] | ||
54 | //! We can tell it's not exhaustive, because `U(P, _)` is true (we're not covering | ||
55 | //! `[(Some(false), _)]`, for instance). In addition, row 3 is not useful, because | ||
56 | //! all the values it covers are already covered by row 2. | ||
57 | //! | ||
58 | //! A list of patterns can be thought of as a stack, because we are mainly interested in the top of | ||
59 | //! the stack at any given point, and we can pop or apply constructors to get new pattern-stacks. | ||
60 | //! To match the paper, the top of the stack is at the beginning / on the left. | ||
61 | //! | ||
62 | //! There are two important operations on pattern-stacks necessary to understand the algorithm: | ||
63 | //! 1. We can pop a given constructor off the top of a stack. This operation is called | ||
64 | //! `specialize`, and is denoted `S(c, p)` where `c` is a constructor (like `Some` or | ||
65 | //! `None`) and `p` a pattern-stack. | ||
66 | //! If the pattern on top of the stack can cover `c`, this removes the constructor and | ||
67 | //! pushes its arguments onto the stack. It also expands OR-patterns into distinct patterns. | ||
68 | //! Otherwise the pattern-stack is discarded. | ||
69 | //! This essentially filters those pattern-stacks whose top covers the constructor `c` and | ||
70 | //! discards the others. | ||
71 | //! | ||
72 | //! For example, the first pattern above initially gives a stack `[(Some(true), _)]`. If we | ||
73 | //! pop the tuple constructor, we are left with `[Some(true), _]`, and if we then pop the | ||
74 | //! `Some` constructor we get `[true, _]`. If we had popped `None` instead, we would get | ||
75 | //! nothing back. | ||
76 | //! | ||
77 | //! This returns zero or more new pattern-stacks, as follows. We look at the pattern `p_1` | ||
78 | //! on top of the stack, and we have four cases: | ||
79 | //! 1.1. `p_1 = c(r_1, .., r_a)`, i.e. the top of the stack has constructor `c`. We | ||
80 | //! push onto the stack the arguments of this constructor, and return the result: | ||
81 | //! r_1, .., r_a, p_2, .., p_n | ||
82 | //! 1.2. `p_1 = c'(r_1, .., r_a')` where `c ≠ c'`. We discard the current stack and | ||
83 | //! return nothing. | ||
84 | //! 1.3. `p_1 = _`. We push onto the stack as many wildcards as the constructor `c` has | ||
85 | //! arguments (its arity), and return the resulting stack: | ||
86 | //! _, .., _, p_2, .., p_n | ||
87 | //! 1.4. `p_1 = r_1 | r_2`. We expand the OR-pattern and then recurse on each resulting | ||
88 | //! stack: | ||
89 | //! S(c, (r_1, p_2, .., p_n)) | ||
90 | //! S(c, (r_2, p_2, .., p_n)) | ||
91 | //! | ||
92 | //! 2. We can pop a wildcard off the top of the stack. This is called `D(p)`, where `p` is | ||
93 | //! a pattern-stack. | ||
94 | //! This is used when we know there are missing constructor cases, but there might be | ||
95 | //! existing wildcard patterns, so to check the usefulness of the matrix, we have to check | ||
96 | //! all its *other* components. | ||
97 | //! | ||
98 | //! It is computed as follows. We look at the pattern `p_1` on top of the stack, | ||
99 | //! and we have three cases: | ||
100 | //! 1.1. `p_1 = c(r_1, .., r_a)`. We discard the current stack and return nothing. | ||
101 | //! 1.2. `p_1 = _`. We return the rest of the stack: | ||
102 | //! p_2, .., p_n | ||
103 | //! 1.3. `p_1 = r_1 | r_2`. We expand the OR-pattern and then recurse on each resulting | ||
104 | //! stack. | ||
105 | //! D((r_1, p_2, .., p_n)) | ||
106 | //! D((r_2, p_2, .., p_n)) | ||
107 | //! | ||
108 | //! Note that the OR-patterns are not always used directly in Rust, but are used to derive the | ||
109 | //! exhaustive integer matching rules, so they're written here for posterity. | ||
110 | //! | ||
111 | //! Both those operations extend straightforwardly to a list or pattern-stacks, i.e. a matrix, by | ||
112 | //! working row-by-row. Popping a constructor ends up keeping only the matrix rows that start with | ||
113 | //! the given constructor, and popping a wildcard keeps those rows that start with a wildcard. | ||
114 | //! | ||
115 | //! | ||
116 | //! The algorithm for computing `U` | ||
117 | //! ------------------------------- | ||
118 | //! The algorithm is inductive (on the number of columns: i.e., components of tuple patterns). | ||
119 | //! That means we're going to check the components from left-to-right, so the algorithm | ||
120 | //! operates principally on the first component of the matrix and new pattern-stack `p`. | ||
121 | //! This algorithm is realised in the `is_useful` function. | ||
122 | //! | ||
123 | //! Base case. (`n = 0`, i.e., an empty tuple pattern) | ||
124 | //! - If `P` already contains an empty pattern (i.e., if the number of patterns `m > 0`), | ||
125 | //! then `U(P, p)` is false. | ||
126 | //! - Otherwise, `P` must be empty, so `U(P, p)` is true. | ||
127 | //! | ||
128 | //! Inductive step. (`n > 0`, i.e., whether there's at least one column | ||
129 | //! [which may then be expanded into further columns later]) | ||
130 | //! We're going to match on the top of the new pattern-stack, `p_1`. | ||
131 | //! - If `p_1 == c(r_1, .., r_a)`, i.e. we have a constructor pattern. | ||
132 | //! Then, the usefulness of `p_1` can be reduced to whether it is useful when | ||
133 | //! we ignore all the patterns in the first column of `P` that involve other constructors. | ||
134 | //! This is where `S(c, P)` comes in: | ||
135 | //! `U(P, p) := U(S(c, P), S(c, p))` | ||
136 | //! This special case is handled in `is_useful_specialized`. | ||
137 | //! | ||
138 | //! For example, if `P` is: | ||
139 | //! [ | ||
140 | //! [Some(true), _], | ||
141 | //! [None, 0], | ||
142 | //! ] | ||
143 | //! and `p` is [Some(false), 0], then we don't care about row 2 since we know `p` only | ||
144 | //! matches values that row 2 doesn't. For row 1 however, we need to dig into the | ||
145 | //! arguments of `Some` to know whether some new value is covered. So we compute | ||
146 | //! `U([[true, _]], [false, 0])`. | ||
147 | //! | ||
148 | //! - If `p_1 == _`, then we look at the list of constructors that appear in the first | ||
149 | //! component of the rows of `P`: | ||
150 | //! + If there are some constructors that aren't present, then we might think that the | ||
151 | //! wildcard `_` is useful, since it covers those constructors that weren't covered | ||
152 | //! before. | ||
153 | //! That's almost correct, but only works if there were no wildcards in those first | ||
154 | //! components. So we need to check that `p` is useful with respect to the rows that | ||
155 | //! start with a wildcard, if there are any. This is where `D` comes in: | ||
156 | //! `U(P, p) := U(D(P), D(p))` | ||
157 | //! | ||
158 | //! For example, if `P` is: | ||
159 | //! [ | ||
160 | //! [_, true, _], | ||
161 | //! [None, false, 1], | ||
162 | //! ] | ||
163 | //! and `p` is [_, false, _], the `Some` constructor doesn't appear in `P`. So if we | ||
164 | //! only had row 2, we'd know that `p` is useful. However row 1 starts with a | ||
165 | //! wildcard, so we need to check whether `U([[true, _]], [false, 1])`. | ||
166 | //! | ||
167 | //! + Otherwise, all possible constructors (for the relevant type) are present. In this | ||
168 | //! case we must check whether the wildcard pattern covers any unmatched value. For | ||
169 | //! that, we can think of the `_` pattern as a big OR-pattern that covers all | ||
170 | //! possible constructors. For `Option`, that would mean `_ = None | Some(_)` for | ||
171 | //! example. The wildcard pattern is useful in this case if it is useful when | ||
172 | //! specialized to one of the possible constructors. So we compute: | ||
173 | //! `U(P, p) := ∃(k ϵ constructors) U(S(k, P), S(k, p))` | ||
174 | //! | ||
175 | //! For example, if `P` is: | ||
176 | //! [ | ||
177 | //! [Some(true), _], | ||
178 | //! [None, false], | ||
179 | //! ] | ||
180 | //! and `p` is [_, false], both `None` and `Some` constructors appear in the first | ||
181 | //! components of `P`. We will therefore try popping both constructors in turn: we | ||
182 | //! compute U([[true, _]], [_, false]) for the `Some` constructor, and U([[false]], | ||
183 | //! [false]) for the `None` constructor. The first case returns true, so we know that | ||
184 | //! `p` is useful for `P`. Indeed, it matches `[Some(false), _]` that wasn't matched | ||
185 | //! before. | ||
186 | //! | ||
187 | //! - If `p_1 == r_1 | r_2`, then the usefulness depends on each `r_i` separately: | ||
188 | //! `U(P, p) := U(P, (r_1, p_2, .., p_n)) | ||
189 | //! || U(P, (r_2, p_2, .., p_n))` | ||
6 | use std::sync::Arc; | 190 | use std::sync::Arc; |
7 | 191 | ||
8 | use smallvec::{smallvec, SmallVec}; | 192 | use smallvec::{smallvec, SmallVec}; |
@@ -134,16 +318,26 @@ impl PatStack { | |||
134 | ) -> MatchCheckResult<Option<PatStack>> { | 318 | ) -> MatchCheckResult<Option<PatStack>> { |
135 | let result = match (self.head().as_pat(cx), constructor) { | 319 | let result = match (self.head().as_pat(cx), constructor) { |
136 | (Pat::Tuple(ref pat_ids), Constructor::Tuple { arity }) => { | 320 | (Pat::Tuple(ref pat_ids), Constructor::Tuple { arity }) => { |
137 | if pat_ids.len() != *arity { | 321 | debug_assert_eq!( |
138 | None | 322 | pat_ids.len(), |
139 | } else { | 323 | *arity, |
140 | Some(self.replace_head_with(pat_ids)) | 324 | "we type check before calling this code, so we should never hit this case", |
325 | ); | ||
326 | |||
327 | Some(self.replace_head_with(pat_ids)) | ||
328 | } | ||
329 | (Pat::Lit(lit_expr), Constructor::Bool(constructor_val)) => { | ||
330 | match cx.body.exprs[lit_expr] { | ||
331 | Expr::Literal(Literal::Bool(pat_val)) if *constructor_val == pat_val => { | ||
332 | Some(self.to_tail()) | ||
333 | } | ||
334 | // it was a bool but the value doesn't match | ||
335 | Expr::Literal(Literal::Bool(_)) => None, | ||
336 | // perhaps this is actually unreachable given we have | ||
337 | // already checked that these match arms have the appropriate type? | ||
338 | _ => return Err(MatchCheckNotImplemented), | ||
141 | } | 339 | } |
142 | } | 340 | } |
143 | (Pat::Lit(_), Constructor::Bool(_)) => { | ||
144 | // for now we only support bool literals | ||
145 | Some(self.to_tail()) | ||
146 | } | ||
147 | (Pat::Wild, constructor) => Some(self.expand_wildcard(cx, constructor)?), | 341 | (Pat::Wild, constructor) => Some(self.expand_wildcard(cx, constructor)?), |
148 | (Pat::Path(_), Constructor::Enum(constructor)) => { | 342 | (Pat::Path(_), Constructor::Enum(constructor)) => { |
149 | // enums with no associated data become `Pat::Path` | 343 | // enums with no associated data become `Pat::Path` |
@@ -162,7 +356,7 @@ impl PatStack { | |||
162 | Some(self.replace_head_with(pat_ids)) | 356 | Some(self.replace_head_with(pat_ids)) |
163 | } | 357 | } |
164 | } | 358 | } |
165 | (Pat::Or(_), _) => unreachable!("we desugar or patterns so this should never happen"), | 359 | (Pat::Or(_), _) => return Err(MatchCheckNotImplemented), |
166 | (_, _) => return Err(MatchCheckNotImplemented), | 360 | (_, _) => return Err(MatchCheckNotImplemented), |
167 | }; | 361 | }; |
168 | 362 | ||
@@ -186,19 +380,8 @@ impl PatStack { | |||
186 | ); | 380 | ); |
187 | 381 | ||
188 | let mut patterns: PatStackInner = smallvec![]; | 382 | let mut patterns: PatStackInner = smallvec![]; |
189 | let arity = match constructor { | ||
190 | Constructor::Bool(_) => 0, | ||
191 | Constructor::Tuple { arity } => *arity, | ||
192 | Constructor::Enum(e) => { | ||
193 | match cx.db.enum_data(e.parent).variants[e.local_id].variant_data.as_ref() { | ||
194 | VariantData::Tuple(struct_field_data) => struct_field_data.len(), | ||
195 | VariantData::Unit => 0, | ||
196 | _ => return Err(MatchCheckNotImplemented), | ||
197 | } | ||
198 | } | ||
199 | }; | ||
200 | 383 | ||
201 | for _ in 0..arity { | 384 | for _ in 0..constructor.arity(cx)? { |
202 | patterns.push(PatIdOrWild::Wild); | 385 | patterns.push(PatIdOrWild::Wild); |
203 | } | 386 | } |
204 | 387 | ||
@@ -368,46 +551,23 @@ pub(crate) fn is_useful( | |||
368 | // constructors are covered (`Some`/`None`), so we need | 551 | // constructors are covered (`Some`/`None`), so we need |
369 | // to perform specialization to see that our wildcard will cover | 552 | // to perform specialization to see that our wildcard will cover |
370 | // the `Some(false)` case. | 553 | // the `Some(false)` case. |
371 | let mut constructor = None; | 554 | // |
372 | for pat in matrix.heads() { | 555 | // Here we create a constructor for each variant and then check |
373 | if let Some(c) = pat_constructor(cx, pat)? { | 556 | // usefulness after specializing for that constructor. |
374 | constructor = Some(c); | 557 | let mut found_unimplemented = false; |
375 | break; | 558 | for constructor in constructor.all_constructors(cx) { |
376 | } | 559 | let matrix = matrix.specialize_constructor(&cx, &constructor)?; |
560 | let v = v.expand_wildcard(&cx, &constructor)?; | ||
561 | |||
562 | match is_useful(&cx, &matrix, &v) { | ||
563 | Ok(Usefulness::Useful) => return Ok(Usefulness::Useful), | ||
564 | Ok(Usefulness::NotUseful) => continue, | ||
565 | _ => found_unimplemented = true, | ||
566 | }; | ||
377 | } | 567 | } |
378 | 568 | ||
379 | if let Some(constructor) = constructor { | 569 | if found_unimplemented { |
380 | if let Constructor::Enum(e) = constructor { | 570 | Err(MatchCheckNotImplemented) |
381 | // For enums we handle each variant as a distinct constructor, so | ||
382 | // here we create a constructor for each variant and then check | ||
383 | // usefulness after specializing for that constructor. | ||
384 | let mut found_unimplemented = false; | ||
385 | for constructor in | ||
386 | cx.db.enum_data(e.parent).variants.iter().map(|(local_id, _)| { | ||
387 | Constructor::Enum(EnumVariantId { parent: e.parent, local_id }) | ||
388 | }) | ||
389 | { | ||
390 | let matrix = matrix.specialize_constructor(&cx, &constructor)?; | ||
391 | let v = v.expand_wildcard(&cx, &constructor)?; | ||
392 | |||
393 | match is_useful(&cx, &matrix, &v) { | ||
394 | Ok(Usefulness::Useful) => return Ok(Usefulness::Useful), | ||
395 | Ok(Usefulness::NotUseful) => continue, | ||
396 | _ => found_unimplemented = true, | ||
397 | }; | ||
398 | } | ||
399 | |||
400 | if found_unimplemented { | ||
401 | Err(MatchCheckNotImplemented) | ||
402 | } else { | ||
403 | Ok(Usefulness::NotUseful) | ||
404 | } | ||
405 | } else { | ||
406 | let matrix = matrix.specialize_constructor(&cx, &constructor)?; | ||
407 | let v = v.expand_wildcard(&cx, &constructor)?; | ||
408 | |||
409 | is_useful(&cx, &matrix, &v) | ||
410 | } | ||
411 | } else { | 571 | } else { |
412 | Ok(Usefulness::NotUseful) | 572 | Ok(Usefulness::NotUseful) |
413 | } | 573 | } |
@@ -425,7 +585,7 @@ pub(crate) fn is_useful( | |||
425 | } | 585 | } |
426 | } | 586 | } |
427 | 587 | ||
428 | #[derive(Debug)] | 588 | #[derive(Debug, Clone, Copy)] |
429 | /// Similar to TypeCtor, but includes additional information about the specific | 589 | /// Similar to TypeCtor, but includes additional information about the specific |
430 | /// value being instantiated. For example, TypeCtor::Bool doesn't contain the | 590 | /// value being instantiated. For example, TypeCtor::Bool doesn't contain the |
431 | /// boolean value. | 591 | /// boolean value. |
@@ -435,6 +595,40 @@ enum Constructor { | |||
435 | Enum(EnumVariantId), | 595 | Enum(EnumVariantId), |
436 | } | 596 | } |
437 | 597 | ||
598 | impl Constructor { | ||
599 | fn arity(&self, cx: &MatchCheckCtx) -> MatchCheckResult<usize> { | ||
600 | let arity = match self { | ||
601 | Constructor::Bool(_) => 0, | ||
602 | Constructor::Tuple { arity } => *arity, | ||
603 | Constructor::Enum(e) => { | ||
604 | match cx.db.enum_data(e.parent).variants[e.local_id].variant_data.as_ref() { | ||
605 | VariantData::Tuple(struct_field_data) => struct_field_data.len(), | ||
606 | VariantData::Unit => 0, | ||
607 | _ => return Err(MatchCheckNotImplemented), | ||
608 | } | ||
609 | } | ||
610 | }; | ||
611 | |||
612 | Ok(arity) | ||
613 | } | ||
614 | |||
615 | fn all_constructors(&self, cx: &MatchCheckCtx) -> Vec<Constructor> { | ||
616 | match self { | ||
617 | Constructor::Bool(_) => vec![Constructor::Bool(true), Constructor::Bool(false)], | ||
618 | Constructor::Tuple { .. } => vec![*self], | ||
619 | Constructor::Enum(e) => cx | ||
620 | .db | ||
621 | .enum_data(e.parent) | ||
622 | .variants | ||
623 | .iter() | ||
624 | .map(|(local_id, _)| { | ||
625 | Constructor::Enum(EnumVariantId { parent: e.parent, local_id }) | ||
626 | }) | ||
627 | .collect(), | ||
628 | } | ||
629 | } | ||
630 | } | ||
631 | |||
438 | /// Returns the constructor for the given pattern. Should only return None | 632 | /// Returns the constructor for the given pattern. Should only return None |
439 | /// in the case of a Wild pattern. | 633 | /// in the case of a Wild pattern. |
440 | fn pat_constructor(cx: &MatchCheckCtx, pat: PatIdOrWild) -> MatchCheckResult<Option<Constructor>> { | 634 | fn pat_constructor(cx: &MatchCheckCtx, pat: PatIdOrWild) -> MatchCheckResult<Option<Constructor>> { |
@@ -501,14 +695,7 @@ fn all_constructors_covered( | |||
501 | } | 695 | } |
502 | 696 | ||
503 | fn enum_variant_matches(cx: &MatchCheckCtx, pat_id: PatId, enum_variant_id: EnumVariantId) -> bool { | 697 | fn enum_variant_matches(cx: &MatchCheckCtx, pat_id: PatId, enum_variant_id: EnumVariantId) -> bool { |
504 | if let Some(VariantId::EnumVariantId(pat_variant_id)) = | 698 | Some(enum_variant_id.into()) == cx.infer.variant_resolution_for_pat(pat_id) |
505 | cx.infer.variant_resolution_for_pat(pat_id) | ||
506 | { | ||
507 | if pat_variant_id.local_id == enum_variant_id.local_id { | ||
508 | return true; | ||
509 | } | ||
510 | } | ||
511 | false | ||
512 | } | 699 | } |
513 | 700 | ||
514 | #[cfg(test)] | 701 | #[cfg(test)] |
@@ -522,10 +709,10 @@ mod tests { | |||
522 | TestDB::with_single_file(content).0.diagnostics().0 | 709 | TestDB::with_single_file(content).0.diagnostics().0 |
523 | } | 710 | } |
524 | 711 | ||
525 | pub(super) fn check_diagnostic_with_no_fix(content: &str) { | 712 | pub(super) fn check_diagnostic(content: &str) { |
526 | let diagnostic_count = TestDB::with_single_file(content).0.diagnostics().1; | 713 | let diagnostic_count = TestDB::with_single_file(content).0.diagnostics().1; |
527 | 714 | ||
528 | assert_eq!(1, diagnostic_count, "no diagnotic reported"); | 715 | assert_eq!(1, diagnostic_count, "no diagnostic reported"); |
529 | } | 716 | } |
530 | 717 | ||
531 | pub(super) fn check_no_diagnostic(content: &str) { | 718 | pub(super) fn check_no_diagnostic(content: &str) { |
@@ -558,7 +745,7 @@ mod tests { | |||
558 | } | 745 | } |
559 | "; | 746 | "; |
560 | 747 | ||
561 | check_diagnostic_with_no_fix(content); | 748 | check_diagnostic(content); |
562 | } | 749 | } |
563 | 750 | ||
564 | #[test] | 751 | #[test] |
@@ -596,7 +783,7 @@ mod tests { | |||
596 | } | 783 | } |
597 | "; | 784 | "; |
598 | 785 | ||
599 | check_diagnostic_with_no_fix(content); | 786 | check_diagnostic(content); |
600 | } | 787 | } |
601 | 788 | ||
602 | #[test] | 789 | #[test] |
@@ -621,7 +808,7 @@ mod tests { | |||
621 | } | 808 | } |
622 | "; | 809 | "; |
623 | 810 | ||
624 | check_diagnostic_with_no_fix(content); | 811 | check_diagnostic(content); |
625 | } | 812 | } |
626 | 813 | ||
627 | #[test] | 814 | #[test] |
@@ -646,7 +833,7 @@ mod tests { | |||
646 | } | 833 | } |
647 | "; | 834 | "; |
648 | 835 | ||
649 | check_diagnostic_with_no_fix(content); | 836 | check_diagnostic(content); |
650 | } | 837 | } |
651 | 838 | ||
652 | #[test] | 839 | #[test] |
@@ -659,7 +846,7 @@ mod tests { | |||
659 | } | 846 | } |
660 | "; | 847 | "; |
661 | 848 | ||
662 | check_diagnostic_with_no_fix(content); | 849 | check_diagnostic(content); |
663 | } | 850 | } |
664 | 851 | ||
665 | #[test] | 852 | #[test] |
@@ -685,7 +872,7 @@ mod tests { | |||
685 | } | 872 | } |
686 | "; | 873 | "; |
687 | 874 | ||
688 | check_diagnostic_with_no_fix(content); | 875 | check_diagnostic(content); |
689 | } | 876 | } |
690 | 877 | ||
691 | #[test] | 878 | #[test] |
@@ -698,7 +885,37 @@ mod tests { | |||
698 | } | 885 | } |
699 | "; | 886 | "; |
700 | 887 | ||
701 | check_diagnostic_with_no_fix(content); | 888 | check_diagnostic(content); |
889 | } | ||
890 | |||
891 | #[test] | ||
892 | fn tuple_of_bools_missing_arm() { | ||
893 | let content = r" | ||
894 | fn test_fn() { | ||
895 | match (false, true) { | ||
896 | (false, true) => {}, | ||
897 | (false, false) => {}, | ||
898 | (true, false) => {}, | ||
899 | } | ||
900 | } | ||
901 | "; | ||
902 | |||
903 | check_diagnostic(content); | ||
904 | } | ||
905 | |||
906 | #[test] | ||
907 | fn tuple_of_bools_with_wilds() { | ||
908 | let content = r" | ||
909 | fn test_fn() { | ||
910 | match (false, true) { | ||
911 | (false, _) => {}, | ||
912 | (true, false) => {}, | ||
913 | (_, true) => {}, | ||
914 | } | ||
915 | } | ||
916 | "; | ||
917 | |||
918 | check_no_diagnostic(content); | ||
702 | } | 919 | } |
703 | 920 | ||
704 | #[test] | 921 | #[test] |
@@ -727,7 +944,7 @@ mod tests { | |||
727 | } | 944 | } |
728 | "; | 945 | "; |
729 | 946 | ||
730 | check_diagnostic_with_no_fix(content); | 947 | check_diagnostic(content); |
731 | } | 948 | } |
732 | 949 | ||
733 | #[test] | 950 | #[test] |
@@ -754,7 +971,7 @@ mod tests { | |||
754 | } | 971 | } |
755 | "; | 972 | "; |
756 | 973 | ||
757 | check_diagnostic_with_no_fix(content); | 974 | check_diagnostic(content); |
758 | } | 975 | } |
759 | 976 | ||
760 | #[test] | 977 | #[test] |
@@ -767,7 +984,7 @@ mod tests { | |||
767 | } | 984 | } |
768 | "; | 985 | "; |
769 | 986 | ||
770 | check_diagnostic_with_no_fix(content); | 987 | check_diagnostic(content); |
771 | } | 988 | } |
772 | 989 | ||
773 | #[test] | 990 | #[test] |
@@ -796,7 +1013,7 @@ mod tests { | |||
796 | } | 1013 | } |
797 | "; | 1014 | "; |
798 | 1015 | ||
799 | check_diagnostic_with_no_fix(content); | 1016 | check_diagnostic(content); |
800 | } | 1017 | } |
801 | 1018 | ||
802 | #[test] | 1019 | #[test] |
@@ -827,7 +1044,7 @@ mod tests { | |||
827 | } | 1044 | } |
828 | "; | 1045 | "; |
829 | 1046 | ||
830 | check_diagnostic_with_no_fix(content); | 1047 | check_diagnostic(content); |
831 | } | 1048 | } |
832 | 1049 | ||
833 | #[test] | 1050 | #[test] |
@@ -844,7 +1061,7 @@ mod tests { | |||
844 | } | 1061 | } |
845 | "; | 1062 | "; |
846 | 1063 | ||
847 | check_diagnostic_with_no_fix(content); | 1064 | check_diagnostic(content); |
848 | } | 1065 | } |
849 | 1066 | ||
850 | #[test] | 1067 | #[test] |
@@ -879,7 +1096,7 @@ mod tests { | |||
879 | } | 1096 | } |
880 | "; | 1097 | "; |
881 | 1098 | ||
882 | check_diagnostic_with_no_fix(content); | 1099 | check_diagnostic(content); |
883 | } | 1100 | } |
884 | 1101 | ||
885 | #[test] | 1102 | #[test] |
@@ -913,7 +1130,7 @@ mod tests { | |||
913 | } | 1130 | } |
914 | "; | 1131 | "; |
915 | 1132 | ||
916 | check_diagnostic_with_no_fix(content); | 1133 | check_diagnostic(content); |
917 | } | 1134 | } |
918 | 1135 | ||
919 | #[test] | 1136 | #[test] |
@@ -931,7 +1148,7 @@ mod tests { | |||
931 | } | 1148 | } |
932 | "; | 1149 | "; |
933 | 1150 | ||
934 | check_diagnostic_with_no_fix(content); | 1151 | check_diagnostic(content); |
935 | } | 1152 | } |
936 | 1153 | ||
937 | #[test] | 1154 | #[test] |
@@ -1004,7 +1221,7 @@ mod tests { | |||
1004 | } | 1221 | } |
1005 | "; | 1222 | "; |
1006 | 1223 | ||
1007 | check_diagnostic_with_no_fix(content); | 1224 | check_diagnostic(content); |
1008 | } | 1225 | } |
1009 | 1226 | ||
1010 | #[test] | 1227 | #[test] |
@@ -1089,7 +1306,7 @@ mod tests { | |||
1089 | "; | 1306 | "; |
1090 | 1307 | ||
1091 | // Match arms with the incorrect type are filtered out. | 1308 | // Match arms with the incorrect type are filtered out. |
1092 | check_diagnostic_with_no_fix(content); | 1309 | check_diagnostic(content); |
1093 | } | 1310 | } |
1094 | 1311 | ||
1095 | #[test] | 1312 | #[test] |
@@ -1104,7 +1321,23 @@ mod tests { | |||
1104 | "; | 1321 | "; |
1105 | 1322 | ||
1106 | // Match arms with the incorrect type are filtered out. | 1323 | // Match arms with the incorrect type are filtered out. |
1107 | check_diagnostic_with_no_fix(content); | 1324 | check_diagnostic(content); |
1325 | } | ||
1326 | |||
1327 | #[test] | ||
1328 | fn enum_not_in_scope() { | ||
1329 | let content = r" | ||
1330 | fn test_fn() { | ||
1331 | match Foo::Bar { | ||
1332 | Foo::Baz => (), | ||
1333 | } | ||
1334 | } | ||
1335 | "; | ||
1336 | |||
1337 | // The enum is not in scope so we don't perform exhaustiveness | ||
1338 | // checking, but we want to be sure we don't panic here (and | ||
1339 | // we don't create a diagnostic). | ||
1340 | check_no_diagnostic(content); | ||
1108 | } | 1341 | } |
1109 | } | 1342 | } |
1110 | 1343 | ||
@@ -1158,17 +1391,21 @@ mod false_negatives { | |||
1158 | } | 1391 | } |
1159 | 1392 | ||
1160 | #[test] | 1393 | #[test] |
1161 | fn enum_not_in_scope() { | 1394 | fn internal_or() { |
1162 | let content = r" | 1395 | let content = r" |
1163 | fn test_fn() { | 1396 | fn test_fn() { |
1164 | match Foo::Bar { | 1397 | enum Either { |
1165 | Foo::Baz => (), | 1398 | A(bool), |
1399 | B, | ||
1400 | } | ||
1401 | match Either::B { | ||
1402 | Either::A(true | false) => (), | ||
1166 | } | 1403 | } |
1167 | } | 1404 | } |
1168 | "; | 1405 | "; |
1169 | 1406 | ||
1170 | // This is a false negative. | 1407 | // This is a false negative. |
1171 | // The enum is not in scope so we don't perform exhaustiveness checking. | 1408 | // We do not currently handle patterns with internal `or`s. |
1172 | check_no_diagnostic(content); | 1409 | check_no_diagnostic(content); |
1173 | } | 1410 | } |
1174 | } | 1411 | } |