1
Fork 0

Remove all matching on ty.kind() outside cx

This commit is contained in:
Nadrieril 2023-12-11 11:11:12 +01:00
parent b111b2e839
commit 081c3dcf43
4 changed files with 31 additions and 36 deletions

View file

@ -49,10 +49,14 @@ pub struct MatchCheckCtxt<'p, 'tcx> {
} }
impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> { impl<'p, 'tcx> MatchCheckCtxt<'p, 'tcx> {
pub(super) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool { pub(crate) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
!ty.is_inhabited_from(self.tcx, self.module, self.param_env) !ty.is_inhabited_from(self.tcx, self.module, self.param_env)
} }
pub(crate) fn is_opaque(ty: Ty<'tcx>) -> bool {
matches!(ty.kind(), ty::Alias(ty::Opaque, ..))
}
/// Returns whether the given type is an enum from another crate declared `#[non_exhaustive]`. /// Returns whether the given type is an enum from another crate declared `#[non_exhaustive]`.
pub fn is_foreign_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool { pub fn is_foreign_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool {
match ty.kind() { match ty.kind() {

View file

@ -53,12 +53,11 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
} }
// If the type is opaque and it is revealed anywhere in the column, we take the revealed // If the type is opaque and it is revealed anywhere in the column, we take the revealed
// version. Otherwise we could encounter constructors for the revealed type and crash. // version. Otherwise we could encounter constructors for the revealed type and crash.
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..));
let first_ty = self.patterns[0].ty(); let first_ty = self.patterns[0].ty();
if is_opaque(first_ty) { if MatchCheckCtxt::is_opaque(first_ty) {
for pat in &self.patterns { for pat in &self.patterns {
let ty = pat.ty(); let ty = pat.ty();
if !is_opaque(ty) { if !MatchCheckCtxt::is_opaque(ty) {
return Some(ty); return Some(ty);
} }
} }

View file

@ -6,13 +6,12 @@ use std::fmt;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use rustc_data_structures::captures::Captures; use rustc_data_structures::captures::Captures;
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::Ty;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::Span;
use self::Constructor::*; use self::Constructor::*;
use self::SliceKind::*;
use crate::constructor::{Constructor, SliceKind}; use crate::constructor::{Constructor, Slice, SliceKind};
use crate::cx::MatchCheckCtxt; use crate::cx::MatchCheckCtxt;
use crate::usefulness::PatCtxt; use crate::usefulness::PatCtxt;
@ -90,31 +89,25 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
// We return a wildcard for each field of `other_ctor`. // We return a wildcard for each field of `other_ctor`.
pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect() pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect()
} }
(Slice(self_slice), Slice(other_slice)) (
if self_slice.arity() != other_slice.arity() => &Slice(self_slice @ Slice { kind: SliceKind::VarLen(prefix, suffix), .. }),
{ &Slice(other_slice),
// The only tricky case: two slices of different arity. Since `self_slice` covers ) if self_slice.arity() != other_slice.arity() => {
// `other_slice`, `self_slice` must be `VarLen`, i.e. of the form // The only non-trivial case: two slices of different arity. `other_slice` is
// `[prefix, .., suffix]`. Moreover `other_slice` is guaranteed to have a larger // guaranteed to have a larger arity, so we fill the middle part with enough
// arity. So we fill the middle part with enough wildcards to reach the length of // wildcards to reach the length of the new, larger slice.
// the new, larger slice. // Start with a slice of wildcards of the appropriate length.
match self_slice.kind { let mut fields: SmallVec<[_; 2]> =
FixedLen(_) => bug!("{:?} doesn't cover {:?}", self_slice, other_slice), pcx.cx.ctor_wildcard_fields(other_ctor, pcx.ty).iter().collect();
VarLen(prefix, suffix) => { // Fill in the fields from both ends.
let (ty::Slice(inner_ty) | ty::Array(inner_ty, _)) = *self.ty.kind() else { let new_arity = fields.len();
bug!("bad slice pattern {:?} {:?}", self.ctor, self.ty); for i in 0..prefix {
}; fields[i] = &self.fields[i];
let prefix = &self.fields[..prefix];
let suffix = &self.fields[self_slice.arity() - suffix..];
let wildcard: &_ = pcx
.cx
.pattern_arena
.alloc(DeconstructedPat::wildcard(inner_ty, DUMMY_SP));
let extra_wildcards = other_slice.arity() - self_slice.arity();
let extra_wildcards = (0..extra_wildcards).map(|_| wildcard);
prefix.iter().chain(extra_wildcards).chain(suffix).collect()
}
} }
for i in 0..suffix {
fields[new_arity - 1 - i] = &self.fields[self.fields.len() - 1 - i];
}
fields
} }
_ => self.fields.iter().collect(), _ => self.fields.iter().collect(),
} }

View file

@ -556,7 +556,7 @@ use smallvec::{smallvec, SmallVec};
use std::fmt; use std::fmt;
use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack}; use rustc_data_structures::{captures::Captures, stack::ensure_sufficient_stack};
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::Ty;
use rustc_span::{Span, DUMMY_SP}; use rustc_span::{Span, DUMMY_SP};
use crate::constructor::{Constructor, ConstructorSet}; use crate::constructor::{Constructor, ConstructorSet};
@ -856,11 +856,10 @@ impl<'p, 'tcx> Matrix<'p, 'tcx> {
let mut ty = self.wildcard_row.head().ty(); let mut ty = self.wildcard_row.head().ty();
// If the type is opaque and it is revealed anywhere in the column, we take the revealed // If the type is opaque and it is revealed anywhere in the column, we take the revealed
// version. Otherwise we could encounter constructors for the revealed type and crash. // version. Otherwise we could encounter constructors for the revealed type and crash.
let is_opaque = |ty: Ty<'tcx>| matches!(ty.kind(), ty::Alias(ty::Opaque, ..)); if MatchCheckCtxt::is_opaque(ty) {
if is_opaque(ty) {
for pat in self.heads() { for pat in self.heads() {
let pat_ty = pat.ty(); let pat_ty = pat.ty();
if !is_opaque(pat_ty) { if !MatchCheckCtxt::is_opaque(pat_ty) {
ty = pat_ty; ty = pat_ty;
break; break;
} }