Skip to content

Commit 694cd75

Browse files
Merge pull request #20175 from dianne/match-check-box-cleanup
`hir_ty::match_check` cleanup: remove special handling for box patterns
2 parents 2d518c7 + 8dc54f4 commit 694cd75

File tree

2 files changed

+20
-60
lines changed

2 files changed

+20
-60
lines changed

crates/hir-ty/src/diagnostics/match_check.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use crate::{
2525
db::HirDatabase,
2626
display::{HirDisplay, HirDisplayError, HirFormatter},
2727
infer::BindingMode,
28-
lang_items::is_box,
2928
};
3029

3130
use self::pat_util::EnumerateAndAdjustIterator;
@@ -77,7 +76,7 @@ pub(crate) enum PatKind {
7776
subpatterns: Vec<FieldPat>,
7877
},
7978

80-
/// `box P`, `&P`, `&mut P`, etc.
79+
/// `&P`, `&mut P`, etc.
8180
Deref {
8281
subpattern: Pat,
8382
},
@@ -406,7 +405,6 @@ impl HirDisplay for Pat {
406405
}
407406
PatKind::Deref { subpattern } => {
408407
match self.ty.kind(Interner) {
409-
TyKind::Adt(adt, _) if is_box(f.db, adt.0) => write!(f, "box ")?,
410408
&TyKind::Ref(mutbl, ..) => {
411409
write!(f, "&{}", if mutbl == Mutability::Mut { "mut " } else { "" })?
412410
}

crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
inhabitedness::{is_enum_variant_uninhabited_from, is_ty_uninhabited_from},
2222
};
2323

24-
use super::{FieldPat, Pat, PatKind, is_box};
24+
use super::{FieldPat, Pat, PatKind};
2525

2626
use Constructor::*;
2727

@@ -170,8 +170,6 @@ impl<'db> MatchCheckCtx<'db> {
170170
}
171171
PatKind::Deref { subpattern } => {
172172
ctor = match pat.ty.kind(Interner) {
173-
// This is a box pattern.
174-
TyKind::Adt(adt, _) if is_box(self.db, adt.0) => Struct,
175173
TyKind::Ref(..) => Ref,
176174
_ => {
177175
never!("pattern has unexpected type: pat: {:?}, ty: {:?}", pat, &pat.ty);
@@ -194,23 +192,6 @@ impl<'db> MatchCheckCtx<'db> {
194192
ctor = Struct;
195193
arity = substs.len(Interner);
196194
}
197-
TyKind::Adt(adt, _) if is_box(self.db, adt.0) => {
198-
// The only legal patterns of type `Box` (outside `std`) are `_` and box
199-
// patterns. If we're here we can assume this is a box pattern.
200-
// FIXME(Nadrieril): A `Box` can in theory be matched either with `Box(_,
201-
// _)` or a box pattern. As a hack to avoid an ICE with the former, we
202-
// ignore other fields than the first one. This will trigger an error later
203-
// anyway.
204-
// See https://github.com/rust-lang/rust/issues/82772 ,
205-
// explanation: https://github.com/rust-lang/rust/pull/82789#issuecomment-796921977
206-
// The problem is that we can't know from the type whether we'll match
207-
// normally or through box-patterns. We'll have to figure out a proper
208-
// solution when we introduce generalized deref patterns. Also need to
209-
// prevent mixing of those two options.
210-
fields.retain(|ipat| ipat.idx == 0);
211-
ctor = Struct;
212-
arity = 1;
213-
}
214195
&TyKind::Adt(AdtId(adt), _) => {
215196
ctor = match pat.kind.as_ref() {
216197
PatKind::Leaf { .. } if matches!(adt, hir_def::AdtId::UnionId(_)) => {
@@ -277,12 +258,6 @@ impl<'db> MatchCheckCtx<'db> {
277258
})
278259
.collect(),
279260
},
280-
TyKind::Adt(adt, _) if is_box(self.db, adt.0) => {
281-
// Without `box_patterns`, the only legal pattern of type `Box` is `_` (outside
282-
// of `std`). So this branch is only reachable when the feature is enabled and
283-
// the pattern is a box pattern.
284-
PatKind::Deref { subpattern: subpatterns.next().unwrap() }
285-
}
286261
TyKind::Adt(adt, substs) => {
287262
let variant = Self::variant_id_for_adt(self.db, pat.ctor(), adt.0).unwrap();
288263
let subpatterns = self
@@ -343,14 +318,8 @@ impl PatCx for MatchCheckCtx<'_> {
343318
Struct | Variant(_) | UnionField => match *ty.kind(Interner) {
344319
TyKind::Tuple(arity, ..) => arity,
345320
TyKind::Adt(AdtId(adt), ..) => {
346-
if is_box(self.db, adt) {
347-
// The only legal patterns of type `Box` (outside `std`) are `_` and box
348-
// patterns. If we're here we can assume this is a box pattern.
349-
1
350-
} else {
351-
let variant = Self::variant_id_for_adt(self.db, ctor, adt).unwrap();
352-
variant.fields(self.db).fields().len()
353-
}
321+
let variant = Self::variant_id_for_adt(self.db, ctor, adt).unwrap();
322+
variant.fields(self.db).fields().len()
354323
}
355324
_ => {
356325
never!("Unexpected type for `Single` constructor: {:?}", ty);
@@ -383,29 +352,22 @@ impl PatCx for MatchCheckCtx<'_> {
383352
tys.cloned().map(|ty| (ty, PrivateUninhabitedField(false))).collect()
384353
}
385354
TyKind::Ref(.., rty) => single(rty.clone()),
386-
&TyKind::Adt(AdtId(adt), ref substs) => {
387-
if is_box(self.db, adt) {
388-
// The only legal patterns of type `Box` (outside `std`) are `_` and box
389-
// patterns. If we're here we can assume this is a box pattern.
390-
let subst_ty = substs.at(Interner, 0).assert_ty_ref(Interner).clone();
391-
single(subst_ty)
392-
} else {
393-
let variant = Self::variant_id_for_adt(self.db, ctor, adt).unwrap();
394-
395-
let visibilities = LazyCell::new(|| self.db.field_visibilities(variant));
396-
397-
self.list_variant_fields(ty, variant)
398-
.map(move |(fid, ty)| {
399-
let is_visible = || {
400-
matches!(adt, hir_def::AdtId::EnumId(..))
401-
|| visibilities[fid].is_visible_from(self.db, self.module)
402-
};
403-
let is_uninhabited = self.is_uninhabited(&ty);
404-
let private_uninhabited = is_uninhabited && !is_visible();
405-
(ty, PrivateUninhabitedField(private_uninhabited))
406-
})
407-
.collect()
408-
}
355+
&TyKind::Adt(AdtId(adt), ..) => {
356+
let variant = Self::variant_id_for_adt(self.db, ctor, adt).unwrap();
357+
358+
let visibilities = LazyCell::new(|| self.db.field_visibilities(variant));
359+
360+
self.list_variant_fields(ty, variant)
361+
.map(move |(fid, ty)| {
362+
let is_visible = || {
363+
matches!(adt, hir_def::AdtId::EnumId(..))
364+
|| visibilities[fid].is_visible_from(self.db, self.module)
365+
};
366+
let is_uninhabited = self.is_uninhabited(&ty);
367+
let private_uninhabited = is_uninhabited && !is_visible();
368+
(ty, PrivateUninhabitedField(private_uninhabited))
369+
})
370+
.collect()
409371
}
410372
ty_kind => {
411373
never!("Unexpected type for `{:?}` constructor: {:?}", ctor, ty_kind);

0 commit comments

Comments
 (0)