Skip to content

Commit 9216b42

Browse files
committed
Add special Skip constructor
1 parent 2bf8ade commit 9216b42

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

compiler/rustc_pattern_analysis/src/constructor.rs

+7
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,10 @@ pub enum Constructor<Cx: TypeCx> {
688688
/// Fake extra constructor for constructors that are not seen in the matrix, as explained at the
689689
/// top of the file.
690690
Missing,
691+
/// Fake extra constructor that indicates that we should skip the column entirely. This is used
692+
/// when a private field is empty, so that we don't observe its emptiness. Only used for
693+
/// specialization.
694+
Skip,
691695
}
692696

693697
impl<Cx: TypeCx> Clone for Constructor<Cx> {
@@ -713,6 +717,7 @@ impl<Cx: TypeCx> Clone for Constructor<Cx> {
713717
Constructor::NonExhaustive => Constructor::NonExhaustive,
714718
Constructor::Hidden => Constructor::Hidden,
715719
Constructor::Missing => Constructor::Missing,
720+
Constructor::Skip => Constructor::Skip,
716721
}
717722
}
718723
}
@@ -767,6 +772,8 @@ impl<Cx: TypeCx> Constructor<Cx> {
767772
}
768773
// Wildcards cover anything
769774
(_, Wildcard) => true,
775+
// `Skip` skips everything.
776+
(Skip, _) => true,
770777
// Only a wildcard pattern can match these special constructors.
771778
(Missing { .. } | NonExhaustive | Hidden, _) => false,
772779

compiler/rustc_pattern_analysis/src/pat.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ impl<Cx: TypeCx> DeconstructedPat<Cx> {
8484
match (&self.ctor, other_ctor) {
8585
// Return a wildcard for each field of `other_ctor`.
8686
(Wildcard, _) => wildcard_sub_tys(),
87+
// Skip this column.
88+
(_, Skip) => smallvec![],
8789
// The only non-trivial case: two slices of different arity. `other_slice` is
8890
// guaranteed to have a larger arity, so we fill the middle part with enough
8991
// wildcards to reach the length of the new, larger slice.
@@ -192,7 +194,7 @@ impl<Cx: TypeCx> fmt::Debug for DeconstructedPat<Cx> {
192194
}
193195
Ok(())
194196
}
195-
Wildcard | Missing { .. } | NonExhaustive | Hidden => write!(f, "_ : {:?}", pat.ty()),
197+
Wildcard | Missing | NonExhaustive | Hidden | Skip => write!(f, "_ : {:?}", pat.ty()),
196198
}
197199
}
198200
}

compiler/rustc_pattern_analysis/src/rustc.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,8 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
249249
}
250250
_ => bug!("bad slice pattern {:?} {:?}", ctor, ty),
251251
},
252-
Bool(..)
253-
| IntRange(..)
254-
| F32Range(..)
255-
| F64Range(..)
256-
| Str(..)
257-
| Opaque(..)
258-
| NonExhaustive
259-
| Hidden
260-
| Missing { .. }
261-
| Wildcard => &[],
252+
Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..)
253+
| NonExhaustive | Hidden | Missing | Skip | Wildcard => &[],
262254
Or => {
263255
bug!("called `Fields::wildcards` on an `Or` ctor")
264256
}
@@ -288,16 +280,8 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
288280
},
289281
Ref => 1,
290282
Slice(slice) => slice.arity(),
291-
Bool(..)
292-
| IntRange(..)
293-
| F32Range(..)
294-
| F64Range(..)
295-
| Str(..)
296-
| Opaque(..)
297-
| NonExhaustive
298-
| Hidden
299-
| Missing { .. }
300-
| Wildcard => 0,
283+
Bool(..) | IntRange(..) | F32Range(..) | F64Range(..) | Str(..) | Opaque(..)
284+
| NonExhaustive | Hidden | Missing | Skip | Wildcard => 0,
301285
Or => bug!("The `Or` constructor doesn't have a fixed arity"),
302286
}
303287
}
@@ -838,7 +822,7 @@ impl<'p, 'tcx: 'p> RustcMatchCheckCtxt<'p, 'tcx> {
838822
}
839823
}
840824
&Str(value) => PatKind::Constant { value },
841-
Wildcard | NonExhaustive | Hidden => PatKind::Wild,
825+
Wildcard | NonExhaustive | Hidden | Skip => PatKind::Wild,
842826
Missing { .. } => bug!(
843827
"trying to convert a `Missing` constructor into a `Pat`; this is probably a bug,
844828
`Missing` should have been processed in `apply_constructors`"

0 commit comments

Comments
 (0)