Skip to content

Commit 0a0a8b7

Browse files
committed
refactor struct pattern checking to get info for peeling
See the previous commit for details. This doesn't yet extract the struct pat's type's ADT def before peeling, but it should now be possible.
1 parent df7a08a commit 0a0a8b7

File tree

1 file changed

+27
-21
lines changed
  • compiler/rustc_hir_typeck/src

1 file changed

+27
-21
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
361361
PatKind::Expr(PatExpr { kind: PatExprKind::Path(qpath), hir_id, span }) => {
362362
Some { 0: &self.check_pat_path(*hir_id, pat.hir_id, *span, qpath, &ti) }
363363
}
364+
PatKind::Struct(ref qpath, fields, has_rest_pat) => {
365+
Some { 0: &self.check_pat_struct(pat, qpath, fields, has_rest_pat) }
366+
}
364367
_ => None,
365368
};
366369
let adjust_mode = self.calc_adjust_mode(pat, resolved_pat.and_then(|r| r.path_res));
@@ -393,9 +396,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
393396
PatKind::TupleStruct(ref qpath, subpats, ddpos) => {
394397
self.check_pat_tuple_struct(pat, qpath, subpats, ddpos, expected, pat_info)
395398
}
396-
PatKind::Struct(ref qpath, fields, has_rest_pat) => {
397-
self.check_pat_struct(pat, qpath, fields, has_rest_pat, expected, pat_info)
398-
}
399+
PatKind::Struct(..) => (resolved_pat.unwrap().check)(expected, pat_info),
399400
PatKind::Guard(pat, cond) => {
400401
self.check_pat(pat, expected, pat_info);
401402
self.check_expr_has_type_or_error(cond, self.tcx.types.bool, |_| {});
@@ -1229,29 +1230,34 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12291230
qpath: &hir::QPath<'tcx>,
12301231
fields: &'tcx [hir::PatField<'tcx>],
12311232
has_rest_pat: bool,
1232-
expected: Ty<'tcx>,
1233-
pat_info: PatInfo<'tcx>,
1234-
) -> Ty<'tcx> {
1233+
) -> ResolvedPat<impl Fn(Ty<'tcx>, PatInfo<'tcx>) -> Ty<'tcx>> {
12351234
// Resolve the path and check the definition for errors.
1236-
let (variant, pat_ty) = match self.check_struct_path(qpath, pat.hir_id) {
1237-
Ok(data) => data,
1238-
Err(guar) => {
1239-
let err = Ty::new_error(self.tcx, guar);
1240-
for field in fields {
1241-
self.check_pat(field.pat, err, pat_info);
1235+
let variant_and_pat_ty = self.check_struct_path(qpath, pat.hir_id);
1236+
1237+
let check = move |expected: Ty<'tcx>, pat_info: PatInfo<'tcx>| -> Ty<'tcx> {
1238+
let (variant, pat_ty) = match variant_and_pat_ty {
1239+
Ok(data) => data,
1240+
Err(guar) => {
1241+
let err = Ty::new_error(self.tcx, guar);
1242+
for field in fields {
1243+
self.check_pat(field.pat, err, pat_info);
1244+
}
1245+
return err;
12421246
}
1243-
return err;
1247+
};
1248+
1249+
// Type-check the path.
1250+
let _ = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
1251+
1252+
// Type-check subpatterns.
1253+
match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info)
1254+
{
1255+
Ok(()) => pat_ty,
1256+
Err(guar) => Ty::new_error(self.tcx, guar),
12441257
}
12451258
};
12461259

1247-
// Type-check the path.
1248-
let _ = self.demand_eqtype_pat(pat.span, expected, pat_ty, &pat_info.top_info);
1249-
1250-
// Type-check subpatterns.
1251-
match self.check_struct_pat_fields(pat_ty, pat, variant, fields, has_rest_pat, pat_info) {
1252-
Ok(()) => pat_ty,
1253-
Err(guar) => Ty::new_error(self.tcx, guar),
1254-
}
1260+
ResolvedPat { path_res: None, check }
12551261
}
12561262

12571263
fn check_pat_path(

0 commit comments

Comments
 (0)