aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/diagnostics/pattern/usefulness.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/diagnostics/pattern/usefulness.rs')
-rw-r--r--crates/hir_ty/src/diagnostics/pattern/usefulness.rs84
1 files changed, 73 insertions, 11 deletions
diff --git a/crates/hir_ty/src/diagnostics/pattern/usefulness.rs b/crates/hir_ty/src/diagnostics/pattern/usefulness.rs
index f5f6bf494..89e6c6593 100644
--- a/crates/hir_ty/src/diagnostics/pattern/usefulness.rs
+++ b/crates/hir_ty/src/diagnostics/pattern/usefulness.rs
@@ -179,6 +179,7 @@ impl FromIterator<PatId> for PatStack {
179 } 179 }
180} 180}
181 181
182/// A 2D matrix.
182#[derive(Clone)] 183#[derive(Clone)]
183pub(super) struct Matrix { 184pub(super) struct Matrix {
184 patterns: Vec<PatStack>, 185 patterns: Vec<PatStack>,
@@ -321,6 +322,10 @@ impl SubPatSet {
321 } 322 }
322 _ => panic!("bug"), 323 _ => panic!("bug"),
323 } 324 }
325
326 if self.is_full() {
327 *self = Full;
328 }
324 } 329 }
325 330
326 /// Returns a list of the spans of the unreachable subpatterns. If `self` is empty (i.e. the 331 /// Returns a list of the spans of the unreachable subpatterns. If `self` is empty (i.e. the
@@ -449,14 +454,37 @@ impl Usefulness {
449 ) -> Self { 454 ) -> Self {
450 match self { 455 match self {
451 WithWitnesses(witnesses) if witnesses.is_empty() => WithWitnesses(witnesses), 456 WithWitnesses(witnesses) if witnesses.is_empty() => WithWitnesses(witnesses),
452 WithWitnesses(w) => { 457 WithWitnesses(witnesses) => {
453 let new_witnesses = if matches!(ctor, Constructor::Missing) { 458 let new_witnesses = if matches!(ctor, Constructor::Missing) {
454 let mut split_wildcard = SplitWildcard::new(pcx); 459 let mut split_wildcard = SplitWildcard::new(pcx);
455 split_wildcard.split(pcx, matrix.head_ctors(pcx.cx)); 460 split_wildcard.split(pcx, matrix.head_ctors(pcx.cx));
461 // Construct for each missing constructor a "wild" version of this
462 // constructor, that matches everything that can be built with
463 // it. For example, if `ctor` is a `Constructor::Variant` for
464 // `Option::Some`, we get the pattern `Some(_)`.
465 let new_patterns: Vec<_> = split_wildcard
466 .iter_missing(pcx)
467 .map(|missing_ctor| {
468 Fields::wildcards(pcx, missing_ctor).apply(pcx, missing_ctor)
469 })
470 .collect();
471 witnesses
472 .into_iter()
473 .flat_map(|witness| {
474 new_patterns.iter().map(move |pat| {
475 let mut witness = witness.clone();
476 witness.0.push(pat.clone());
477 witness
478 })
479 })
480 .collect()
456 } else { 481 } else {
457 todo!("Usefulness::apply_constructor({:?})", ctor) 482 witnesses
483 .into_iter()
484 .map(|witness| witness.apply_constructor(pcx, &ctor, ctor_wild_subpatterns))
485 .collect()
458 }; 486 };
459 todo!("Usefulness::apply_constructor({:?})", ctor) 487 WithWitnesses(new_witnesses)
460 } 488 }
461 NoWitnesses(subpats) => NoWitnesses(subpats.unspecialize(ctor_wild_subpatterns.len())), 489 NoWitnesses(subpats) => NoWitnesses(subpats.unspecialize(ctor_wild_subpatterns.len())),
462 } 490 }
@@ -469,6 +497,39 @@ enum WitnessPreference {
469 LeaveOutWitness, 497 LeaveOutWitness,
470} 498}
471 499
500/// A witness of non-exhaustiveness for error reporting, represented
501/// as a list of patterns (in reverse order of construction) with
502/// wildcards inside to represent elements that can take any inhabitant
503/// of the type as a value.
504///
505/// A witness against a list of patterns should have the same types
506/// and length as the pattern matched against. Because Rust `match`
507/// is always against a single pattern, at the end the witness will
508/// have length 1, but in the middle of the algorithm, it can contain
509/// multiple patterns.
510///
511/// For example, if we are constructing a witness for the match against
512///
513/// ```
514/// struct Pair(Option<(u32, u32)>, bool);
515///
516/// match (p: Pair) {
517/// Pair(None, _) => {}
518/// Pair(_, false) => {}
519/// }
520/// ```
521///
522/// We'll perform the following steps:
523/// 1. Start with an empty witness
524/// `Witness(vec![])`
525/// 2. Push a witness `true` against the `false`
526/// `Witness(vec![true])`
527/// 3. Push a witness `Some(_)` against the `None`
528/// `Witness(vec![true, Some(_)])`
529/// 4. Apply the `Pair` constructor to the witnesses
530/// `Witness(vec![Pair(Some(_), true)])`
531///
532/// The final `Pair(Some(_), true)` is then the resulting witness.
472#[derive(Clone, Debug)] 533#[derive(Clone, Debug)]
473pub(crate) struct Witness(Vec<Pat>); 534pub(crate) struct Witness(Vec<Pat>);
474 535
@@ -560,7 +621,7 @@ fn is_useful(
560 assert!(rows.iter().all(|r| r.len() == v.len())); 621 assert!(rows.iter().all(|r| r.len() == v.len()));
561 622
562 // FIXME(Nadrieril): Hack to work around type normalization issues (see rust-lang/rust#72476). 623 // FIXME(Nadrieril): Hack to work around type normalization issues (see rust-lang/rust#72476).
563 // TODO(iDawer): ty.as_reference() 624 // TODO(iDawer): ty.strip_references() ?
564 let ty = matrix.heads().next().map_or(cx.type_of(v.head()), |r| cx.type_of(r)); 625 let ty = matrix.heads().next().map_or(cx.type_of(v.head()), |r| cx.type_of(r));
565 let pcx = PatCtxt { cx, ty, is_top_level }; 626 let pcx = PatCtxt { cx, ty, is_top_level };
566 627
@@ -643,6 +704,11 @@ pub(crate) struct UsefulnessReport {
643 pub(crate) non_exhaustiveness_witnesses: Vec<Pat>, 704 pub(crate) non_exhaustiveness_witnesses: Vec<Pat>,
644} 705}
645 706
707/// The entrypoint for the usefulness algorithm. Computes whether a match is exhaustive and which
708/// of its arms are reachable.
709///
710/// Note: the input patterns must have been lowered through
711/// `check_match::MatchVisitor::lower_pattern`.
646pub(crate) fn compute_match_usefulness( 712pub(crate) fn compute_match_usefulness(
647 cx: &MatchCheckCtx<'_>, 713 cx: &MatchCheckCtx<'_>,
648 arms: &[MatchArm], 714 arms: &[MatchArm],
@@ -670,14 +736,10 @@ pub(crate) fn compute_match_usefulness(
670 736
671 let wild_pattern = cx.pattern_arena.borrow_mut().alloc(Pat::Wild, &cx.infer[cx.match_expr]); 737 let wild_pattern = cx.pattern_arena.borrow_mut().alloc(Pat::Wild, &cx.infer[cx.match_expr]);
672 let v = PatStack::from_pattern(wild_pattern); 738 let v = PatStack::from_pattern(wild_pattern);
673 let usefulness = is_useful(cx, &matrix, &v, LeaveOutWitness, false, true); 739 let usefulness = is_useful(cx, &matrix, &v, ConstructWitness, false, true);
674 let non_exhaustiveness_witnesses = match usefulness { 740 let non_exhaustiveness_witnesses = match usefulness {
675 // TODO: ConstructWitness 741 WithWitnesses(pats) => pats.into_iter().map(Witness::single_pattern).collect(),
676 // WithWitnesses(pats) => pats.into_iter().map(Witness::single_pattern).collect(), 742 NoWitnesses(_) => panic!("bug"),
677 // NoWitnesses(_) => panic!("bug"),
678 NoWitnesses(subpats) if subpats.is_empty() => Vec::new(),
679 NoWitnesses(subpats) => vec![Pat::Wild],
680 WithWitnesses(..) => panic!("bug"),
681 }; 743 };
682 UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses } 744 UsefulnessReport { arm_usefulness, non_exhaustiveness_witnesses }
683} 745}