Skip to content

Commit d1580ee

Browse files
committed
typeck/pat.rs: extract is_non_ref_pat.
1 parent dbe6d59 commit d1580ee

File tree

1 file changed

+35
-27
lines changed
  • src/librustc_typeck/check

1 file changed

+35
-27
lines changed

src/librustc_typeck/check/pat.rs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5656

5757
debug!("check_pat_walk(pat={:?},expected={:?},def_bm={:?})", pat, expected, def_bm);
5858

59-
let mut path_resolution = None;
60-
let is_non_ref_pat = match pat.node {
61-
PatKind::Struct(..) |
62-
PatKind::TupleStruct(..) |
63-
PatKind::Or(_) |
64-
PatKind::Tuple(..) |
65-
PatKind::Box(_) |
66-
PatKind::Range(..) |
67-
PatKind::Slice(..) => true,
68-
PatKind::Lit(ref lt) => {
69-
let ty = self.check_expr(lt);
70-
match ty.sty {
71-
ty::Ref(..) => false,
72-
_ => true,
73-
}
74-
}
75-
PatKind::Path(ref qpath) => {
76-
let resolution = self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span);
77-
path_resolution = Some(resolution);
78-
match resolution.0 {
79-
Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => false,
80-
_ => true,
81-
}
82-
}
83-
PatKind::Wild |
84-
PatKind::Binding(..) |
85-
PatKind::Ref(..) => false,
59+
let path_resolution = match &pat.node {
60+
PatKind::Path(qpath) => Some(self.resolve_ty_and_res_ufcs(qpath, pat.hir_id, pat.span)),
61+
_ => None,
8662
};
63+
64+
let is_non_ref_pat = self.is_non_ref_pat(pat, path_resolution.map(|(res, ..)| res));
8765
if is_non_ref_pat {
8866
debug!("pattern is non reference pattern");
8967
let mut exp_ty = self.resolve_type_vars_with_obligations(&expected);
@@ -560,6 +538,36 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
560538
// subtyping.
561539
}
562540

541+
/// Is the pattern a "non reference pattern"?
542+
/// When the pattern is a path pattern, `opt_path_res` must be `Some(res)`.
543+
fn is_non_ref_pat(&self, pat: &'tcx hir::Pat, opt_path_res: Option<Res>) -> bool {
544+
match pat.node {
545+
PatKind::Struct(..) |
546+
PatKind::TupleStruct(..) |
547+
PatKind::Or(_) |
548+
PatKind::Tuple(..) |
549+
PatKind::Box(_) |
550+
PatKind::Range(..) |
551+
PatKind::Slice(..) => true,
552+
PatKind::Lit(ref lt) => {
553+
let ty = self.check_expr(lt);
554+
match ty.sty {
555+
ty::Ref(..) => false,
556+
_ => true,
557+
}
558+
}
559+
PatKind::Path(_) => {
560+
match opt_path_res.unwrap() {
561+
Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => false,
562+
_ => true,
563+
}
564+
}
565+
PatKind::Wild |
566+
PatKind::Binding(..) |
567+
PatKind::Ref(..) => false,
568+
}
569+
}
570+
563571
fn borrow_pat_suggestion(
564572
&self,
565573
err: &mut DiagnosticBuilder<'_>,

0 commit comments

Comments
 (0)