Skip to content

Commit 081c3dc

Browse files
committed
Remove all matching on ty.kind() outside cx
1 parent b111b2e commit 081c3dc

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

compiler/rustc_pattern_analysis/src/cx.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,14 @@ pub struct MatchCheckCtxt<'p, 'tcx> {
4949
}
5050

5151
impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
52-
pub(super) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
52+
pub(crate) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
5353
!ty.is_inhabited_from(self.tcx, self.module, self.param_env)
5454
}
5555

56+
pub(crate) fn is_opaque(ty: Ty<'tcx>) -> bool {
57+
matches!(ty.kind(), ty::Alias(ty::Opaque, ..))
58+
}
59+
5660
/// Returns whether the given type is an enum from another crate declared `#[non_exhaustive]`.
5761
pub fn is_foreign_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool {
5862
match ty.kind() {

compiler/rustc_pattern_analysis/src/lints.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
5353
}
5454
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
5555
// version. Otherwise we could encounter constructors for the revealed type and crash.
56-
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
5756
let first_ty = self.patterns[0].ty();
58-
if is_opaque(first_ty) {
57+
if MatchCheckCtxt::is_opaque(first_ty) {
5958
for pat in &self.patterns {
6059
let ty = pat.ty();
61-
if !is_opaque(ty) {
60+
if !MatchCheckCtxt::is_opaque(ty) {
6261
return Some(ty);
6362
}
6463
}

compiler/rustc_pattern_analysis/src/pat.rs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use std::fmt;
66
use smallvec::{smallvec, SmallVec};
77

88
use rustc_data_structures::captures::Captures;
9-
use rustc_middle::ty::{self, Ty};
10-
use rustc_span::{Span, DUMMY_SP};
9+
use rustc_middle::ty::Ty;
10+
use rustc_span::Span;
1111

1212
use self::Constructor::*;
13-
use self::SliceKind::*;
1413

15-
use crate::constructor::{Constructor, SliceKind};
14+
use crate::constructor::{Constructor, Slice, SliceKind};
1615
use crate::cx::MatchCheckCtxt;
1716
use crate::usefulness::PatCtxt;
1817

@@ -90,31 +89,25 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
9089
// We return a wildcard for each field of `other_ctor`.
9190
pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect()
9291
}
93-
(Slice(self_slice), Slice(other_slice))
94-
if self_slice.arity() != other_slice.arity() =>
95-
{
96-
// The only tricky case: two slices of different arity. Since `self_slice` covers
97-
// `other_slice`, `self_slice` must be `VarLen`, i.e. of the form
98-
// `[prefix, .., suffix]`. Moreover `other_slice` is guaranteed to have a larger
99-
// arity. So we fill the middle part with enough wildcards to reach the length of
100-
// the new, larger slice.
101-
match self_slice.kind {
102-
FixedLen(_) => bug!("{:?} doesn't cover {:?}", self_slice, other_slice),
103-
VarLen(prefix, suffix) => {
104-
let (ty::Slice(inner_ty) | ty::Array(inner_ty, _)) = *self.ty.kind() else {
105-
bug!("bad slice pattern {:?} {:?}", self.ctor, self.ty);
106-
};
107-
let prefix = &self.fields[..prefix];
108-
let suffix = &self.fields[self_slice.arity() - suffix..];
109-
let wildcard: &_ = pcx
110-
.cx
111-
.pattern_arena
112-
.alloc(DeconstructedPat::wildcard(inner_ty, DUMMY_SP));
113-
let extra_wildcards = other_slice.arity() - self_slice.arity();
114-
let extra_wildcards = (0..extra_wildcards).map(|_| wildcard);
115-
prefix.iter().chain(extra_wildcards).chain(suffix).collect()
116-
}
92+
(
93+
&Slice(self_slice @ Slice { kind: SliceKind::VarLen(prefix, suffix), .. }),
94+
&Slice(other_slice),
95+
) if self_slice.arity() != other_slice.arity() => {
96+
// The only non-trivial case: two slices of different arity. `other_slice` is
97+
// guaranteed to have a larger arity, so we fill the middle part with enough
98+
// wildcards to reach the length of the new, larger slice.
99+
// Start with a slice of wildcards of the appropriate length.
100+
let mut fields: SmallVec<[_; 2]> =
101+
pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect();
102+
// Fill in the fields from both ends.
103+
let new_arity = fields.len();
104+
for i in 0..prefix {
105+
fields[i] = &self.fields[i];
117106
}
107+
for i in 0..suffix {
108+
fields[new_arity - 1 - i] = &self.fields[self.fields.len() - 1 - i];
109+
}
110+
fields
118111
}
119112
_ => self.fields.iter().collect(),
120113
}

compiler/rustc_pattern_analysis/src/usefulness.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ use smallvec::{smallvec, SmallVec};
556556
use std::fmt;
557557

558558
use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack};
559-
use rustc_middle::ty::{self, Ty};
559+
use rustc_middle::ty::Ty;
560560
use rustc_span::{Span, DUMMY_SP};
561561

562562
use crate::constructor::{Constructor, ConstructorSet};
@@ -856,11 +856,10 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
856856
let mut ty = self.wildcard_row.head().ty();
857857
// If the type is opaque and it is revealed anywhere in the column, we take the revealed
858858
// version. Otherwise we could encounter constructors for the revealed type and crash.
859-
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
860-
if is_opaque(ty) {
859+
if MatchCheckCtxt::is_opaque(ty) {
861860
for pat in self.heads() {
862861
let pat_ty = pat.ty();
863-
if !is_opaque(pat_ty) {
862+
if !MatchCheckCtxt::is_opaque(pat_ty) {
864863
ty = pat_ty;
865864
break;
866865
}

0 commit comments

Comments
 (0)