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