Extract coerce_closure_fn_ty function

This commit is contained in:
Santiago Pastorino 2017-11-30 12:22:11 -03:00 committed by Niko Matsakis
parent 900d4d5bda
commit ae035cb731
2 changed files with 22 additions and 17 deletions

View File

@ -1717,6 +1717,27 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}))
}
/// Create an unsafe fn ty based on a safe fn ty.
pub fn coerce_closure_fn_ty(self, sig: PolyFnSig<'tcx>) -> Ty<'tcx> {
let converted_sig = sig.map_bound(|s| {
let params_iter = match s.inputs()[0].sty {
ty::TyTuple(params, _) => {
params.into_iter().cloned()
}
_ => bug!(),
};
self.mk_fn_sig(
params_iter,
s.output(),
s.variadic,
hir::Unsafety::Normal,
abi::Abi::Rust
)
});
self.mk_fn_ptr(converted_sig)
}
// Interns a type/name combination, stores the resulting box in cx.interners,
// and returns the box as cast to an unsafe ptr (see comments for Ty above).
pub fn mk_ty(self, st: TypeVariants<'tcx>) -> Ty<'tcx> {

View File

@ -75,7 +75,6 @@ use rustc::ty::fold::TypeFoldable;
use rustc::ty::error::TypeError;
use rustc::ty::relate::RelateResult;
use errors::DiagnosticBuilder;
use syntax::abi;
use syntax::feature_gate;
use syntax::ptr::P;
use syntax_pos;
@ -670,22 +669,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
// to
// `fn(arg0,arg1,...) -> _`
let sig = self.closure_sig(def_id_a, substs_a);
let converted_sig = sig.map_bound(|s| {
let params_iter = match s.inputs()[0].sty {
ty::TyTuple(params, _) => {
params.into_iter().cloned()
}
_ => bug!(),
};
self.tcx.mk_fn_sig(
params_iter,
s.output(),
s.variadic,
hir::Unsafety::Normal,
abi::Abi::Rust
)
});
let pointer_ty = self.tcx.mk_fn_ptr(converted_sig);
let pointer_ty = self.tcx.coerce_closure_fn_ty(sig);
debug!("coerce_closure_to_fn(a={:?}, b={:?}, pty={:?})",
a, b, pointer_ty);
self.unify_and(pointer_ty, b, simple(Adjust::ClosureFnPointer))