typeck/pat.rs: check_pat_top is the entry point.

This clarifies the fact that type checking patterns unconditionally
starts with `BindByValue` as the default binding mode making the
notion of a default binding mode internal to type checking patterns.
This commit is contained in:
Mazdak Farrokhzad 2019-08-24 13:47:07 +02:00
parent 9d69783a46
commit 65deeae76d
3 changed files with 15 additions and 12 deletions

View File

@ -3,7 +3,7 @@
use rustc::hir::{self, ExprKind};
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc::traits::{ObligationCause, ObligationCauseCode};
use rustc::ty::{self, Ty};
use rustc::ty::Ty;
use syntax_pos::Span;
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@ -59,8 +59,7 @@ pub fn check_match(
let mut all_pats_diverge = Diverges::WarnedAlways;
for p in &arm.pats {
self.diverges.set(Diverges::Maybe);
let binding_mode = ty::BindingMode::BindByValue(hir::Mutability::MutImmutable);
self.check_pat_walk(&p, discrim_ty, binding_mode, Some(discrim.span));
self.check_pat_top(&p, discrim_ty, Some(discrim.span));
all_pats_diverge &= self.diverges.get();
}

View File

@ -1104,8 +1104,7 @@ fn check_fn<'a, 'tcx>(
// Add formal parameters.
for (arg_ty, arg) in fn_sig.inputs().iter().zip(&body.arguments) {
// Check the pattern.
let binding_mode = ty::BindingMode::BindByValue(hir::Mutability::MutImmutable);
fcx.check_pat_walk(&arg.pat, arg_ty, binding_mode, None);
fcx.check_pat_top(&arg.pat, arg_ty, None);
// Check that argument is Sized.
// The check for a non-trivial pattern is a hack to avoid duplicate warnings
@ -3631,12 +3630,7 @@ pub fn check_decl_local(&self, local: &'tcx hir::Local) {
}
}
self.check_pat_walk(
&local.pat,
t,
ty::BindingMode::BindByValue(hir::Mutability::MutImmutable),
None,
);
self.check_pat_top(&local.pat, t, None);
let pat_ty = self.node_ty(local.pat.hir_id);
if pat_ty.references_error() {
self.write_ty(local.hir_id, pat_ty);

View File

@ -29,6 +29,16 @@
https://doc.rust-lang.org/reference/types.html#trait-objects";
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub fn check_pat_top(
&self,
pat: &'tcx hir::Pat,
expected: Ty<'tcx>,
discrim_span: Option<Span>,
) {
let def_bm = ty::BindingMode::BindByValue(hir::Mutability::MutImmutable);
self.check_pat_walk(pat, expected, def_bm, discrim_span);
}
/// `discrim_span` argument having a `Span` indicates that this pattern is part of a match
/// expression arm guard, and it points to the match discriminant to add context in type errors.
/// In the following example, `discrim_span` corresponds to the `a + b` expression:
@ -45,7 +55,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// = note: expected type `usize`
/// found type `std::result::Result<_, _>`
/// ```
pub fn check_pat_walk(
fn check_pat_walk(
&self,
pat: &'tcx hir::Pat,
expected: Ty<'tcx>,