Skip to content

Commit 01a771a

Browse files
committed
Add debug assertions against slow path reference results
1 parent f95e4f3 commit 01a771a

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

compiler/rustc_mir_build/src/thir/pattern/_match.rs

+33-6
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'tcx> Pat<'tcx> {
416416

417417
/// A row of a matrix. Rows of len 1 are very common, which is why `SmallVec[_; 2]`
418418
/// works well.
419-
#[derive(Debug, Clone)]
419+
#[derive(Debug, Clone, PartialEq)]
420420
crate struct PatStack<'p, 'tcx>(SmallVec<[&'p Pat<'tcx>; 2]>);
421421

422422
impl<'p, 'tcx> PatStack<'p, 'tcx> {
@@ -506,7 +506,7 @@ impl<'p, 'tcx> FromIterator<&'p Pat<'tcx>> for PatStack<'p, 'tcx> {
506506

507507
/// Depending on the match patterns, the specialization process might be able to use a fast path.
508508
/// Tracks whether we can use the fast path and the lookup table needed in those cases.
509-
#[derive(Clone, Debug)]
509+
#[derive(Clone, Debug, PartialEq)]
510510
enum SpecializationCache {
511511
/// Patterns consist of only enum variants.
512512
/// Variant patterns does not intersect with each other (in contrast to range patterns),
@@ -523,7 +523,7 @@ enum SpecializationCache {
523523
}
524524

525525
/// A 2D matrix.
526-
#[derive(Clone)]
526+
#[derive(Clone, PartialEq)]
527527
crate struct Matrix<'p, 'tcx> {
528528
patterns: Vec<PatStack<'p, 'tcx>>,
529529
cache: SpecializationCache,
@@ -622,7 +622,19 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
622622
fn specialize_wildcard(&self) -> Self {
623623
match &self.cache {
624624
SpecializationCache::Variants { wilds, .. } => {
625-
wilds.iter().filter_map(|&i| self.patterns[i].specialize_wildcard()).collect()
625+
let result =
626+
wilds.iter().filter_map(|&i| self.patterns[i].specialize_wildcard()).collect();
627+
// When debug assertions are enabled, check the results against the "slow path"
628+
// result.
629+
debug_assert_eq!(
630+
result,
631+
Self {
632+
patterns: self.patterns.clone(),
633+
cache: SpecializationCache::Incompatible
634+
}
635+
.specialize_wildcard()
636+
);
637+
result
626638
}
627639
SpecializationCache::Incompatible => {
628640
self.patterns.iter().filter_map(|r| r.specialize_wildcard()).collect()
@@ -639,7 +651,7 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
639651
) -> Matrix<'p, 'tcx> {
640652
match &self.cache {
641653
SpecializationCache::Variants { lookup, wilds } => {
642-
if let Constructor::Variant(id) = constructor {
654+
let result: Self = if let Constructor::Variant(id) = constructor {
643655
lookup
644656
.get(id)
645657
// Default to `wilds` for absent keys. See `update_cache` for an explanation.
@@ -655,7 +667,22 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
655667
.collect()
656668
} else {
657669
unreachable!()
658-
}
670+
};
671+
// When debug assertions are enabled, check the results against the "slow path"
672+
// result.
673+
debug_assert_eq!(
674+
result,
675+
Matrix {
676+
patterns: self.patterns.clone(),
677+
cache: SpecializationCache::Incompatible
678+
}
679+
.specialize_constructor(
680+
cx,
681+
constructor,
682+
ctor_wild_subpatterns
683+
)
684+
);
685+
result
659686
}
660687
SpecializationCache::Incompatible => self
661688
.patterns

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ crate enum PatternError {
3939
NonConstPath(Span),
4040
}
4141

42-
#[derive(Copy, Clone, Debug)]
42+
#[derive(Copy, Clone, Debug, PartialEq)]
4343
crate enum BindingMode {
4444
ByValue,
4545
ByRef(BorrowKind),
4646
}
4747

48-
#[derive(Clone, Debug)]
48+
#[derive(Clone, Debug, PartialEq)]
4949
crate struct FieldPat<'tcx> {
5050
crate field: Field,
5151
crate pattern: Pat<'tcx>,
5252
}
5353

54-
#[derive(Clone, Debug)]
54+
#[derive(Clone, Debug, PartialEq)]
5555
crate struct Pat<'tcx> {
5656
crate ty: Ty<'tcx>,
5757
crate span: Span,
@@ -116,7 +116,7 @@ crate struct Ascription<'tcx> {
116116
crate user_ty_span: Span,
117117
}
118118

119-
#[derive(Clone, Debug)]
119+
#[derive(Clone, Debug, PartialEq)]
120120
crate enum PatKind<'tcx> {
121121
Wild,
122122

0 commit comments

Comments
 (0)