diff options
Diffstat (limited to 'crates/ra_hir_ty/src/match_checking.rs')
-rw-r--r-- | crates/ra_hir_ty/src/match_checking.rs | 2157 |
1 files changed, 2157 insertions, 0 deletions
diff --git a/crates/ra_hir_ty/src/match_checking.rs b/crates/ra_hir_ty/src/match_checking.rs new file mode 100644 index 000000000..5495ce284 --- /dev/null +++ b/crates/ra_hir_ty/src/match_checking.rs | |||
@@ -0,0 +1,2157 @@ | |||
1 | //! This module implements match statement exhaustiveness checking and usefulness checking | ||
2 | //! for match arms. | ||
3 | //! | ||
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. 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 | //! ```ignore | ||
42 | //! // x: (Option<bool>, Result<()>) | ||
43 | //! match x { | ||
44 | //! (Some(true), _) => {} | ||
45 | //! (None, Err(())) => {} | ||
46 | //! (None, Err(_)) => {} | ||
47 | //! } | ||
48 | //! ``` | ||
49 | //! | ||
50 | //! Here, the matrix `P` starts as: | ||
51 | //! | ||
52 | //! ```text | ||
53 | //! [ | ||
54 | //! [(Some(true), _)], | ||
55 | //! [(None, Err(()))], | ||
56 | //! [(None, Err(_))], | ||
57 | //! ] | ||
58 | //! ``` | ||
59 | //! | ||
60 | //! We can tell it's not exhaustive, because `U(P, _)` is true (we're not covering | ||
61 | //! `[(Some(false), _)]`, for instance). In addition, row 3 is not useful, because | ||
62 | //! all the values it covers are already covered by row 2. | ||
63 | //! | ||
64 | //! A list of patterns can be thought of as a stack, because we are mainly interested in the top of | ||
65 | //! the stack at any given point, and we can pop or apply constructors to get new pattern-stacks. | ||
66 | //! To match the paper, the top of the stack is at the beginning / on the left. | ||
67 | //! | ||
68 | //! There are two important operations on pattern-stacks necessary to understand the algorithm: | ||
69 | //! | ||
70 | //! 1. We can pop a given constructor off the top of a stack. This operation is called | ||
71 | //! `specialize`, and is denoted `S(c, p)` where `c` is a constructor (like `Some` or | ||
72 | //! `None`) and `p` a pattern-stack. | ||
73 | //! If the pattern on top of the stack can cover `c`, this removes the constructor and | ||
74 | //! pushes its arguments onto the stack. It also expands OR-patterns into distinct patterns. | ||
75 | //! Otherwise the pattern-stack is discarded. | ||
76 | //! This essentially filters those pattern-stacks whose top covers the constructor `c` and | ||
77 | //! discards the others. | ||
78 | //! | ||
79 | //! For example, the first pattern above initially gives a stack `[(Some(true), _)]`. If we | ||
80 | //! pop the tuple constructor, we are left with `[Some(true), _]`, and if we then pop the | ||
81 | //! `Some` constructor we get `[true, _]`. If we had popped `None` instead, we would get | ||
82 | //! nothing back. | ||
83 | //! | ||
84 | //! This returns zero or more new pattern-stacks, as follows. We look at the pattern `p_1` | ||
85 | //! on top of the stack, and we have four cases: | ||
86 | //! | ||
87 | //! * 1.1. `p_1 = c(r_1, .., r_a)`, i.e. the top of the stack has constructor `c`. We push onto | ||
88 | //! the stack the arguments of this constructor, and return the result: | ||
89 | //! | ||
90 | //! r_1, .., r_a, p_2, .., p_n | ||
91 | //! | ||
92 | //! * 1.2. `p_1 = c'(r_1, .., r_a')` where `c ≠ c'`. We discard the current stack and return | ||
93 | //! nothing. | ||
94 | //! * 1.3. `p_1 = _`. We push onto the stack as many wildcards as the constructor `c` has | ||
95 | //! arguments (its arity), and return the resulting stack: | ||
96 | //! | ||
97 | //! _, .., _, p_2, .., p_n | ||
98 | //! | ||
99 | //! * 1.4. `p_1 = r_1 | r_2`. We expand the OR-pattern and then recurse on each resulting stack: | ||
100 | //! | ||
101 | //! S(c, (r_1, p_2, .., p_n)) | ||
102 | //! S(c, (r_2, p_2, .., p_n)) | ||
103 | //! | ||
104 | //! 2. We can pop a wildcard off the top of the stack. This is called `D(p)`, where `p` is | ||
105 | //! a pattern-stack. | ||
106 | //! This is used when we know there are missing constructor cases, but there might be | ||
107 | //! existing wildcard patterns, so to check the usefulness of the matrix, we have to check | ||
108 | //! all its *other* components. | ||
109 | //! | ||
110 | //! It is computed as follows. We look at the pattern `p_1` on top of the stack, | ||
111 | //! and we have three cases: | ||
112 | //! * 1.1. `p_1 = c(r_1, .., r_a)`. We discard the current stack and return nothing. | ||
113 | //! * 1.2. `p_1 = _`. We return the rest of the stack: | ||
114 | //! | ||
115 | //! p_2, .., p_n | ||
116 | //! | ||
117 | //! * 1.3. `p_1 = r_1 | r_2`. We expand the OR-pattern and then recurse on each resulting stack: | ||
118 | //! | ||
119 | //! D((r_1, p_2, .., p_n)) | ||
120 | //! D((r_2, p_2, .., p_n)) | ||
121 | //! | ||
122 | //! Note that the OR-patterns are not always used directly in Rust, but are used to derive the | ||
123 | //! exhaustive integer matching rules, so they're written here for posterity. | ||
124 | //! | ||
125 | //! Both those operations extend straightforwardly to a list or pattern-stacks, i.e. a matrix, by | ||
126 | //! working row-by-row. Popping a constructor ends up keeping only the matrix rows that start with | ||
127 | //! the given constructor, and popping a wildcard keeps those rows that start with a wildcard. | ||
128 | //! | ||
129 | //! | ||
130 | //! The algorithm for computing `U` | ||
131 | //! ------------------------------- | ||
132 | //! The algorithm is inductive (on the number of columns: i.e., components of tuple patterns). | ||
133 | //! That means we're going to check the components from left-to-right, so the algorithm | ||
134 | //! operates principally on the first component of the matrix and new pattern-stack `p`. | ||
135 | //! This algorithm is realised in the `is_useful` function. | ||
136 | //! | ||
137 | //! Base case (`n = 0`, i.e., an empty tuple pattern): | ||
138 | //! - If `P` already contains an empty pattern (i.e., if the number of patterns `m > 0`), then | ||
139 | //! `U(P, p)` is false. | ||
140 | //! - Otherwise, `P` must be empty, so `U(P, p)` is true. | ||
141 | //! | ||
142 | //! Inductive step (`n > 0`, i.e., whether there's at least one column [which may then be expanded | ||
143 | //! into further columns later]). We're going to match on the top of the new pattern-stack, `p_1`: | ||
144 | //! | ||
145 | //! - If `p_1 == c(r_1, .., r_a)`, i.e. we have a constructor pattern. | ||
146 | //! Then, the usefulness of `p_1` can be reduced to whether it is useful when | ||
147 | //! we ignore all the patterns in the first column of `P` that involve other constructors. | ||
148 | //! This is where `S(c, P)` comes in: | ||
149 | //! | ||
150 | //! ```text | ||
151 | //! U(P, p) := U(S(c, P), S(c, p)) | ||
152 | //! ``` | ||
153 | //! | ||
154 | //! This special case is handled in `is_useful_specialized`. | ||
155 | //! | ||
156 | //! For example, if `P` is: | ||
157 | //! | ||
158 | //! ```text | ||
159 | //! [ | ||
160 | //! [Some(true), _], | ||
161 | //! [None, 0], | ||
162 | //! ] | ||
163 | //! ``` | ||
164 | //! | ||
165 | //! and `p` is `[Some(false), 0]`, then we don't care about row 2 since we know `p` only | ||
166 | //! matches values that row 2 doesn't. For row 1 however, we need to dig into the | ||
167 | //! arguments of `Some` to know whether some new value is covered. So we compute | ||
168 | //! `U([[true, _]], [false, 0])`. | ||
169 | //! | ||
170 | //! - If `p_1 == _`, then we look at the list of constructors that appear in the first component of | ||
171 | //! the rows of `P`: | ||
172 | //! - If there are some constructors that aren't present, then we might think that the | ||
173 | //! wildcard `_` is useful, since it covers those constructors that weren't covered | ||
174 | //! before. | ||
175 | //! That's almost correct, but only works if there were no wildcards in those first | ||
176 | //! components. So we need to check that `p` is useful with respect to the rows that | ||
177 | //! start with a wildcard, if there are any. This is where `D` comes in: | ||
178 | //! `U(P, p) := U(D(P), D(p))` | ||
179 | //! | ||
180 | //! For example, if `P` is: | ||
181 | //! ```text | ||
182 | //! [ | ||
183 | //! [_, true, _], | ||
184 | //! [None, false, 1], | ||
185 | //! ] | ||
186 | //! ``` | ||
187 | //! and `p` is `[_, false, _]`, the `Some` constructor doesn't appear in `P`. So if we | ||
188 | //! only had row 2, we'd know that `p` is useful. However row 1 starts with a | ||
189 | //! wildcard, so we need to check whether `U([[true, _]], [false, 1])`. | ||
190 | //! | ||
191 | //! - Otherwise, all possible constructors (for the relevant type) are present. In this | ||
192 | //! case we must check whether the wildcard pattern covers any unmatched value. For | ||
193 | //! that, we can think of the `_` pattern as a big OR-pattern that covers all | ||
194 | //! possible constructors. For `Option`, that would mean `_ = None | Some(_)` for | ||
195 | //! example. The wildcard pattern is useful in this case if it is useful when | ||
196 | //! specialized to one of the possible constructors. So we compute: | ||
197 | //! `U(P, p) := ∃(k ϵ constructors) U(S(k, P), S(k, p))` | ||
198 | //! | ||
199 | //! For example, if `P` is: | ||
200 | //! ```text | ||
201 | //! [ | ||
202 | //! [Some(true), _], | ||
203 | //! [None, false], | ||
204 | //! ] | ||
205 | //! ``` | ||
206 | //! and `p` is `[_, false]`, both `None` and `Some` constructors appear in the first | ||
207 | //! components of `P`. We will therefore try popping both constructors in turn: we | ||
208 | //! compute `U([[true, _]], [_, false])` for the `Some` constructor, and `U([[false]], | ||
209 | //! [false])` for the `None` constructor. The first case returns true, so we know that | ||
210 | //! `p` is useful for `P`. Indeed, it matches `[Some(false), _]` that wasn't matched | ||
211 | //! before. | ||
212 | //! | ||
213 | //! - If `p_1 == r_1 | r_2`, then the usefulness depends on each `r_i` separately: | ||
214 | //! | ||
215 | //! ```text | ||
216 | //! U(P, p) := U(P, (r_1, p_2, .., p_n)) | ||
217 | //! || U(P, (r_2, p_2, .., p_n)) | ||
218 | //! ``` | ||
219 | use std::sync::Arc; | ||
220 | |||
221 | use smallvec::{smallvec, SmallVec}; | ||
222 | |||
223 | use crate::{ | ||
224 | db::HirDatabase, | ||
225 | expr::{Body, Expr, Literal, Pat, PatId}, | ||
226 | ApplicationTy, InferenceResult, Ty, TypeCtor, | ||
227 | }; | ||
228 | use hir_def::{adt::VariantData, AdtId, EnumVariantId, VariantId}; | ||
229 | use ra_arena::Idx; | ||
230 | |||
231 | #[derive(Debug, Clone, Copy)] | ||
232 | /// Either a pattern from the source code being analyzed, represented as | ||
233 | /// as `PatId`, or a `Wild` pattern which is created as an intermediate | ||
234 | /// step in the match checking algorithm and thus is not backed by a | ||
235 | /// real `PatId`. | ||
236 | /// | ||
237 | /// Note that it is totally valid for the `PatId` variant to contain | ||
238 | /// a `PatId` which resolves to a `Wild` pattern, if that wild pattern | ||
239 | /// exists in the source code being analyzed. | ||
240 | enum PatIdOrWild { | ||
241 | PatId(PatId), | ||
242 | Wild, | ||
243 | } | ||
244 | |||
245 | impl PatIdOrWild { | ||
246 | fn as_pat(self, cx: &MatchCheckCtx) -> Pat { | ||
247 | match self { | ||
248 | PatIdOrWild::PatId(id) => cx.body.pats[id].clone(), | ||
249 | PatIdOrWild::Wild => Pat::Wild, | ||
250 | } | ||
251 | } | ||
252 | |||
253 | fn as_id(self) -> Option<PatId> { | ||
254 | match self { | ||
255 | PatIdOrWild::PatId(id) => Some(id), | ||
256 | PatIdOrWild::Wild => None, | ||
257 | } | ||
258 | } | ||
259 | } | ||
260 | |||
261 | impl From<PatId> for PatIdOrWild { | ||
262 | fn from(pat_id: PatId) -> Self { | ||
263 | Self::PatId(pat_id) | ||
264 | } | ||
265 | } | ||
266 | |||
267 | impl From<&PatId> for PatIdOrWild { | ||
268 | fn from(pat_id: &PatId) -> Self { | ||
269 | Self::PatId(*pat_id) | ||
270 | } | ||
271 | } | ||
272 | |||
273 | #[derive(Debug, Clone, Copy, PartialEq)] | ||
274 | pub enum MatchCheckErr { | ||
275 | NotImplemented, | ||
276 | MalformedMatchArm, | ||
277 | /// Used when type inference cannot resolve the type of | ||
278 | /// a pattern or expression. | ||
279 | Unknown, | ||
280 | } | ||
281 | |||
282 | /// The return type of `is_useful` is either an indication of usefulness | ||
283 | /// of the match arm, or an error in the case the match statement | ||
284 | /// is made up of types for which exhaustiveness checking is currently | ||
285 | /// not completely implemented. | ||
286 | /// | ||
287 | /// The `std::result::Result` type is used here rather than a custom enum | ||
288 | /// to allow the use of `?`. | ||
289 | pub type MatchCheckResult<T> = Result<T, MatchCheckErr>; | ||
290 | |||
291 | #[derive(Debug)] | ||
292 | /// A row in a Matrix. | ||
293 | /// | ||
294 | /// This type is modeled from the struct of the same name in `rustc`. | ||
295 | pub(crate) struct PatStack(PatStackInner); | ||
296 | type PatStackInner = SmallVec<[PatIdOrWild; 2]>; | ||
297 | |||
298 | impl PatStack { | ||
299 | pub(crate) fn from_pattern(pat_id: PatId) -> PatStack { | ||
300 | Self(smallvec!(pat_id.into())) | ||
301 | } | ||
302 | |||
303 | pub(crate) fn from_wild() -> PatStack { | ||
304 | Self(smallvec!(PatIdOrWild::Wild)) | ||
305 | } | ||
306 | |||
307 | fn from_slice(slice: &[PatIdOrWild]) -> PatStack { | ||
308 | Self(SmallVec::from_slice(slice)) | ||
309 | } | ||
310 | |||
311 | fn from_vec(v: PatStackInner) -> PatStack { | ||
312 | Self(v) | ||
313 | } | ||
314 | |||
315 | fn get_head(&self) -> Option<PatIdOrWild> { | ||
316 | self.0.first().copied() | ||
317 | } | ||
318 | |||
319 | fn tail(&self) -> &[PatIdOrWild] { | ||
320 | self.0.get(1..).unwrap_or(&[]) | ||
321 | } | ||
322 | |||
323 | fn to_tail(&self) -> PatStack { | ||
324 | Self::from_slice(self.tail()) | ||
325 | } | ||
326 | |||
327 | fn replace_head_with<I, T>(&self, pats: I) -> PatStack | ||
328 | where | ||
329 | I: Iterator<Item = T>, | ||
330 | T: Into<PatIdOrWild>, | ||
331 | { | ||
332 | let mut patterns: PatStackInner = smallvec![]; | ||
333 | for pat in pats { | ||
334 | patterns.push(pat.into()); | ||
335 | } | ||
336 | for pat in &self.0[1..] { | ||
337 | patterns.push(*pat); | ||
338 | } | ||
339 | PatStack::from_vec(patterns) | ||
340 | } | ||
341 | |||
342 | /// Computes `D(self)`. | ||
343 | /// | ||
344 | /// See the module docs and the associated documentation in rustc for details. | ||
345 | fn specialize_wildcard(&self, cx: &MatchCheckCtx) -> Option<PatStack> { | ||
346 | if matches!(self.get_head()?.as_pat(cx), Pat::Wild) { | ||
347 | Some(self.to_tail()) | ||
348 | } else { | ||
349 | None | ||
350 | } | ||
351 | } | ||
352 | |||
353 | /// Computes `S(constructor, self)`. | ||
354 | /// | ||
355 | /// See the module docs and the associated documentation in rustc for details. | ||
356 | fn specialize_constructor( | ||
357 | &self, | ||
358 | cx: &MatchCheckCtx, | ||
359 | constructor: &Constructor, | ||
360 | ) -> MatchCheckResult<Option<PatStack>> { | ||
361 | let head = match self.get_head() { | ||
362 | Some(head) => head, | ||
363 | None => return Ok(None), | ||
364 | }; | ||
365 | |||
366 | let head_pat = head.as_pat(cx); | ||
367 | let result = match (head_pat, constructor) { | ||
368 | (Pat::Tuple { args: ref pat_ids, ellipsis }, Constructor::Tuple { arity: _ }) => { | ||
369 | if ellipsis.is_some() { | ||
370 | // If there are ellipsis here, we should add the correct number of | ||
371 | // Pat::Wild patterns to `pat_ids`. We should be able to use the | ||
372 | // constructors arity for this, but at the time of writing we aren't | ||
373 | // correctly calculating this arity when ellipsis are present. | ||
374 | return Err(MatchCheckErr::NotImplemented); | ||
375 | } | ||
376 | |||
377 | Some(self.replace_head_with(pat_ids.iter())) | ||
378 | } | ||
379 | (Pat::Lit(lit_expr), Constructor::Bool(constructor_val)) => { | ||
380 | match cx.body.exprs[lit_expr] { | ||
381 | Expr::Literal(Literal::Bool(pat_val)) if *constructor_val == pat_val => { | ||
382 | Some(self.to_tail()) | ||
383 | } | ||
384 | // it was a bool but the value doesn't match | ||
385 | Expr::Literal(Literal::Bool(_)) => None, | ||
386 | // perhaps this is actually unreachable given we have | ||
387 | // already checked that these match arms have the appropriate type? | ||
388 | _ => return Err(MatchCheckErr::NotImplemented), | ||
389 | } | ||
390 | } | ||
391 | (Pat::Wild, constructor) => Some(self.expand_wildcard(cx, constructor)?), | ||
392 | (Pat::Path(_), Constructor::Enum(constructor)) => { | ||
393 | // unit enum variants become `Pat::Path` | ||
394 | let pat_id = head.as_id().expect("we know this isn't a wild"); | ||
395 | if !enum_variant_matches(cx, pat_id, *constructor) { | ||
396 | None | ||
397 | } else { | ||
398 | Some(self.to_tail()) | ||
399 | } | ||
400 | } | ||
401 | ( | ||
402 | Pat::TupleStruct { args: ref pat_ids, ellipsis, .. }, | ||
403 | Constructor::Enum(enum_constructor), | ||
404 | ) => { | ||
405 | let pat_id = head.as_id().expect("we know this isn't a wild"); | ||
406 | if !enum_variant_matches(cx, pat_id, *enum_constructor) { | ||
407 | None | ||
408 | } else { | ||
409 | let constructor_arity = constructor.arity(cx)?; | ||
410 | if let Some(ellipsis_position) = ellipsis { | ||
411 | // If there are ellipsis in the pattern, the ellipsis must take the place | ||
412 | // of at least one sub-pattern, so `pat_ids` should be smaller than the | ||
413 | // constructor arity. | ||
414 | if pat_ids.len() < constructor_arity { | ||
415 | let mut new_patterns: Vec<PatIdOrWild> = vec![]; | ||
416 | |||
417 | for pat_id in &pat_ids[0..ellipsis_position] { | ||
418 | new_patterns.push((*pat_id).into()); | ||
419 | } | ||
420 | |||
421 | for _ in 0..(constructor_arity - pat_ids.len()) { | ||
422 | new_patterns.push(PatIdOrWild::Wild); | ||
423 | } | ||
424 | |||
425 | for pat_id in &pat_ids[ellipsis_position..pat_ids.len()] { | ||
426 | new_patterns.push((*pat_id).into()); | ||
427 | } | ||
428 | |||
429 | Some(self.replace_head_with(new_patterns.into_iter())) | ||
430 | } else { | ||
431 | return Err(MatchCheckErr::MalformedMatchArm); | ||
432 | } | ||
433 | } else { | ||
434 | // If there is no ellipsis in the tuple pattern, the number | ||
435 | // of patterns must equal the constructor arity. | ||
436 | if pat_ids.len() == constructor_arity { | ||
437 | Some(self.replace_head_with(pat_ids.into_iter())) | ||
438 | } else { | ||
439 | return Err(MatchCheckErr::MalformedMatchArm); | ||
440 | } | ||
441 | } | ||
442 | } | ||
443 | } | ||
444 | (Pat::Record { args: ref arg_patterns, .. }, Constructor::Enum(e)) => { | ||
445 | let pat_id = head.as_id().expect("we know this isn't a wild"); | ||
446 | if !enum_variant_matches(cx, pat_id, *e) { | ||
447 | None | ||
448 | } else { | ||
449 | match cx.db.enum_data(e.parent).variants[e.local_id].variant_data.as_ref() { | ||
450 | VariantData::Record(struct_field_arena) => { | ||
451 | // Here we treat any missing fields in the record as the wild pattern, as | ||
452 | // if the record has ellipsis. We want to do this here even if the | ||
453 | // record does not contain ellipsis, because it allows us to continue | ||
454 | // enforcing exhaustiveness for the rest of the match statement. | ||
455 | // | ||
456 | // Creating the diagnostic for the missing field in the pattern | ||
457 | // should be done in a different diagnostic. | ||
458 | let patterns = struct_field_arena.iter().map(|(_, struct_field)| { | ||
459 | arg_patterns | ||
460 | .iter() | ||
461 | .find(|pat| pat.name == struct_field.name) | ||
462 | .map(|pat| PatIdOrWild::from(pat.pat)) | ||
463 | .unwrap_or(PatIdOrWild::Wild) | ||
464 | }); | ||
465 | |||
466 | Some(self.replace_head_with(patterns)) | ||
467 | } | ||
468 | _ => return Err(MatchCheckErr::Unknown), | ||
469 | } | ||
470 | } | ||
471 | } | ||
472 | (Pat::Or(_), _) => return Err(MatchCheckErr::NotImplemented), | ||
473 | (_, _) => return Err(MatchCheckErr::NotImplemented), | ||
474 | }; | ||
475 | |||
476 | Ok(result) | ||
477 | } | ||
478 | |||
479 | /// A special case of `specialize_constructor` where the head of the pattern stack | ||
480 | /// is a Wild pattern. | ||
481 | /// | ||
482 | /// Replaces the Wild pattern at the head of the pattern stack with N Wild patterns | ||
483 | /// (N >= 0), where N is the arity of the given constructor. | ||
484 | fn expand_wildcard( | ||
485 | &self, | ||
486 | cx: &MatchCheckCtx, | ||
487 | constructor: &Constructor, | ||
488 | ) -> MatchCheckResult<PatStack> { | ||
489 | assert_eq!( | ||
490 | Pat::Wild, | ||
491 | self.get_head().expect("expand_wildcard called on empty PatStack").as_pat(cx), | ||
492 | "expand_wildcard must only be called on PatStack with wild at head", | ||
493 | ); | ||
494 | |||
495 | let mut patterns: PatStackInner = smallvec![]; | ||
496 | |||
497 | for _ in 0..constructor.arity(cx)? { | ||
498 | patterns.push(PatIdOrWild::Wild); | ||
499 | } | ||
500 | |||
501 | for pat in &self.0[1..] { | ||
502 | patterns.push(*pat); | ||
503 | } | ||
504 | |||
505 | Ok(PatStack::from_vec(patterns)) | ||
506 | } | ||
507 | } | ||
508 | |||
509 | /// A collection of PatStack. | ||
510 | /// | ||
511 | /// This type is modeled from the struct of the same name in `rustc`. | ||
512 | pub(crate) struct Matrix(Vec<PatStack>); | ||
513 | |||
514 | impl Matrix { | ||
515 | pub(crate) fn empty() -> Self { | ||
516 | Self(vec![]) | ||
517 | } | ||
518 | |||
519 | pub(crate) fn push(&mut self, cx: &MatchCheckCtx, row: PatStack) { | ||
520 | if let Some(Pat::Or(pat_ids)) = row.get_head().map(|pat_id| pat_id.as_pat(cx)) { | ||
521 | // Or patterns are expanded here | ||
522 | for pat_id in pat_ids { | ||
523 | self.0.push(PatStack::from_pattern(pat_id)); | ||
524 | } | ||
525 | } else { | ||
526 | self.0.push(row); | ||
527 | } | ||
528 | } | ||
529 | |||
530 | fn is_empty(&self) -> bool { | ||
531 | self.0.is_empty() | ||
532 | } | ||
533 | |||
534 | fn heads(&self) -> Vec<PatIdOrWild> { | ||
535 | self.0.iter().flat_map(|p| p.get_head()).collect() | ||
536 | } | ||
537 | |||
538 | /// Computes `D(self)` for each contained PatStack. | ||
539 | /// | ||
540 | /// See the module docs and the associated documentation in rustc for details. | ||
541 | fn specialize_wildcard(&self, cx: &MatchCheckCtx) -> Self { | ||
542 | Self::collect(cx, self.0.iter().filter_map(|r| r.specialize_wildcard(cx))) | ||
543 | } | ||
544 | |||
545 | /// Computes `S(constructor, self)` for each contained PatStack. | ||
546 | /// | ||
547 | /// See the module docs and the associated documentation in rustc for details. | ||
548 | fn specialize_constructor( | ||
549 | &self, | ||
550 | cx: &MatchCheckCtx, | ||
551 | constructor: &Constructor, | ||
552 | ) -> MatchCheckResult<Self> { | ||
553 | let mut new_matrix = Matrix::empty(); | ||
554 | for pat in &self.0 { | ||
555 | if let Some(pat) = pat.specialize_constructor(cx, constructor)? { | ||
556 | new_matrix.push(cx, pat); | ||
557 | } | ||
558 | } | ||
559 | |||
560 | Ok(new_matrix) | ||
561 | } | ||
562 | |||
563 | fn collect<T: IntoIterator<Item = PatStack>>(cx: &MatchCheckCtx, iter: T) -> Self { | ||
564 | let mut matrix = Matrix::empty(); | ||
565 | |||
566 | for pat in iter { | ||
567 | // using push ensures we expand or-patterns | ||
568 | matrix.push(cx, pat); | ||
569 | } | ||
570 | |||
571 | matrix | ||
572 | } | ||
573 | } | ||
574 | |||
575 | #[derive(Clone, Debug, PartialEq)] | ||
576 | /// An indication of the usefulness of a given match arm, where | ||
577 | /// usefulness is defined as matching some patterns which were | ||
578 | /// not matched by an prior match arms. | ||
579 | /// | ||
580 | /// We may eventually need an `Unknown` variant here. | ||
581 | pub enum Usefulness { | ||
582 | Useful, | ||
583 | NotUseful, | ||
584 | } | ||
585 | |||
586 | pub struct MatchCheckCtx<'a> { | ||
587 | pub match_expr: Idx<Expr>, | ||
588 | pub body: Arc<Body>, | ||
589 | pub infer: Arc<InferenceResult>, | ||
590 | pub db: &'a dyn HirDatabase, | ||
591 | } | ||
592 | |||
593 | /// Given a set of patterns `matrix`, and pattern to consider `v`, determines | ||
594 | /// whether `v` is useful. A pattern is useful if it covers cases which were | ||
595 | /// not previously covered. | ||
596 | /// | ||
597 | /// When calling this function externally (that is, not the recursive calls) it | ||
598 | /// expected that you have already type checked the match arms. All patterns in | ||
599 | /// matrix should be the same type as v, as well as they should all be the same | ||
600 | /// type as the match expression. | ||
601 | pub(crate) fn is_useful( | ||
602 | cx: &MatchCheckCtx, | ||
603 | matrix: &Matrix, | ||
604 | v: &PatStack, | ||
605 | ) -> MatchCheckResult<Usefulness> { | ||
606 | // Handle two special cases: | ||
607 | // - enum with no variants | ||
608 | // - `!` type | ||
609 | // In those cases, no match arm is useful. | ||
610 | match cx.infer[cx.match_expr].strip_references() { | ||
611 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Adt(AdtId::EnumId(enum_id)), .. }) => { | ||
612 | if cx.db.enum_data(*enum_id).variants.is_empty() { | ||
613 | return Ok(Usefulness::NotUseful); | ||
614 | } | ||
615 | } | ||
616 | Ty::Apply(ApplicationTy { ctor: TypeCtor::Never, .. }) => { | ||
617 | return Ok(Usefulness::NotUseful); | ||
618 | } | ||
619 | _ => (), | ||
620 | } | ||
621 | |||
622 | let head = match v.get_head() { | ||
623 | Some(head) => head, | ||
624 | None => { | ||
625 | let result = if matrix.is_empty() { Usefulness::Useful } else { Usefulness::NotUseful }; | ||
626 | |||
627 | return Ok(result); | ||
628 | } | ||
629 | }; | ||
630 | |||
631 | if let Pat::Or(pat_ids) = head.as_pat(cx) { | ||
632 | let mut found_unimplemented = false; | ||
633 | let any_useful = pat_ids.iter().any(|&pat_id| { | ||
634 | let v = PatStack::from_pattern(pat_id); | ||
635 | |||
636 | match is_useful(cx, matrix, &v) { | ||
637 | Ok(Usefulness::Useful) => true, | ||
638 | Ok(Usefulness::NotUseful) => false, | ||
639 | _ => { | ||
640 | found_unimplemented = true; | ||
641 | false | ||
642 | } | ||
643 | } | ||
644 | }); | ||
645 | |||
646 | return if any_useful { | ||
647 | Ok(Usefulness::Useful) | ||
648 | } else if found_unimplemented { | ||
649 | Err(MatchCheckErr::NotImplemented) | ||
650 | } else { | ||
651 | Ok(Usefulness::NotUseful) | ||
652 | }; | ||
653 | } | ||
654 | |||
655 | if let Some(constructor) = pat_constructor(cx, head)? { | ||
656 | let matrix = matrix.specialize_constructor(&cx, &constructor)?; | ||
657 | let v = v | ||
658 | .specialize_constructor(&cx, &constructor)? | ||
659 | .expect("we know this can't fail because we get the constructor from `v.head()` above"); | ||
660 | |||
661 | is_useful(&cx, &matrix, &v) | ||
662 | } else { | ||
663 | // expanding wildcard | ||
664 | let mut used_constructors: Vec<Constructor> = vec![]; | ||
665 | for pat in matrix.heads() { | ||
666 | if let Some(constructor) = pat_constructor(cx, pat)? { | ||
667 | used_constructors.push(constructor); | ||
668 | } | ||
669 | } | ||
670 | |||
671 | // We assume here that the first constructor is the "correct" type. Since we | ||
672 | // only care about the "type" of the constructor (i.e. if it is a bool we | ||
673 | // don't care about the value), this assumption should be valid as long as | ||
674 | // the match statement is well formed. We currently uphold this invariant by | ||
675 | // filtering match arms before calling `is_useful`, only passing in match arms | ||
676 | // whose type matches the type of the match expression. | ||
677 | match &used_constructors.first() { | ||
678 | Some(constructor) if all_constructors_covered(&cx, constructor, &used_constructors) => { | ||
679 | // If all constructors are covered, then we need to consider whether | ||
680 | // any values are covered by this wildcard. | ||
681 | // | ||
682 | // For example, with matrix '[[Some(true)], [None]]', all | ||
683 | // constructors are covered (`Some`/`None`), so we need | ||
684 | // to perform specialization to see that our wildcard will cover | ||
685 | // the `Some(false)` case. | ||
686 | // | ||
687 | // Here we create a constructor for each variant and then check | ||
688 | // usefulness after specializing for that constructor. | ||
689 | let mut found_unimplemented = false; | ||
690 | for constructor in constructor.all_constructors(cx) { | ||
691 | let matrix = matrix.specialize_constructor(&cx, &constructor)?; | ||
692 | let v = v.expand_wildcard(&cx, &constructor)?; | ||
693 | |||
694 | match is_useful(&cx, &matrix, &v) { | ||
695 | Ok(Usefulness::Useful) => return Ok(Usefulness::Useful), | ||
696 | Ok(Usefulness::NotUseful) => continue, | ||
697 | _ => found_unimplemented = true, | ||
698 | }; | ||
699 | } | ||
700 | |||
701 | if found_unimplemented { | ||
702 | Err(MatchCheckErr::NotImplemented) | ||
703 | } else { | ||
704 | Ok(Usefulness::NotUseful) | ||
705 | } | ||
706 | } | ||
707 | _ => { | ||
708 | // Either not all constructors are covered, or the only other arms | ||
709 | // are wildcards. Either way, this pattern is useful if it is useful | ||
710 | // when compared to those arms with wildcards. | ||
711 | let matrix = matrix.specialize_wildcard(&cx); | ||
712 | let v = v.to_tail(); | ||
713 | |||
714 | is_useful(&cx, &matrix, &v) | ||
715 | } | ||
716 | } | ||
717 | } | ||
718 | } | ||
719 | |||
720 | #[derive(Debug, Clone, Copy)] | ||
721 | /// Similar to TypeCtor, but includes additional information about the specific | ||
722 | /// value being instantiated. For example, TypeCtor::Bool doesn't contain the | ||
723 | /// boolean value. | ||
724 | enum Constructor { | ||
725 | Bool(bool), | ||
726 | Tuple { arity: usize }, | ||
727 | Enum(EnumVariantId), | ||
728 | } | ||
729 | |||
730 | impl Constructor { | ||
731 | fn arity(&self, cx: &MatchCheckCtx) -> MatchCheckResult<usize> { | ||
732 | let arity = match self { | ||
733 | Constructor::Bool(_) => 0, | ||
734 | Constructor::Tuple { arity } => *arity, | ||
735 | Constructor::Enum(e) => { | ||
736 | match cx.db.enum_data(e.parent).variants[e.local_id].variant_data.as_ref() { | ||
737 | VariantData::Tuple(struct_field_data) => struct_field_data.len(), | ||
738 | VariantData::Record(struct_field_data) => struct_field_data.len(), | ||
739 | VariantData::Unit => 0, | ||
740 | } | ||
741 | } | ||
742 | }; | ||
743 | |||
744 | Ok(arity) | ||
745 | } | ||
746 | |||
747 | fn all_constructors(&self, cx: &MatchCheckCtx) -> Vec<Constructor> { | ||
748 | match self { | ||
749 | Constructor::Bool(_) => vec![Constructor::Bool(true), Constructor::Bool(false)], | ||
750 | Constructor::Tuple { .. } => vec![*self], | ||
751 | Constructor::Enum(e) => cx | ||
752 | .db | ||
753 | .enum_data(e.parent) | ||
754 | .variants | ||
755 | .iter() | ||
756 | .map(|(local_id, _)| { | ||
757 | Constructor::Enum(EnumVariantId { parent: e.parent, local_id }) | ||
758 | }) | ||
759 | .collect(), | ||
760 | } | ||
761 | } | ||
762 | } | ||
763 | |||
764 | /// Returns the constructor for the given pattern. Should only return None | ||
765 | /// in the case of a Wild pattern. | ||
766 | fn pat_constructor(cx: &MatchCheckCtx, pat: PatIdOrWild) -> MatchCheckResult<Option<Constructor>> { | ||
767 | let res = match pat.as_pat(cx) { | ||
768 | Pat::Wild => None, | ||
769 | // FIXME somehow create the Tuple constructor with the proper arity. If there are | ||
770 | // ellipsis, the arity is not equal to the number of patterns. | ||
771 | Pat::Tuple { args: pats, ellipsis } if ellipsis.is_none() => { | ||
772 | Some(Constructor::Tuple { arity: pats.len() }) | ||
773 | } | ||
774 | Pat::Lit(lit_expr) => match cx.body.exprs[lit_expr] { | ||
775 | Expr::Literal(Literal::Bool(val)) => Some(Constructor::Bool(val)), | ||
776 | _ => return Err(MatchCheckErr::NotImplemented), | ||
777 | }, | ||
778 | Pat::TupleStruct { .. } | Pat::Path(_) | Pat::Record { .. } => { | ||
779 | let pat_id = pat.as_id().expect("we already know this pattern is not a wild"); | ||
780 | let variant_id = | ||
781 | cx.infer.variant_resolution_for_pat(pat_id).ok_or(MatchCheckErr::Unknown)?; | ||
782 | match variant_id { | ||
783 | VariantId::EnumVariantId(enum_variant_id) => { | ||
784 | Some(Constructor::Enum(enum_variant_id)) | ||
785 | } | ||
786 | _ => return Err(MatchCheckErr::NotImplemented), | ||
787 | } | ||
788 | } | ||
789 | _ => return Err(MatchCheckErr::NotImplemented), | ||
790 | }; | ||
791 | |||
792 | Ok(res) | ||
793 | } | ||
794 | |||
795 | fn all_constructors_covered( | ||
796 | cx: &MatchCheckCtx, | ||
797 | constructor: &Constructor, | ||
798 | used_constructors: &[Constructor], | ||
799 | ) -> bool { | ||
800 | match constructor { | ||
801 | Constructor::Tuple { arity } => { | ||
802 | used_constructors.iter().any(|constructor| match constructor { | ||
803 | Constructor::Tuple { arity: used_arity } => arity == used_arity, | ||
804 | _ => false, | ||
805 | }) | ||
806 | } | ||
807 | Constructor::Bool(_) => { | ||
808 | if used_constructors.is_empty() { | ||
809 | return false; | ||
810 | } | ||
811 | |||
812 | let covers_true = | ||
813 | used_constructors.iter().any(|c| matches!(c, Constructor::Bool(true))); | ||
814 | let covers_false = | ||
815 | used_constructors.iter().any(|c| matches!(c, Constructor::Bool(false))); | ||
816 | |||
817 | covers_true && covers_false | ||
818 | } | ||
819 | Constructor::Enum(e) => cx.db.enum_data(e.parent).variants.iter().all(|(id, _)| { | ||
820 | for constructor in used_constructors { | ||
821 | if let Constructor::Enum(e) = constructor { | ||
822 | if id == e.local_id { | ||
823 | return true; | ||
824 | } | ||
825 | } | ||
826 | } | ||
827 | |||
828 | false | ||
829 | }), | ||
830 | } | ||
831 | } | ||
832 | |||
833 | fn enum_variant_matches(cx: &MatchCheckCtx, pat_id: PatId, enum_variant_id: EnumVariantId) -> bool { | ||
834 | Some(enum_variant_id.into()) == cx.infer.variant_resolution_for_pat(pat_id) | ||
835 | } | ||
836 | |||
837 | #[cfg(test)] | ||
838 | mod tests { | ||
839 | pub(super) use insta::assert_snapshot; | ||
840 | pub(super) use ra_db::fixture::WithFixture; | ||
841 | |||
842 | pub(super) use crate::{diagnostics::MissingMatchArms, test_db::TestDB}; | ||
843 | |||
844 | pub(super) fn check_diagnostic_message(ra_fixture: &str) -> String { | ||
845 | TestDB::with_single_file(ra_fixture).0.diagnostic::<MissingMatchArms>().0 | ||
846 | } | ||
847 | |||
848 | pub(super) fn check_diagnostic(ra_fixture: &str) { | ||
849 | let diagnostic_count = | ||
850 | TestDB::with_single_file(ra_fixture).0.diagnostic::<MissingMatchArms>().1; | ||
851 | |||
852 | assert_eq!(1, diagnostic_count, "no diagnostic reported"); | ||
853 | } | ||
854 | |||
855 | pub(super) fn check_no_diagnostic(ra_fixture: &str) { | ||
856 | let (s, diagnostic_count) = | ||
857 | TestDB::with_single_file(ra_fixture).0.diagnostic::<MissingMatchArms>(); | ||
858 | |||
859 | assert_eq!(0, diagnostic_count, "expected no diagnostic, found one: {}", s); | ||
860 | } | ||
861 | |||
862 | #[test] | ||
863 | fn empty_tuple_no_arms_diagnostic_message() { | ||
864 | assert_snapshot!( | ||
865 | check_diagnostic_message(r" | ||
866 | fn test_fn() { | ||
867 | match () { | ||
868 | } | ||
869 | } | ||
870 | "), | ||
871 | @"\"()\": Missing match arm\n" | ||
872 | ); | ||
873 | } | ||
874 | |||
875 | #[test] | ||
876 | fn empty_tuple_no_arms() { | ||
877 | check_diagnostic( | ||
878 | r" | ||
879 | fn test_fn() { | ||
880 | match () { | ||
881 | } | ||
882 | } | ||
883 | ", | ||
884 | ); | ||
885 | } | ||
886 | |||
887 | #[test] | ||
888 | fn empty_tuple_wild() { | ||
889 | check_no_diagnostic( | ||
890 | r" | ||
891 | fn test_fn() { | ||
892 | match () { | ||
893 | _ => {} | ||
894 | } | ||
895 | } | ||
896 | ", | ||
897 | ); | ||
898 | } | ||
899 | |||
900 | #[test] | ||
901 | fn empty_tuple_no_diagnostic() { | ||
902 | check_no_diagnostic( | ||
903 | r" | ||
904 | fn test_fn() { | ||
905 | match () { | ||
906 | () => {} | ||
907 | } | ||
908 | } | ||
909 | ", | ||
910 | ); | ||
911 | } | ||
912 | |||
913 | #[test] | ||
914 | fn tuple_of_empty_tuple_no_arms() { | ||
915 | check_diagnostic( | ||
916 | r" | ||
917 | fn test_fn() { | ||
918 | match (()) { | ||
919 | } | ||
920 | } | ||
921 | ", | ||
922 | ); | ||
923 | } | ||
924 | |||
925 | #[test] | ||
926 | fn tuple_of_empty_tuple_no_diagnostic() { | ||
927 | check_no_diagnostic( | ||
928 | r" | ||
929 | fn test_fn() { | ||
930 | match (()) { | ||
931 | (()) => {} | ||
932 | } | ||
933 | } | ||
934 | ", | ||
935 | ); | ||
936 | } | ||
937 | |||
938 | #[test] | ||
939 | fn tuple_of_two_empty_tuple_no_arms() { | ||
940 | check_diagnostic( | ||
941 | r" | ||
942 | fn test_fn() { | ||
943 | match ((), ()) { | ||
944 | } | ||
945 | } | ||
946 | ", | ||
947 | ); | ||
948 | } | ||
949 | |||
950 | #[test] | ||
951 | fn tuple_of_two_empty_tuple_no_diagnostic() { | ||
952 | check_no_diagnostic( | ||
953 | r" | ||
954 | fn test_fn() { | ||
955 | match ((), ()) { | ||
956 | ((), ()) => {} | ||
957 | } | ||
958 | } | ||
959 | ", | ||
960 | ); | ||
961 | } | ||
962 | |||
963 | #[test] | ||
964 | fn bool_no_arms() { | ||
965 | check_diagnostic( | ||
966 | r" | ||
967 | fn test_fn() { | ||
968 | match false { | ||
969 | } | ||
970 | } | ||
971 | ", | ||
972 | ); | ||
973 | } | ||
974 | |||
975 | #[test] | ||
976 | fn bool_missing_arm() { | ||
977 | check_diagnostic( | ||
978 | r" | ||
979 | fn test_fn() { | ||
980 | match false { | ||
981 | true => {} | ||
982 | } | ||
983 | } | ||
984 | ", | ||
985 | ); | ||
986 | } | ||
987 | |||
988 | #[test] | ||
989 | fn bool_no_diagnostic() { | ||
990 | check_no_diagnostic( | ||
991 | r" | ||
992 | fn test_fn() { | ||
993 | match false { | ||
994 | true => {} | ||
995 | false => {} | ||
996 | } | ||
997 | } | ||
998 | ", | ||
999 | ); | ||
1000 | } | ||
1001 | |||
1002 | #[test] | ||
1003 | fn tuple_of_bools_no_arms() { | ||
1004 | check_diagnostic( | ||
1005 | r" | ||
1006 | fn test_fn() { | ||
1007 | match (false, true) { | ||
1008 | } | ||
1009 | } | ||
1010 | ", | ||
1011 | ); | ||
1012 | } | ||
1013 | |||
1014 | #[test] | ||
1015 | fn tuple_of_bools_missing_arms() { | ||
1016 | check_diagnostic( | ||
1017 | r" | ||
1018 | fn test_fn() { | ||
1019 | match (false, true) { | ||
1020 | (true, true) => {}, | ||
1021 | } | ||
1022 | } | ||
1023 | ", | ||
1024 | ); | ||
1025 | } | ||
1026 | |||
1027 | #[test] | ||
1028 | fn tuple_of_bools_missing_arm() { | ||
1029 | check_diagnostic( | ||
1030 | r" | ||
1031 | fn test_fn() { | ||
1032 | match (false, true) { | ||
1033 | (false, true) => {}, | ||
1034 | (false, false) => {}, | ||
1035 | (true, false) => {}, | ||
1036 | } | ||
1037 | } | ||
1038 | ", | ||
1039 | ); | ||
1040 | } | ||
1041 | |||
1042 | #[test] | ||
1043 | fn tuple_of_bools_with_wilds() { | ||
1044 | check_no_diagnostic( | ||
1045 | r" | ||
1046 | fn test_fn() { | ||
1047 | match (false, true) { | ||
1048 | (false, _) => {}, | ||
1049 | (true, false) => {}, | ||
1050 | (_, true) => {}, | ||
1051 | } | ||
1052 | } | ||
1053 | ", | ||
1054 | ); | ||
1055 | } | ||
1056 | |||
1057 | #[test] | ||
1058 | fn tuple_of_bools_no_diagnostic() { | ||
1059 | check_no_diagnostic( | ||
1060 | r" | ||
1061 | fn test_fn() { | ||
1062 | match (false, true) { | ||
1063 | (true, true) => {}, | ||
1064 | (true, false) => {}, | ||
1065 | (false, true) => {}, | ||
1066 | (false, false) => {}, | ||
1067 | } | ||
1068 | } | ||
1069 | ", | ||
1070 | ); | ||
1071 | } | ||
1072 | |||
1073 | #[test] | ||
1074 | fn tuple_of_bools_binding_missing_arms() { | ||
1075 | check_diagnostic( | ||
1076 | r" | ||
1077 | fn test_fn() { | ||
1078 | match (false, true) { | ||
1079 | (true, _x) => {}, | ||
1080 | } | ||
1081 | } | ||
1082 | ", | ||
1083 | ); | ||
1084 | } | ||
1085 | |||
1086 | #[test] | ||
1087 | fn tuple_of_bools_binding_no_diagnostic() { | ||
1088 | check_no_diagnostic( | ||
1089 | r" | ||
1090 | fn test_fn() { | ||
1091 | match (false, true) { | ||
1092 | (true, _x) => {}, | ||
1093 | (false, true) => {}, | ||
1094 | (false, false) => {}, | ||
1095 | } | ||
1096 | } | ||
1097 | ", | ||
1098 | ); | ||
1099 | } | ||
1100 | |||
1101 | #[test] | ||
1102 | fn tuple_of_bools_with_ellipsis_at_end_no_diagnostic() { | ||
1103 | check_no_diagnostic( | ||
1104 | r" | ||
1105 | fn test_fn() { | ||
1106 | match (false, true, false) { | ||
1107 | (false, ..) => {}, | ||
1108 | (true, ..) => {}, | ||
1109 | } | ||
1110 | } | ||
1111 | ", | ||
1112 | ); | ||
1113 | } | ||
1114 | |||
1115 | #[test] | ||
1116 | fn tuple_of_bools_with_ellipsis_at_beginning_no_diagnostic() { | ||
1117 | check_no_diagnostic( | ||
1118 | r" | ||
1119 | fn test_fn() { | ||
1120 | match (false, true, false) { | ||
1121 | (.., false) => {}, | ||
1122 | (.., true) => {}, | ||
1123 | } | ||
1124 | } | ||
1125 | ", | ||
1126 | ); | ||
1127 | } | ||
1128 | |||
1129 | #[test] | ||
1130 | fn tuple_of_bools_with_ellipsis_no_diagnostic() { | ||
1131 | check_no_diagnostic( | ||
1132 | r" | ||
1133 | fn test_fn() { | ||
1134 | match (false, true, false) { | ||
1135 | (..) => {}, | ||
1136 | } | ||
1137 | } | ||
1138 | ", | ||
1139 | ); | ||
1140 | } | ||
1141 | |||
1142 | #[test] | ||
1143 | fn tuple_of_tuple_and_bools_no_arms() { | ||
1144 | check_diagnostic( | ||
1145 | r" | ||
1146 | fn test_fn() { | ||
1147 | match (false, ((), false)) { | ||
1148 | } | ||
1149 | } | ||
1150 | ", | ||
1151 | ); | ||
1152 | } | ||
1153 | |||
1154 | #[test] | ||
1155 | fn tuple_of_tuple_and_bools_missing_arms() { | ||
1156 | check_diagnostic( | ||
1157 | r" | ||
1158 | fn test_fn() { | ||
1159 | match (false, ((), false)) { | ||
1160 | (true, ((), true)) => {}, | ||
1161 | } | ||
1162 | } | ||
1163 | ", | ||
1164 | ); | ||
1165 | } | ||
1166 | |||
1167 | #[test] | ||
1168 | fn tuple_of_tuple_and_bools_no_diagnostic() { | ||
1169 | check_no_diagnostic( | ||
1170 | r" | ||
1171 | fn test_fn() { | ||
1172 | match (false, ((), false)) { | ||
1173 | (true, ((), true)) => {}, | ||
1174 | (true, ((), false)) => {}, | ||
1175 | (false, ((), true)) => {}, | ||
1176 | (false, ((), false)) => {}, | ||
1177 | } | ||
1178 | } | ||
1179 | ", | ||
1180 | ); | ||
1181 | } | ||
1182 | |||
1183 | #[test] | ||
1184 | fn tuple_of_tuple_and_bools_wildcard_missing_arms() { | ||
1185 | check_diagnostic( | ||
1186 | r" | ||
1187 | fn test_fn() { | ||
1188 | match (false, ((), false)) { | ||
1189 | (true, _) => {}, | ||
1190 | } | ||
1191 | } | ||
1192 | ", | ||
1193 | ); | ||
1194 | } | ||
1195 | |||
1196 | #[test] | ||
1197 | fn tuple_of_tuple_and_bools_wildcard_no_diagnostic() { | ||
1198 | check_no_diagnostic( | ||
1199 | r" | ||
1200 | fn test_fn() { | ||
1201 | match (false, ((), false)) { | ||
1202 | (true, ((), true)) => {}, | ||
1203 | (true, ((), false)) => {}, | ||
1204 | (false, _) => {}, | ||
1205 | } | ||
1206 | } | ||
1207 | ", | ||
1208 | ); | ||
1209 | } | ||
1210 | |||
1211 | #[test] | ||
1212 | fn enum_no_arms() { | ||
1213 | check_diagnostic( | ||
1214 | r" | ||
1215 | enum Either { | ||
1216 | A, | ||
1217 | B, | ||
1218 | } | ||
1219 | fn test_fn() { | ||
1220 | match Either::A { | ||
1221 | } | ||
1222 | } | ||
1223 | ", | ||
1224 | ); | ||
1225 | } | ||
1226 | |||
1227 | #[test] | ||
1228 | fn enum_missing_arms() { | ||
1229 | check_diagnostic( | ||
1230 | r" | ||
1231 | enum Either { | ||
1232 | A, | ||
1233 | B, | ||
1234 | } | ||
1235 | fn test_fn() { | ||
1236 | match Either::B { | ||
1237 | Either::A => {}, | ||
1238 | } | ||
1239 | } | ||
1240 | ", | ||
1241 | ); | ||
1242 | } | ||
1243 | |||
1244 | #[test] | ||
1245 | fn enum_no_diagnostic() { | ||
1246 | check_no_diagnostic( | ||
1247 | r" | ||
1248 | enum Either { | ||
1249 | A, | ||
1250 | B, | ||
1251 | } | ||
1252 | fn test_fn() { | ||
1253 | match Either::B { | ||
1254 | Either::A => {}, | ||
1255 | Either::B => {}, | ||
1256 | } | ||
1257 | } | ||
1258 | ", | ||
1259 | ); | ||
1260 | } | ||
1261 | |||
1262 | #[test] | ||
1263 | fn enum_ref_missing_arms() { | ||
1264 | check_diagnostic( | ||
1265 | r" | ||
1266 | enum Either { | ||
1267 | A, | ||
1268 | B, | ||
1269 | } | ||
1270 | fn test_fn() { | ||
1271 | match &Either::B { | ||
1272 | Either::A => {}, | ||
1273 | } | ||
1274 | } | ||
1275 | ", | ||
1276 | ); | ||
1277 | } | ||
1278 | |||
1279 | #[test] | ||
1280 | fn enum_ref_no_diagnostic() { | ||
1281 | check_no_diagnostic( | ||
1282 | r" | ||
1283 | enum Either { | ||
1284 | A, | ||
1285 | B, | ||
1286 | } | ||
1287 | fn test_fn() { | ||
1288 | match &Either::B { | ||
1289 | Either::A => {}, | ||
1290 | Either::B => {}, | ||
1291 | } | ||
1292 | } | ||
1293 | ", | ||
1294 | ); | ||
1295 | } | ||
1296 | |||
1297 | #[test] | ||
1298 | fn enum_containing_bool_no_arms() { | ||
1299 | check_diagnostic( | ||
1300 | r" | ||
1301 | enum Either { | ||
1302 | A(bool), | ||
1303 | B, | ||
1304 | } | ||
1305 | fn test_fn() { | ||
1306 | match Either::B { | ||
1307 | } | ||
1308 | } | ||
1309 | ", | ||
1310 | ); | ||
1311 | } | ||
1312 | |||
1313 | #[test] | ||
1314 | fn enum_containing_bool_missing_arms() { | ||
1315 | check_diagnostic( | ||
1316 | r" | ||
1317 | enum Either { | ||
1318 | A(bool), | ||
1319 | B, | ||
1320 | } | ||
1321 | fn test_fn() { | ||
1322 | match Either::B { | ||
1323 | Either::A(true) => (), | ||
1324 | Either::B => (), | ||
1325 | } | ||
1326 | } | ||
1327 | ", | ||
1328 | ); | ||
1329 | } | ||
1330 | |||
1331 | #[test] | ||
1332 | fn enum_containing_bool_no_diagnostic() { | ||
1333 | check_no_diagnostic( | ||
1334 | r" | ||
1335 | enum Either { | ||
1336 | A(bool), | ||
1337 | B, | ||
1338 | } | ||
1339 | fn test_fn() { | ||
1340 | match Either::B { | ||
1341 | Either::A(true) => (), | ||
1342 | Either::A(false) => (), | ||
1343 | Either::B => (), | ||
1344 | } | ||
1345 | } | ||
1346 | ", | ||
1347 | ); | ||
1348 | } | ||
1349 | |||
1350 | #[test] | ||
1351 | fn enum_containing_bool_with_wild_no_diagnostic() { | ||
1352 | check_no_diagnostic( | ||
1353 | r" | ||
1354 | enum Either { | ||
1355 | A(bool), | ||
1356 | B, | ||
1357 | } | ||
1358 | fn test_fn() { | ||
1359 | match Either::B { | ||
1360 | Either::B => (), | ||
1361 | _ => (), | ||
1362 | } | ||
1363 | } | ||
1364 | ", | ||
1365 | ); | ||
1366 | } | ||
1367 | |||
1368 | #[test] | ||
1369 | fn enum_containing_bool_with_wild_2_no_diagnostic() { | ||
1370 | check_no_diagnostic( | ||
1371 | r" | ||
1372 | enum Either { | ||
1373 | A(bool), | ||
1374 | B, | ||
1375 | } | ||
1376 | fn test_fn() { | ||
1377 | match Either::B { | ||
1378 | Either::A(_) => (), | ||
1379 | Either::B => (), | ||
1380 | } | ||
1381 | } | ||
1382 | ", | ||
1383 | ); | ||
1384 | } | ||
1385 | |||
1386 | #[test] | ||
1387 | fn enum_different_sizes_missing_arms() { | ||
1388 | check_diagnostic( | ||
1389 | r" | ||
1390 | enum Either { | ||
1391 | A(bool), | ||
1392 | B(bool, bool), | ||
1393 | } | ||
1394 | fn test_fn() { | ||
1395 | match Either::A(false) { | ||
1396 | Either::A(_) => (), | ||
1397 | Either::B(false, _) => (), | ||
1398 | } | ||
1399 | } | ||
1400 | ", | ||
1401 | ); | ||
1402 | } | ||
1403 | |||
1404 | #[test] | ||
1405 | fn enum_different_sizes_no_diagnostic() { | ||
1406 | check_no_diagnostic( | ||
1407 | r" | ||
1408 | enum Either { | ||
1409 | A(bool), | ||
1410 | B(bool, bool), | ||
1411 | } | ||
1412 | fn test_fn() { | ||
1413 | match Either::A(false) { | ||
1414 | Either::A(_) => (), | ||
1415 | Either::B(true, _) => (), | ||
1416 | Either::B(false, _) => (), | ||
1417 | } | ||
1418 | } | ||
1419 | ", | ||
1420 | ); | ||
1421 | } | ||
1422 | |||
1423 | #[test] | ||
1424 | fn or_no_diagnostic() { | ||
1425 | check_no_diagnostic( | ||
1426 | r" | ||
1427 | enum Either { | ||
1428 | A(bool), | ||
1429 | B(bool, bool), | ||
1430 | } | ||
1431 | fn test_fn() { | ||
1432 | match Either::A(false) { | ||
1433 | Either::A(true) | Either::A(false) => (), | ||
1434 | Either::B(true, _) => (), | ||
1435 | Either::B(false, _) => (), | ||
1436 | } | ||
1437 | } | ||
1438 | ", | ||
1439 | ); | ||
1440 | } | ||
1441 | |||
1442 | #[test] | ||
1443 | fn tuple_of_enum_no_diagnostic() { | ||
1444 | check_no_diagnostic( | ||
1445 | r" | ||
1446 | enum Either { | ||
1447 | A(bool), | ||
1448 | B(bool, bool), | ||
1449 | } | ||
1450 | enum Either2 { | ||
1451 | C, | ||
1452 | D, | ||
1453 | } | ||
1454 | fn test_fn() { | ||
1455 | match (Either::A(false), Either2::C) { | ||
1456 | (Either::A(true), _) | (Either::A(false), _) => (), | ||
1457 | (Either::B(true, _), Either2::C) => (), | ||
1458 | (Either::B(false, _), Either2::C) => (), | ||
1459 | (Either::B(_, _), Either2::D) => (), | ||
1460 | } | ||
1461 | } | ||
1462 | ", | ||
1463 | ); | ||
1464 | } | ||
1465 | |||
1466 | #[test] | ||
1467 | fn mismatched_types() { | ||
1468 | // Match statements with arms that don't match the | ||
1469 | // expression pattern do not fire this diagnostic. | ||
1470 | check_no_diagnostic( | ||
1471 | r" | ||
1472 | enum Either { | ||
1473 | A, | ||
1474 | B, | ||
1475 | } | ||
1476 | enum Either2 { | ||
1477 | C, | ||
1478 | D, | ||
1479 | } | ||
1480 | fn test_fn() { | ||
1481 | match Either::A { | ||
1482 | Either2::C => (), | ||
1483 | Either2::D => (), | ||
1484 | } | ||
1485 | } | ||
1486 | ", | ||
1487 | ); | ||
1488 | } | ||
1489 | |||
1490 | #[test] | ||
1491 | fn mismatched_types_with_different_arity() { | ||
1492 | // Match statements with arms that don't match the | ||
1493 | // expression pattern do not fire this diagnostic. | ||
1494 | check_no_diagnostic( | ||
1495 | r" | ||
1496 | fn test_fn() { | ||
1497 | match (true, false) { | ||
1498 | (true, false, true) => (), | ||
1499 | (true) => (), | ||
1500 | } | ||
1501 | } | ||
1502 | ", | ||
1503 | ); | ||
1504 | } | ||
1505 | |||
1506 | #[test] | ||
1507 | fn malformed_match_arm_tuple_missing_pattern() { | ||
1508 | // Match statements with arms that don't match the | ||
1509 | // expression pattern do not fire this diagnostic. | ||
1510 | check_no_diagnostic( | ||
1511 | r" | ||
1512 | fn test_fn() { | ||
1513 | match (0) { | ||
1514 | () => (), | ||
1515 | } | ||
1516 | } | ||
1517 | ", | ||
1518 | ); | ||
1519 | } | ||
1520 | |||
1521 | #[test] | ||
1522 | fn malformed_match_arm_tuple_enum_missing_pattern() { | ||
1523 | // We are testing to be sure we don't panic here when the match | ||
1524 | // arm `Either::B` is missing its pattern. | ||
1525 | check_no_diagnostic( | ||
1526 | r" | ||
1527 | enum Either { | ||
1528 | A, | ||
1529 | B(u32), | ||
1530 | } | ||
1531 | fn test_fn() { | ||
1532 | match Either::A { | ||
1533 | Either::A => (), | ||
1534 | Either::B() => (), | ||
1535 | } | ||
1536 | } | ||
1537 | ", | ||
1538 | ); | ||
1539 | } | ||
1540 | |||
1541 | #[test] | ||
1542 | fn enum_not_in_scope() { | ||
1543 | // The enum is not in scope so we don't perform exhaustiveness | ||
1544 | // checking, but we want to be sure we don't panic here (and | ||
1545 | // we don't create a diagnostic). | ||
1546 | check_no_diagnostic( | ||
1547 | r" | ||
1548 | fn test_fn() { | ||
1549 | match Foo::Bar { | ||
1550 | Foo::Baz => (), | ||
1551 | } | ||
1552 | } | ||
1553 | ", | ||
1554 | ); | ||
1555 | } | ||
1556 | |||
1557 | #[test] | ||
1558 | fn expr_diverges() { | ||
1559 | check_no_diagnostic( | ||
1560 | r" | ||
1561 | enum Either { | ||
1562 | A, | ||
1563 | B, | ||
1564 | } | ||
1565 | fn test_fn() { | ||
1566 | match loop {} { | ||
1567 | Either::A => (), | ||
1568 | Either::B => (), | ||
1569 | } | ||
1570 | } | ||
1571 | ", | ||
1572 | ); | ||
1573 | } | ||
1574 | |||
1575 | #[test] | ||
1576 | fn expr_loop_with_break() { | ||
1577 | check_no_diagnostic( | ||
1578 | r" | ||
1579 | enum Either { | ||
1580 | A, | ||
1581 | B, | ||
1582 | } | ||
1583 | fn test_fn() { | ||
1584 | match loop { break Foo::A } { | ||
1585 | Either::A => (), | ||
1586 | Either::B => (), | ||
1587 | } | ||
1588 | } | ||
1589 | ", | ||
1590 | ); | ||
1591 | } | ||
1592 | |||
1593 | #[test] | ||
1594 | fn expr_partially_diverges() { | ||
1595 | check_no_diagnostic( | ||
1596 | r" | ||
1597 | enum Either<T> { | ||
1598 | A(T), | ||
1599 | B, | ||
1600 | } | ||
1601 | fn foo() -> Either<!> { | ||
1602 | Either::B | ||
1603 | } | ||
1604 | fn test_fn() -> u32 { | ||
1605 | match foo() { | ||
1606 | Either::A(val) => val, | ||
1607 | Either::B => 0, | ||
1608 | } | ||
1609 | } | ||
1610 | ", | ||
1611 | ); | ||
1612 | } | ||
1613 | |||
1614 | #[test] | ||
1615 | fn enum_record_no_arms() { | ||
1616 | check_diagnostic( | ||
1617 | r" | ||
1618 | enum Either { | ||
1619 | A { foo: bool }, | ||
1620 | B, | ||
1621 | } | ||
1622 | fn test_fn() { | ||
1623 | let a = Either::A { foo: true }; | ||
1624 | match a { | ||
1625 | } | ||
1626 | } | ||
1627 | ", | ||
1628 | ); | ||
1629 | } | ||
1630 | |||
1631 | #[test] | ||
1632 | fn enum_record_missing_arms() { | ||
1633 | check_diagnostic( | ||
1634 | r" | ||
1635 | enum Either { | ||
1636 | A { foo: bool }, | ||
1637 | B, | ||
1638 | } | ||
1639 | fn test_fn() { | ||
1640 | let a = Either::A { foo: true }; | ||
1641 | match a { | ||
1642 | Either::A { foo: true } => (), | ||
1643 | } | ||
1644 | } | ||
1645 | ", | ||
1646 | ); | ||
1647 | } | ||
1648 | |||
1649 | #[test] | ||
1650 | fn enum_record_no_diagnostic() { | ||
1651 | check_no_diagnostic( | ||
1652 | r" | ||
1653 | enum Either { | ||
1654 | A { foo: bool }, | ||
1655 | B, | ||
1656 | } | ||
1657 | fn test_fn() { | ||
1658 | let a = Either::A { foo: true }; | ||
1659 | match a { | ||
1660 | Either::A { foo: true } => (), | ||
1661 | Either::A { foo: false } => (), | ||
1662 | Either::B => (), | ||
1663 | } | ||
1664 | } | ||
1665 | ", | ||
1666 | ); | ||
1667 | } | ||
1668 | |||
1669 | #[test] | ||
1670 | fn enum_record_missing_field_no_diagnostic() { | ||
1671 | // When `Either::A` is missing a struct member, we don't want | ||
1672 | // to fire the missing match arm diagnostic. This should fire | ||
1673 | // some other diagnostic. | ||
1674 | check_no_diagnostic( | ||
1675 | r" | ||
1676 | enum Either { | ||
1677 | A { foo: bool }, | ||
1678 | B, | ||
1679 | } | ||
1680 | fn test_fn() { | ||
1681 | let a = Either::B; | ||
1682 | match a { | ||
1683 | Either::A { } => (), | ||
1684 | Either::B => (), | ||
1685 | } | ||
1686 | } | ||
1687 | ", | ||
1688 | ); | ||
1689 | } | ||
1690 | |||
1691 | #[test] | ||
1692 | fn enum_record_missing_field_missing_match_arm() { | ||
1693 | // Even though `Either::A` is missing fields, we still want to fire | ||
1694 | // the missing arm diagnostic here, since we know `Either::B` is missing. | ||
1695 | check_diagnostic( | ||
1696 | r" | ||
1697 | enum Either { | ||
1698 | A { foo: bool }, | ||
1699 | B, | ||
1700 | } | ||
1701 | fn test_fn() { | ||
1702 | let a = Either::B; | ||
1703 | match a { | ||
1704 | Either::A { } => (), | ||
1705 | } | ||
1706 | } | ||
1707 | ", | ||
1708 | ); | ||
1709 | } | ||
1710 | |||
1711 | #[test] | ||
1712 | fn enum_record_no_diagnostic_wild() { | ||
1713 | check_no_diagnostic( | ||
1714 | r" | ||
1715 | enum Either { | ||
1716 | A { foo: bool }, | ||
1717 | B, | ||
1718 | } | ||
1719 | fn test_fn() { | ||
1720 | let a = Either::A { foo: true }; | ||
1721 | match a { | ||
1722 | Either::A { foo: _ } => (), | ||
1723 | Either::B => (), | ||
1724 | } | ||
1725 | } | ||
1726 | ", | ||
1727 | ); | ||
1728 | } | ||
1729 | |||
1730 | #[test] | ||
1731 | fn enum_record_fields_out_of_order_missing_arm() { | ||
1732 | check_diagnostic( | ||
1733 | r" | ||
1734 | enum Either { | ||
1735 | A { foo: bool, bar: () }, | ||
1736 | B, | ||
1737 | } | ||
1738 | fn test_fn() { | ||
1739 | let a = Either::A { foo: true }; | ||
1740 | match a { | ||
1741 | Either::A { bar: (), foo: false } => (), | ||
1742 | Either::A { foo: true, bar: () } => (), | ||
1743 | } | ||
1744 | } | ||
1745 | ", | ||
1746 | ); | ||
1747 | } | ||
1748 | |||
1749 | #[test] | ||
1750 | fn enum_record_fields_out_of_order_no_diagnostic() { | ||
1751 | check_no_diagnostic( | ||
1752 | r" | ||
1753 | enum Either { | ||
1754 | A { foo: bool, bar: () }, | ||
1755 | B, | ||
1756 | } | ||
1757 | fn test_fn() { | ||
1758 | let a = Either::A { foo: true }; | ||
1759 | match a { | ||
1760 | Either::A { bar: (), foo: false } => (), | ||
1761 | Either::A { foo: true, bar: () } => (), | ||
1762 | Either::B => (), | ||
1763 | } | ||
1764 | } | ||
1765 | ", | ||
1766 | ); | ||
1767 | } | ||
1768 | |||
1769 | #[test] | ||
1770 | fn enum_record_ellipsis_missing_arm() { | ||
1771 | check_diagnostic( | ||
1772 | r" | ||
1773 | enum Either { | ||
1774 | A { foo: bool, bar: bool }, | ||
1775 | B, | ||
1776 | } | ||
1777 | fn test_fn() { | ||
1778 | match Either::B { | ||
1779 | Either::A { foo: true, .. } => (), | ||
1780 | Either::B => (), | ||
1781 | } | ||
1782 | } | ||
1783 | ", | ||
1784 | ); | ||
1785 | } | ||
1786 | |||
1787 | #[test] | ||
1788 | fn enum_record_ellipsis_no_diagnostic() { | ||
1789 | check_no_diagnostic( | ||
1790 | r" | ||
1791 | enum Either { | ||
1792 | A { foo: bool, bar: bool }, | ||
1793 | B, | ||
1794 | } | ||
1795 | fn test_fn() { | ||
1796 | let a = Either::A { foo: true }; | ||
1797 | match a { | ||
1798 | Either::A { foo: true, .. } => (), | ||
1799 | Either::A { foo: false, .. } => (), | ||
1800 | Either::B => (), | ||
1801 | } | ||
1802 | } | ||
1803 | ", | ||
1804 | ); | ||
1805 | } | ||
1806 | |||
1807 | #[test] | ||
1808 | fn enum_record_ellipsis_all_fields_missing_arm() { | ||
1809 | check_diagnostic( | ||
1810 | r" | ||
1811 | enum Either { | ||
1812 | A { foo: bool, bar: bool }, | ||
1813 | B, | ||
1814 | } | ||
1815 | fn test_fn() { | ||
1816 | let a = Either::B; | ||
1817 | match a { | ||
1818 | Either::A { .. } => (), | ||
1819 | } | ||
1820 | } | ||
1821 | ", | ||
1822 | ); | ||
1823 | } | ||
1824 | |||
1825 | #[test] | ||
1826 | fn enum_record_ellipsis_all_fields_no_diagnostic() { | ||
1827 | check_no_diagnostic( | ||
1828 | r" | ||
1829 | enum Either { | ||
1830 | A { foo: bool, bar: bool }, | ||
1831 | B, | ||
1832 | } | ||
1833 | fn test_fn() { | ||
1834 | let a = Either::B; | ||
1835 | match a { | ||
1836 | Either::A { .. } => (), | ||
1837 | Either::B => (), | ||
1838 | } | ||
1839 | } | ||
1840 | ", | ||
1841 | ); | ||
1842 | } | ||
1843 | |||
1844 | #[test] | ||
1845 | fn enum_tuple_partial_ellipsis_no_diagnostic() { | ||
1846 | check_no_diagnostic( | ||
1847 | r" | ||
1848 | enum Either { | ||
1849 | A(bool, bool, bool, bool), | ||
1850 | B, | ||
1851 | } | ||
1852 | fn test_fn() { | ||
1853 | match Either::B { | ||
1854 | Either::A(true, .., true) => {}, | ||
1855 | Either::A(true, .., false) => {}, | ||
1856 | Either::A(false, .., true) => {}, | ||
1857 | Either::A(false, .., false) => {}, | ||
1858 | Either::B => {}, | ||
1859 | } | ||
1860 | } | ||
1861 | ", | ||
1862 | ); | ||
1863 | } | ||
1864 | |||
1865 | #[test] | ||
1866 | fn enum_tuple_partial_ellipsis_2_no_diagnostic() { | ||
1867 | check_no_diagnostic( | ||
1868 | r" | ||
1869 | enum Either { | ||
1870 | A(bool, bool, bool, bool), | ||
1871 | B, | ||
1872 | } | ||
1873 | fn test_fn() { | ||
1874 | match Either::B { | ||
1875 | Either::A(true, .., true) => {}, | ||
1876 | Either::A(true, .., false) => {}, | ||
1877 | Either::A(.., true) => {}, | ||
1878 | Either::A(.., false) => {}, | ||
1879 | Either::B => {}, | ||
1880 | } | ||
1881 | } | ||
1882 | ", | ||
1883 | ); | ||
1884 | } | ||
1885 | |||
1886 | #[test] | ||
1887 | fn enum_tuple_partial_ellipsis_missing_arm() { | ||
1888 | check_diagnostic( | ||
1889 | r" | ||
1890 | enum Either { | ||
1891 | A(bool, bool, bool, bool), | ||
1892 | B, | ||
1893 | } | ||
1894 | fn test_fn() { | ||
1895 | match Either::B { | ||
1896 | Either::A(true, .., true) => {}, | ||
1897 | Either::A(true, .., false) => {}, | ||
1898 | Either::A(false, .., false) => {}, | ||
1899 | Either::B => {}, | ||
1900 | } | ||
1901 | } | ||
1902 | ", | ||
1903 | ); | ||
1904 | } | ||
1905 | |||
1906 | #[test] | ||
1907 | fn enum_tuple_partial_ellipsis_2_missing_arm() { | ||
1908 | check_diagnostic( | ||
1909 | r" | ||
1910 | enum Either { | ||
1911 | A(bool, bool, bool, bool), | ||
1912 | B, | ||
1913 | } | ||
1914 | fn test_fn() { | ||
1915 | match Either::B { | ||
1916 | Either::A(true, .., true) => {}, | ||
1917 | Either::A(true, .., false) => {}, | ||
1918 | Either::A(.., true) => {}, | ||
1919 | Either::B => {}, | ||
1920 | } | ||
1921 | } | ||
1922 | ", | ||
1923 | ); | ||
1924 | } | ||
1925 | |||
1926 | #[test] | ||
1927 | fn enum_tuple_ellipsis_no_diagnostic() { | ||
1928 | check_no_diagnostic( | ||
1929 | r" | ||
1930 | enum Either { | ||
1931 | A(bool, bool, bool, bool), | ||
1932 | B, | ||
1933 | } | ||
1934 | fn test_fn() { | ||
1935 | match Either::B { | ||
1936 | Either::A(..) => {}, | ||
1937 | Either::B => {}, | ||
1938 | } | ||
1939 | } | ||
1940 | ", | ||
1941 | ); | ||
1942 | } | ||
1943 | |||
1944 | #[test] | ||
1945 | fn enum_never() { | ||
1946 | check_no_diagnostic( | ||
1947 | r" | ||
1948 | enum Never {} | ||
1949 | |||
1950 | fn test_fn(never: Never) { | ||
1951 | match never {} | ||
1952 | } | ||
1953 | ", | ||
1954 | ); | ||
1955 | } | ||
1956 | |||
1957 | #[test] | ||
1958 | fn type_never() { | ||
1959 | check_no_diagnostic( | ||
1960 | r" | ||
1961 | fn test_fn(never: !) { | ||
1962 | match never {} | ||
1963 | } | ||
1964 | ", | ||
1965 | ); | ||
1966 | } | ||
1967 | |||
1968 | #[test] | ||
1969 | fn enum_never_ref() { | ||
1970 | check_no_diagnostic( | ||
1971 | r" | ||
1972 | enum Never {} | ||
1973 | |||
1974 | fn test_fn(never: &Never) { | ||
1975 | match never {} | ||
1976 | } | ||
1977 | ", | ||
1978 | ); | ||
1979 | } | ||
1980 | |||
1981 | #[test] | ||
1982 | fn expr_diverges_missing_arm() { | ||
1983 | check_no_diagnostic( | ||
1984 | r" | ||
1985 | enum Either { | ||
1986 | A, | ||
1987 | B, | ||
1988 | } | ||
1989 | fn test_fn() { | ||
1990 | match loop {} { | ||
1991 | Either::A => (), | ||
1992 | } | ||
1993 | } | ||
1994 | ", | ||
1995 | ); | ||
1996 | } | ||
1997 | |||
1998 | #[test] | ||
1999 | fn or_pattern_panic() { | ||
2000 | check_no_diagnostic( | ||
2001 | r" | ||
2002 | pub enum Category { | ||
2003 | Infinity, | ||
2004 | Zero, | ||
2005 | } | ||
2006 | |||
2007 | fn panic(a: Category, b: Category) { | ||
2008 | match (a, b) { | ||
2009 | (Category::Zero | Category::Infinity, _) => {} | ||
2010 | (_, Category::Zero | Category::Infinity) => {} | ||
2011 | } | ||
2012 | } | ||
2013 | ", | ||
2014 | ); | ||
2015 | } | ||
2016 | |||
2017 | #[test] | ||
2018 | fn or_pattern_panic_2() { | ||
2019 | // FIXME: This is a false positive, but the code used to cause a panic in the match checker, | ||
2020 | // so this acts as a regression test for that. | ||
2021 | check_diagnostic( | ||
2022 | r" | ||
2023 | pub enum Category { | ||
2024 | Infinity, | ||
2025 | Zero, | ||
2026 | } | ||
2027 | |||
2028 | fn panic(a: Category, b: Category) { | ||
2029 | match (a, b) { | ||
2030 | (Category::Infinity, Category::Infinity) | (Category::Zero, Category::Zero) => {} | ||
2031 | |||
2032 | (Category::Infinity | Category::Zero, _) => {} | ||
2033 | } | ||
2034 | } | ||
2035 | ", | ||
2036 | ); | ||
2037 | } | ||
2038 | } | ||
2039 | |||
2040 | #[cfg(test)] | ||
2041 | mod false_negatives { | ||
2042 | //! The implementation of match checking here is a work in progress. As we roll this out, we | ||
2043 | //! prefer false negatives to false positives (ideally there would be no false positives). This | ||
2044 | //! test module should document known false negatives. Eventually we will have a complete | ||
2045 | //! implementation of match checking and this module will be empty. | ||
2046 | //! | ||
2047 | //! The reasons for documenting known false negatives: | ||
2048 | //! | ||
2049 | //! 1. It acts as a backlog of work that can be done to improve the behavior of the system. | ||
2050 | //! 2. It ensures the code doesn't panic when handling these cases. | ||
2051 | |||
2052 | use super::tests::*; | ||
2053 | |||
2054 | #[test] | ||
2055 | fn integers() { | ||
2056 | // This is a false negative. | ||
2057 | // We don't currently check integer exhaustiveness. | ||
2058 | check_no_diagnostic( | ||
2059 | r" | ||
2060 | fn test_fn() { | ||
2061 | match 5 { | ||
2062 | 10 => (), | ||
2063 | 11..20 => (), | ||
2064 | } | ||
2065 | } | ||
2066 | ", | ||
2067 | ); | ||
2068 | } | ||
2069 | |||
2070 | #[test] | ||
2071 | fn internal_or() { | ||
2072 | // This is a false negative. | ||
2073 | // We do not currently handle patterns with internal `or`s. | ||
2074 | check_no_diagnostic( | ||
2075 | r" | ||
2076 | fn test_fn() { | ||
2077 | enum Either { | ||
2078 | A(bool), | ||
2079 | B, | ||
2080 | } | ||
2081 | match Either::B { | ||
2082 | Either::A(true | false) => (), | ||
2083 | } | ||
2084 | } | ||
2085 | ", | ||
2086 | ); | ||
2087 | } | ||
2088 | |||
2089 | #[test] | ||
2090 | fn expr_loop_missing_arm() { | ||
2091 | // This is a false negative. | ||
2092 | // We currently infer the type of `loop { break Foo::A }` to `!`, which | ||
2093 | // causes us to skip the diagnostic since `Either::A` doesn't type check | ||
2094 | // with `!`. | ||
2095 | check_diagnostic( | ||
2096 | r" | ||
2097 | enum Either { | ||
2098 | A, | ||
2099 | B, | ||
2100 | } | ||
2101 | fn test_fn() { | ||
2102 | match loop { break Foo::A } { | ||
2103 | Either::A => (), | ||
2104 | } | ||
2105 | } | ||
2106 | ", | ||
2107 | ); | ||
2108 | } | ||
2109 | |||
2110 | #[test] | ||
2111 | fn tuple_of_bools_with_ellipsis_at_end_missing_arm() { | ||
2112 | // This is a false negative. | ||
2113 | // We don't currently handle tuple patterns with ellipsis. | ||
2114 | check_no_diagnostic( | ||
2115 | r" | ||
2116 | fn test_fn() { | ||
2117 | match (false, true, false) { | ||
2118 | (false, ..) => {}, | ||
2119 | } | ||
2120 | } | ||
2121 | ", | ||
2122 | ); | ||
2123 | } | ||
2124 | |||
2125 | #[test] | ||
2126 | fn tuple_of_bools_with_ellipsis_at_beginning_missing_arm() { | ||
2127 | // This is a false negative. | ||
2128 | // We don't currently handle tuple patterns with ellipsis. | ||
2129 | check_no_diagnostic( | ||
2130 | r" | ||
2131 | fn test_fn() { | ||
2132 | match (false, true, false) { | ||
2133 | (.., false) => {}, | ||
2134 | } | ||
2135 | } | ||
2136 | ", | ||
2137 | ); | ||
2138 | } | ||
2139 | |||
2140 | #[test] | ||
2141 | fn struct_missing_arm() { | ||
2142 | // This is a false negative. | ||
2143 | // We don't currently handle structs. | ||
2144 | check_no_diagnostic( | ||
2145 | r" | ||
2146 | struct Foo { | ||
2147 | a: bool, | ||
2148 | } | ||
2149 | fn test_fn(f: Foo) { | ||
2150 | match f { | ||
2151 | Foo { a: true } => {}, | ||
2152 | } | ||
2153 | } | ||
2154 | ", | ||
2155 | ); | ||
2156 | } | ||
2157 | } | ||