Skip to content

Commit 23dc37d

Browse files
committed
typeck/pat.rs: extract calc_default_binding_mode.
1 parent 3ec5d07 commit 23dc37d

File tree

1 file changed

+34
-23
lines changed
  • src/librustc_typeck/check

1 file changed

+34
-23
lines changed

src/librustc_typeck/check/pat.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6060
PatKind::Path(qpath) => Some(self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span)),
6161
_ => None,
6262
};
63-
64-
let is_non_ref_pat = self.is_non_ref_pat(pat, path_resolution.map(|(res, ..)| res));
65-
let (expected, def_bm) = if is_non_ref_pat {
66-
debug!("pattern is non reference pattern");
67-
self.peel_off_references(pat, expected, def_bm)
68-
} else {
69-
// When you encounter a `&pat` pattern, reset to "by
70-
// value". This is so that `x` and `y` here are by value,
71-
// as they appear to be:
72-
//
73-
// ```
74-
// match &(&22, &44) {
75-
// (&x, &y) => ...
76-
// }
77-
// ```
78-
//
79-
// See issue #46688.
80-
let def_bm = match pat.node {
81-
PatKind::Ref(..) => ty::BindByValue(hir::MutImmutable),
82-
_ => def_bm,
83-
};
84-
(expected, def_bm)
85-
};
63+
let is_nrp = self.is_non_ref_pat(pat, path_resolution.map(|(res, ..)| res));
64+
let (expected, def_bm) = self.calc_default_binding_mode(pat, expected, def_bm, is_nrp);
8665

8766
let ty = match pat.node {
8867
PatKind::Wild => {
@@ -495,6 +474,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
495474
// subtyping.
496475
}
497476

477+
/// Compute the new expected type and default binding mode from the old ones
478+
/// as well as the pattern form we are currently checking.
479+
fn calc_default_binding_mode(
480+
&self,
481+
pat: &'tcx hir::Pat,
482+
expected: Ty<'tcx>,
483+
def_bm: ty::BindingMode,
484+
is_non_ref_pat: bool,
485+
) -> (Ty<'tcx>, ty::BindingMode) {
486+
if is_non_ref_pat {
487+
debug!("pattern is non reference pattern");
488+
self.peel_off_references(pat, expected, def_bm)
489+
} else {
490+
// When you encounter a `&pat` pattern, reset to "by
491+
// value". This is so that `x` and `y` here are by value,
492+
// as they appear to be:
493+
//
494+
// ```
495+
// match &(&22, &44) {
496+
// (&x, &y) => ...
497+
// }
498+
// ```
499+
//
500+
// See issue #46688.
501+
let def_bm = match pat.node {
502+
PatKind::Ref(..) => ty::BindByValue(hir::MutImmutable),
503+
_ => def_bm,
504+
};
505+
(expected, def_bm)
506+
}
507+
}
508+
498509
/// Is the pattern a "non reference pattern"?
499510
/// When the pattern is a path pattern, `opt_path_res` must be `Some(res)`.
500511
fn is_non_ref_pat(&self, pat: &'tcx hir::Pat, opt_path_res: Option<Res>) -> bool {

0 commit comments

Comments
 (0)