fallback to provided signature in the event of a type error
This prevents regressions on some annoying cases.
This commit is contained in:
parent
053383dbef
commit
e8a96c97f4
@ -14,6 +14,7 @@ use super::{check_fn, Expectation, FnCtxt};
|
||||
|
||||
use astconv::AstConv;
|
||||
use rustc::hir::def_id::DefId;
|
||||
use rustc::infer::{InferOk, InferResult};
|
||||
use rustc::infer::LateBoundRegionConversionTime;
|
||||
use rustc::infer::type_variable::TypeVariableOrigin;
|
||||
use rustc::ty::{self, ToPolyTraitRef, Ty};
|
||||
@ -30,16 +31,19 @@ struct ClosureSignatures<'tcx> {
|
||||
}
|
||||
|
||||
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
pub fn check_expr_closure(&self,
|
||||
expr: &hir::Expr,
|
||||
_capture: hir::CaptureClause,
|
||||
decl: &'gcx hir::FnDecl,
|
||||
body_id: hir::BodyId,
|
||||
expected: Expectation<'tcx>)
|
||||
-> Ty<'tcx> {
|
||||
debug!("check_expr_closure(expr={:?},expected={:?})",
|
||||
expr,
|
||||
expected);
|
||||
pub fn check_expr_closure(
|
||||
&self,
|
||||
expr: &hir::Expr,
|
||||
_capture: hir::CaptureClause,
|
||||
decl: &'gcx hir::FnDecl,
|
||||
body_id: hir::BodyId,
|
||||
expected: Expectation<'tcx>,
|
||||
) -> Ty<'tcx> {
|
||||
debug!(
|
||||
"check_expr_closure(expr={:?},expected={:?})",
|
||||
expr,
|
||||
expected
|
||||
);
|
||||
|
||||
// It's always helpful for inference if we know the kind of
|
||||
// closure sooner rather than later, so first examine the expected
|
||||
@ -52,60 +56,84 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
self.check_closure(expr, expected_kind, decl, body, expected_sig)
|
||||
}
|
||||
|
||||
fn check_closure(&self,
|
||||
expr: &hir::Expr,
|
||||
opt_kind: Option<ty::ClosureKind>,
|
||||
decl: &'gcx hir::FnDecl,
|
||||
body: &'gcx hir::Body,
|
||||
expected_sig: Option<ty::FnSig<'tcx>>)
|
||||
-> Ty<'tcx> {
|
||||
debug!("check_closure(opt_kind={:?}, expected_sig={:?})",
|
||||
opt_kind,
|
||||
expected_sig);
|
||||
fn check_closure(
|
||||
&self,
|
||||
expr: &hir::Expr,
|
||||
opt_kind: Option<ty::ClosureKind>,
|
||||
decl: &'gcx hir::FnDecl,
|
||||
body: &'gcx hir::Body,
|
||||
expected_sig: Option<ty::FnSig<'tcx>>,
|
||||
) -> Ty<'tcx> {
|
||||
debug!(
|
||||
"check_closure(opt_kind={:?}, expected_sig={:?})",
|
||||
opt_kind,
|
||||
expected_sig
|
||||
);
|
||||
|
||||
let expr_def_id = self.tcx.hir.local_def_id(expr.id);
|
||||
|
||||
let ClosureSignatures { bound_sig, liberated_sig } =
|
||||
self.sig_of_closure(expr_def_id, decl, body, expected_sig);
|
||||
let ClosureSignatures {
|
||||
bound_sig,
|
||||
liberated_sig,
|
||||
} = self.sig_of_closure(expr_def_id, decl, body, expected_sig);
|
||||
|
||||
debug!("check_closure: ty_of_closure returns {:?}", liberated_sig);
|
||||
|
||||
let interior = check_fn(self, self.param_env, liberated_sig, decl, expr.id, body, true).1;
|
||||
let interior = check_fn(
|
||||
self,
|
||||
self.param_env,
|
||||
liberated_sig,
|
||||
decl,
|
||||
expr.id,
|
||||
body,
|
||||
true,
|
||||
).1;
|
||||
|
||||
// Create type variables (for now) to represent the transformed
|
||||
// types of upvars. These will be unified during the upvar
|
||||
// inference phase (`upvar.rs`).
|
||||
let base_substs = Substs::identity_for_item(self.tcx,
|
||||
self.tcx.closure_base_def_id(expr_def_id));
|
||||
let substs = base_substs.extend_to(self.tcx, expr_def_id,
|
||||
|_, _| span_bug!(expr.span, "closure has region param"),
|
||||
|_, _| self.infcx.next_ty_var(TypeVariableOrigin::TransformedUpvar(expr.span))
|
||||
let base_substs =
|
||||
Substs::identity_for_item(self.tcx, self.tcx.closure_base_def_id(expr_def_id));
|
||||
let substs = base_substs.extend_to(
|
||||
self.tcx,
|
||||
expr_def_id,
|
||||
|_, _| span_bug!(expr.span, "closure has region param"),
|
||||
|_, _| {
|
||||
self.infcx
|
||||
.next_ty_var(TypeVariableOrigin::TransformedUpvar(expr.span))
|
||||
},
|
||||
);
|
||||
let closure_type = self.tcx.mk_closure(expr_def_id, substs);
|
||||
|
||||
if let Some(interior) = interior {
|
||||
let closure_substs = ty::ClosureSubsts {
|
||||
substs: substs,
|
||||
};
|
||||
let closure_substs = ty::ClosureSubsts { substs: substs };
|
||||
return self.tcx.mk_generator(expr_def_id, closure_substs, interior);
|
||||
}
|
||||
|
||||
debug!("check_closure: expr.id={:?} closure_type={:?}", expr.id, closure_type);
|
||||
debug!(
|
||||
"check_closure: expr.id={:?} closure_type={:?}",
|
||||
expr.id,
|
||||
closure_type
|
||||
);
|
||||
|
||||
// Tuple up the arguments and insert the resulting function type into
|
||||
// the `closures` table.
|
||||
let sig = bound_sig.map_bound(|sig| self.tcx.mk_fn_sig(
|
||||
iter::once(self.tcx.intern_tup(sig.inputs(), false)),
|
||||
sig.output(),
|
||||
sig.variadic,
|
||||
sig.unsafety,
|
||||
sig.abi
|
||||
));
|
||||
let sig = bound_sig.map_bound(|sig| {
|
||||
self.tcx.mk_fn_sig(
|
||||
iter::once(self.tcx.intern_tup(sig.inputs(), false)),
|
||||
sig.output(),
|
||||
sig.variadic,
|
||||
sig.unsafety,
|
||||
sig.abi,
|
||||
)
|
||||
});
|
||||
|
||||
debug!("check_closure: expr_def_id={:?}, sig={:?}, opt_kind={:?}",
|
||||
expr_def_id,
|
||||
sig,
|
||||
opt_kind);
|
||||
debug!(
|
||||
"check_closure: expr_def_id={:?}, sig={:?}, opt_kind={:?}",
|
||||
expr_def_id,
|
||||
sig,
|
||||
opt_kind
|
||||
);
|
||||
|
||||
{
|
||||
let mut tables = self.tables.borrow_mut();
|
||||
@ -121,22 +149,26 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
closure_type
|
||||
}
|
||||
|
||||
fn deduce_expectations_from_expected_type
|
||||
(&self,
|
||||
expected_ty: Ty<'tcx>)
|
||||
-> (Option<ty::FnSig<'tcx>>, Option<ty::ClosureKind>) {
|
||||
debug!("deduce_expectations_from_expected_type(expected_ty={:?})",
|
||||
expected_ty);
|
||||
fn deduce_expectations_from_expected_type(
|
||||
&self,
|
||||
expected_ty: Ty<'tcx>,
|
||||
) -> (Option<ty::FnSig<'tcx>>, Option<ty::ClosureKind>) {
|
||||
debug!(
|
||||
"deduce_expectations_from_expected_type(expected_ty={:?})",
|
||||
expected_ty
|
||||
);
|
||||
|
||||
match expected_ty.sty {
|
||||
ty::TyDynamic(ref object_type, ..) => {
|
||||
let sig = object_type.projection_bounds()
|
||||
let sig = object_type
|
||||
.projection_bounds()
|
||||
.filter_map(|pb| {
|
||||
let pb = pb.with_self_ty(self.tcx, self.tcx.types.err);
|
||||
self.deduce_sig_from_projection(&pb)
|
||||
})
|
||||
.next();
|
||||
let kind = object_type.principal()
|
||||
let kind = object_type
|
||||
.principal()
|
||||
.and_then(|p| self.tcx.lang_items().fn_trait_kind(p.def_id()));
|
||||
(sig, kind)
|
||||
}
|
||||
@ -146,19 +178,22 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
}
|
||||
}
|
||||
|
||||
fn deduce_expectations_from_obligations
|
||||
(&self,
|
||||
expected_vid: ty::TyVid)
|
||||
-> (Option<ty::FnSig<'tcx>>, Option<ty::ClosureKind>) {
|
||||
fn deduce_expectations_from_obligations(
|
||||
&self,
|
||||
expected_vid: ty::TyVid,
|
||||
) -> (Option<ty::FnSig<'tcx>>, Option<ty::ClosureKind>) {
|
||||
let fulfillment_cx = self.fulfillment_cx.borrow();
|
||||
// Here `expected_ty` is known to be a type inference variable.
|
||||
|
||||
let expected_sig = fulfillment_cx.pending_obligations()
|
||||
let expected_sig = fulfillment_cx
|
||||
.pending_obligations()
|
||||
.iter()
|
||||
.map(|obligation| &obligation.obligation)
|
||||
.filter_map(|obligation| {
|
||||
debug!("deduce_expectations_from_obligations: obligation.predicate={:?}",
|
||||
obligation.predicate);
|
||||
debug!(
|
||||
"deduce_expectations_from_obligations: obligation.predicate={:?}",
|
||||
obligation.predicate
|
||||
);
|
||||
|
||||
match obligation.predicate {
|
||||
// Given a Projection predicate, we can potentially infer
|
||||
@ -177,7 +212,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
// infer the kind. This can occur if there is a trait-reference
|
||||
// like `F : Fn<A>`. Note that due to subtyping we could encounter
|
||||
// many viable options, so pick the most restrictive.
|
||||
let expected_kind = fulfillment_cx.pending_obligations()
|
||||
let expected_kind = fulfillment_cx
|
||||
.pending_obligations()
|
||||
.iter()
|
||||
.map(|obligation| &obligation.obligation)
|
||||
.filter_map(|obligation| {
|
||||
@ -202,20 +238,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
// inference variable.
|
||||
ty::Predicate::ClosureKind(..) => None,
|
||||
};
|
||||
opt_trait_ref.and_then(|tr| self.self_type_matches_expected_vid(tr, expected_vid))
|
||||
opt_trait_ref
|
||||
.and_then(|tr| self.self_type_matches_expected_vid(tr, expected_vid))
|
||||
.and_then(|tr| self.tcx.lang_items().fn_trait_kind(tr.def_id()))
|
||||
})
|
||||
.fold(None,
|
||||
|best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur))));
|
||||
.fold(None, |best, cur| {
|
||||
Some(best.map_or(cur, |best| cmp::min(best, cur)))
|
||||
});
|
||||
|
||||
(expected_sig, expected_kind)
|
||||
}
|
||||
|
||||
/// Given a projection like "<F as Fn(X)>::Result == Y", we can deduce
|
||||
/// everything we need to know about a closure.
|
||||
fn deduce_sig_from_projection(&self,
|
||||
projection: &ty::PolyProjectionPredicate<'tcx>)
|
||||
-> Option<ty::FnSig<'tcx>> {
|
||||
fn deduce_sig_from_projection(
|
||||
&self,
|
||||
projection: &ty::PolyProjectionPredicate<'tcx>,
|
||||
) -> Option<ty::FnSig<'tcx>> {
|
||||
let tcx = self.tcx;
|
||||
|
||||
debug!("deduce_sig_from_projection({:?})", projection);
|
||||
@ -228,8 +267,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
|
||||
let arg_param_ty = trait_ref.substs().type_at(1);
|
||||
let arg_param_ty = self.resolve_type_vars_if_possible(&arg_param_ty);
|
||||
debug!("deduce_sig_from_projection: arg_param_ty {:?}",
|
||||
arg_param_ty);
|
||||
debug!(
|
||||
"deduce_sig_from_projection: arg_param_ty {:?}",
|
||||
arg_param_ty
|
||||
);
|
||||
|
||||
let input_tys = match arg_param_ty.sty {
|
||||
ty::TyTuple(tys, _) => tys.into_iter(),
|
||||
@ -240,41 +281,47 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
|
||||
let ret_param_ty = projection.0.ty;
|
||||
let ret_param_ty = self.resolve_type_vars_if_possible(&ret_param_ty);
|
||||
debug!("deduce_sig_from_projection: ret_param_ty {:?}", ret_param_ty);
|
||||
debug!(
|
||||
"deduce_sig_from_projection: ret_param_ty {:?}",
|
||||
ret_param_ty
|
||||
);
|
||||
|
||||
let fn_sig = self.tcx.mk_fn_sig(
|
||||
input_tys.cloned(),
|
||||
ret_param_ty,
|
||||
false,
|
||||
hir::Unsafety::Normal,
|
||||
Abi::Rust
|
||||
Abi::Rust,
|
||||
);
|
||||
debug!("deduce_sig_from_projection: fn_sig {:?}", fn_sig);
|
||||
|
||||
Some(fn_sig)
|
||||
}
|
||||
|
||||
fn self_type_matches_expected_vid(&self,
|
||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||
expected_vid: ty::TyVid)
|
||||
-> Option<ty::PolyTraitRef<'tcx>> {
|
||||
fn self_type_matches_expected_vid(
|
||||
&self,
|
||||
trait_ref: ty::PolyTraitRef<'tcx>,
|
||||
expected_vid: ty::TyVid,
|
||||
) -> Option<ty::PolyTraitRef<'tcx>> {
|
||||
let self_ty = self.shallow_resolve(trait_ref.self_ty());
|
||||
debug!("self_type_matches_expected_vid(trait_ref={:?}, self_ty={:?})",
|
||||
trait_ref,
|
||||
self_ty);
|
||||
debug!(
|
||||
"self_type_matches_expected_vid(trait_ref={:?}, self_ty={:?})",
|
||||
trait_ref,
|
||||
self_ty
|
||||
);
|
||||
match self_ty.sty {
|
||||
ty::TyInfer(ty::TyVar(v)) if expected_vid == v => Some(trait_ref),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn sig_of_closure(&self,
|
||||
expr_def_id: DefId,
|
||||
decl: &hir::FnDecl,
|
||||
body: &hir::Body,
|
||||
expected_sig: Option<ty::FnSig<'tcx>>)
|
||||
-> ClosureSignatures<'tcx>
|
||||
{
|
||||
fn sig_of_closure(
|
||||
&self,
|
||||
expr_def_id: DefId,
|
||||
decl: &hir::FnDecl,
|
||||
body: &hir::Body,
|
||||
expected_sig: Option<ty::FnSig<'tcx>>,
|
||||
) -> ClosureSignatures<'tcx> {
|
||||
if let Some(e) = expected_sig {
|
||||
self.sig_of_closure_with_expectation(expr_def_id, decl, body, e)
|
||||
} else {
|
||||
@ -284,12 +331,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
|
||||
/// If there is no expected signature, then we will convert the
|
||||
/// types that the user gave into a signature.
|
||||
fn sig_of_closure_no_expectation(&self,
|
||||
expr_def_id: DefId,
|
||||
decl: &hir::FnDecl,
|
||||
body: &hir::Body)
|
||||
-> ClosureSignatures<'tcx>
|
||||
{
|
||||
fn sig_of_closure_no_expectation(
|
||||
&self,
|
||||
expr_def_id: DefId,
|
||||
decl: &hir::FnDecl,
|
||||
body: &hir::Body,
|
||||
) -> ClosureSignatures<'tcx> {
|
||||
debug!("sig_of_closure_no_expectation()");
|
||||
|
||||
let bound_sig = self.supplied_sig_of_closure(decl);
|
||||
@ -344,14 +391,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
/// - `expected_sig`: the expected signature (if any). Note that
|
||||
/// this is missing a binder: that is, there may be late-bound
|
||||
/// regions with depth 1, which are bound then by the closure.
|
||||
fn sig_of_closure_with_expectation(&self,
|
||||
expr_def_id: DefId,
|
||||
decl: &hir::FnDecl,
|
||||
body: &hir::Body,
|
||||
expected_sig: ty::FnSig<'tcx>)
|
||||
-> ClosureSignatures<'tcx>
|
||||
{
|
||||
debug!("sig_of_closure_with_expectation(expected_sig={:?})", expected_sig);
|
||||
fn sig_of_closure_with_expectation(
|
||||
&self,
|
||||
expr_def_id: DefId,
|
||||
decl: &hir::FnDecl,
|
||||
body: &hir::Body,
|
||||
expected_sig: ty::FnSig<'tcx>,
|
||||
) -> ClosureSignatures<'tcx> {
|
||||
debug!(
|
||||
"sig_of_closure_with_expectation(expected_sig={:?})",
|
||||
expected_sig
|
||||
);
|
||||
|
||||
// Watch out for some surprises and just ignore the
|
||||
// expectation if things don't see to match up with what we
|
||||
@ -367,13 +417,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
// regions appearing free in `expected_sig` are now bound up
|
||||
// in this binder we are creating.
|
||||
assert!(!expected_sig.has_regions_escaping_depth(1));
|
||||
let bound_sig =
|
||||
ty::Binder(self.tcx.mk_fn_sig(
|
||||
expected_sig.inputs().iter().cloned(),
|
||||
expected_sig.output(),
|
||||
decl.variadic,
|
||||
hir::Unsafety::Normal,
|
||||
Abi::RustCall));
|
||||
let bound_sig = ty::Binder(self.tcx.mk_fn_sig(
|
||||
expected_sig.inputs().iter().cloned(),
|
||||
expected_sig.output(),
|
||||
decl.variadic,
|
||||
hir::Unsafety::Normal,
|
||||
Abi::RustCall,
|
||||
));
|
||||
|
||||
// `deduce_expectations_from_expected_type` introduces
|
||||
// late-bound lifetimes defined elsewhere, which we now
|
||||
@ -387,7 +437,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
// Along the way, it also writes out entries for types that the user
|
||||
// wrote into our tables, which are then later used by the privacy
|
||||
// check.
|
||||
self.check_supplied_sig_against_expectation(decl, &closure_sigs);
|
||||
match self.check_supplied_sig_against_expectation(decl, &closure_sigs) {
|
||||
Ok(infer_ok) => self.register_infer_ok_obligations(infer_ok),
|
||||
Err(_) => return self.sig_of_closure_no_expectation(expr_def_id, decl, body),
|
||||
}
|
||||
|
||||
closure_sigs
|
||||
}
|
||||
@ -395,58 +448,90 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
/// Enforce the user's types against the expectation. See
|
||||
/// `sig_of_closure_with_expectation` for details on the overall
|
||||
/// strategy.
|
||||
fn check_supplied_sig_against_expectation(&self,
|
||||
decl: &hir::FnDecl,
|
||||
expected_sigs: &ClosureSignatures<'tcx>)
|
||||
{
|
||||
fn check_supplied_sig_against_expectation(
|
||||
&self,
|
||||
decl: &hir::FnDecl,
|
||||
expected_sigs: &ClosureSignatures<'tcx>,
|
||||
) -> InferResult<'tcx, ()> {
|
||||
// Get the signature S that the user gave.
|
||||
//
|
||||
// (See comment on `sig_of_closure_with_expectation` for the
|
||||
// meaning of these letters.)
|
||||
let supplied_sig = self.supplied_sig_of_closure(decl);
|
||||
|
||||
debug!("check_supplied_sig_against_expectation: supplied_sig={:?}",
|
||||
supplied_sig);
|
||||
debug!(
|
||||
"check_supplied_sig_against_expectation: supplied_sig={:?}",
|
||||
supplied_sig
|
||||
);
|
||||
|
||||
// The liberated version of this signature should be be a subtype
|
||||
// of the liberated form of the expectation.
|
||||
for ((hir_ty, &supplied_ty), expected_ty) in
|
||||
decl.inputs.iter()
|
||||
.zip(*supplied_sig.inputs().skip_binder()) // binder moved to (*) below
|
||||
.zip(expected_sigs.liberated_sig.inputs()) // `liberated_sig` is E'.
|
||||
{
|
||||
// Instantiate (this part of..) S to S', i.e., with fresh variables.
|
||||
let (supplied_ty, _) = self.infcx.replace_late_bound_regions_with_fresh_var(
|
||||
hir_ty.span,
|
||||
// FIXME(#45727): As discussed in [this comment][c1], naively
|
||||
// forcing equality here actually results in suboptimal error
|
||||
// messages in some cases. For now, if there would have been
|
||||
// an obvious error, we fallback to declaring the type of the
|
||||
// closure to be the one the user gave, which allows other
|
||||
// error message code to trigger.
|
||||
//
|
||||
// However, I think [there is potential to do even better
|
||||
// here][c2], since in *this* code we have the precise span of
|
||||
// the type parameter in question in hand when we report the
|
||||
// error.
|
||||
//
|
||||
// [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
|
||||
// [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
|
||||
self.infcx.commit_if_ok(|_| {
|
||||
let mut all_obligations = vec![];
|
||||
|
||||
// The liberated version of this signature should be be a subtype
|
||||
// of the liberated form of the expectation.
|
||||
for ((hir_ty, &supplied_ty), expected_ty) in decl.inputs.iter()
|
||||
.zip(*supplied_sig.inputs().skip_binder()) // binder moved to (*) below
|
||||
.zip(expected_sigs.liberated_sig.inputs())
|
||||
// `liberated_sig` is E'.
|
||||
{
|
||||
// Instantiate (this part of..) S to S', i.e., with fresh variables.
|
||||
let (supplied_ty, _) = self.infcx.replace_late_bound_regions_with_fresh_var(
|
||||
hir_ty.span,
|
||||
LateBoundRegionConversionTime::FnCall,
|
||||
&ty::Binder(supplied_ty),
|
||||
); // recreated from (*) above
|
||||
|
||||
// Check that E' = S'.
|
||||
let cause = &self.misc(hir_ty.span);
|
||||
let InferOk {
|
||||
value: (),
|
||||
obligations,
|
||||
} = self.at(cause, self.param_env)
|
||||
.eq(*expected_ty, supplied_ty)?;
|
||||
all_obligations.extend(obligations);
|
||||
}
|
||||
|
||||
let (supplied_output_ty, _) = self.infcx.replace_late_bound_regions_with_fresh_var(
|
||||
decl.output.span(),
|
||||
LateBoundRegionConversionTime::FnCall,
|
||||
&ty::Binder(supplied_ty)); // recreated from (*) above
|
||||
&supplied_sig.output(),
|
||||
);
|
||||
let cause = &self.misc(decl.output.span());
|
||||
let InferOk {
|
||||
value: (),
|
||||
obligations,
|
||||
} = self.at(cause, self.param_env)
|
||||
.eq(expected_sigs.liberated_sig.output(), supplied_output_ty)?;
|
||||
all_obligations.extend(obligations);
|
||||
|
||||
// Check that E' = S'.
|
||||
self.demand_eqtype(hir_ty.span, expected_ty, supplied_ty);
|
||||
}
|
||||
|
||||
let (supplied_output_ty, _) = self.infcx.replace_late_bound_regions_with_fresh_var(
|
||||
decl.output.span(),
|
||||
LateBoundRegionConversionTime::FnCall,
|
||||
&supplied_sig.output());
|
||||
self.demand_eqtype(decl.output.span(),
|
||||
expected_sigs.liberated_sig.output(),
|
||||
supplied_output_ty);
|
||||
Ok(InferOk {
|
||||
value: (),
|
||||
obligations: all_obligations,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// If there is no expected signature, then we will convert the
|
||||
/// types that the user gave into a signature.
|
||||
fn supplied_sig_of_closure(&self,
|
||||
decl: &hir::FnDecl)
|
||||
-> ty::PolyFnSig<'tcx>
|
||||
{
|
||||
fn supplied_sig_of_closure(&self, decl: &hir::FnDecl) -> ty::PolyFnSig<'tcx> {
|
||||
let astconv: &AstConv = self;
|
||||
|
||||
// First, convert the types that the user supplied (if any).
|
||||
let supplied_arguments =
|
||||
decl.inputs
|
||||
.iter()
|
||||
.map(|a| astconv.ast_ty_to_ty(a));
|
||||
let supplied_arguments = decl.inputs.iter().map(|a| astconv.ast_ty_to_ty(a));
|
||||
let supplied_return = match decl.output {
|
||||
hir::Return(ref output) => astconv.ast_ty_to_ty(&output),
|
||||
hir::DefaultReturn(_) => astconv.ty_infer(decl.output.span()),
|
||||
@ -457,25 +542,30 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
supplied_return,
|
||||
decl.variadic,
|
||||
hir::Unsafety::Normal,
|
||||
Abi::RustCall));
|
||||
Abi::RustCall,
|
||||
));
|
||||
|
||||
debug!("supplied_sig_of_closure: result={:?}", result);
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
fn closure_sigs(&self,
|
||||
expr_def_id: DefId,
|
||||
body: &hir::Body,
|
||||
bound_sig: ty::PolyFnSig<'tcx>)
|
||||
-> ClosureSignatures<'tcx>
|
||||
{
|
||||
fn closure_sigs(
|
||||
&self,
|
||||
expr_def_id: DefId,
|
||||
body: &hir::Body,
|
||||
bound_sig: ty::PolyFnSig<'tcx>,
|
||||
) -> ClosureSignatures<'tcx> {
|
||||
let liberated_sig = self.liberate_late_bound_regions(expr_def_id, &bound_sig);
|
||||
let liberated_sig = self.inh.normalize_associated_types_in(body.value.span,
|
||||
body.value.id,
|
||||
self.param_env,
|
||||
&liberated_sig);
|
||||
ClosureSignatures { bound_sig, liberated_sig }
|
||||
let liberated_sig = self.inh.normalize_associated_types_in(
|
||||
body.value.span,
|
||||
body.value.id,
|
||||
self.param_env,
|
||||
&liberated_sig,
|
||||
);
|
||||
ClosureSignatures {
|
||||
bound_sig,
|
||||
liberated_sig,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,14 +40,14 @@ fn expect_free_supply_bound() {
|
||||
// Here, we are given a function whose region is bound at closure level,
|
||||
// but we expect one bound in the argument. Error results.
|
||||
with_closure_expecting_fn_with_free_region(|x: fn(&u32), y| {});
|
||||
//~^ ERROR mismatched types
|
||||
//~^ ERROR type mismatch in closure arguments
|
||||
}
|
||||
|
||||
fn expect_bound_supply_free_from_fn<'x>(x: &'x u32) {
|
||||
// Here, we are given a `fn(&u32)` but we expect a `fn(&'x
|
||||
// u32)`. In principle, this could be ok, but we demand equality.
|
||||
with_closure_expecting_fn_with_bound_region(|x: fn(&'x u32), y| {});
|
||||
//~^ ERROR mismatched types
|
||||
//~^ ERROR type mismatch in closure arguments
|
||||
}
|
||||
|
||||
fn expect_bound_supply_free_from_closure() {
|
||||
@ -56,7 +56,7 @@ fn expect_bound_supply_free_from_closure() {
|
||||
// the argument level.
|
||||
type Foo<'a> = fn(&'a u32);
|
||||
with_closure_expecting_fn_with_bound_region(|_x: Foo<'_>, y| {});
|
||||
//~^ ERROR mismatched types
|
||||
//~^ ERROR type mismatch in closure arguments
|
||||
}
|
||||
|
||||
fn expect_bound_supply_bound<'x>(x: &'x u32) {
|
||||
|
@ -22,7 +22,7 @@ fn a() {
|
||||
fn b() {
|
||||
// Here we take the supplied types, resulting in an error later on.
|
||||
with_closure(|x: u32, y: i32| {
|
||||
//~^ ERROR mismatched types
|
||||
//~^ ERROR type mismatch in closure arguments
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -14,23 +14,6 @@ error[E0593]: closure is expected to take 2 arguments, but it takes 1 argument
|
||||
| |
|
||||
| expected closure that takes 2 arguments
|
||||
|
||||
error: non-reference pattern used to match a reference (see issue #42640)
|
||||
--> $DIR/closure-arg-count.rs:17:24
|
||||
|
|
||||
17 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
|
||||
| ^^^^^^^^^^^^^^^ help: consider using: `&(tuple, tuple2)`
|
||||
|
|
||||
= help: add #![feature(match_default_bindings)] to the crate attributes to enable
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/closure-arg-count.rs:17:24
|
||||
|
|
||||
17 | [1, 2, 3].sort_by(|(tuple, tuple2)| panic!());
|
||||
| ^^^^^^^^^^^^^^^ expected integral variable, found tuple
|
||||
|
|
||||
= note: expected type `{integer}`
|
||||
found type `(_, _)`
|
||||
|
||||
error[E0593]: closure is expected to take 2 arguments, but it takes 1 argument
|
||||
--> $DIR/closure-arg-count.rs:17:15
|
||||
|
|
||||
@ -73,5 +56,5 @@ error[E0593]: closure is expected to take a single 2-tuple as argument, but it t
|
||||
| |
|
||||
| expected closure that takes a single 2-tuple as argument
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
error: aborting due to 7 previous errors
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user