introduce Subtype type_op

This commit is contained in:
Niko Matsakis 2018-06-06 09:29:14 -04:00
parent 909b10c33b
commit 2b52793f74
2 changed files with 31 additions and 9 deletions

View File

@ -826,11 +826,7 @@ fn sub_types(
self.fully_perform_op(
locations,
|| format!("sub_types({:?} <: {:?})", sub, sup),
CustomTypeOp::new(|this| {
this.infcx
.at(&ObligationCause::dummy(), this.param_env)
.sup(sup, sub)
}),
type_op::Subtype::new(sub, sup),
)
}

View File

@ -10,19 +10,23 @@
use borrow_check::nll::type_check::TypeChecker;
use rustc::infer::InferResult;
use rustc::traits::ObligationCause;
use rustc::ty::Ty;
pub(super) trait TypeOp<'gcx, 'tcx> {
type Output;
fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output>;
fn perform(
self,
type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>,
) -> InferResult<'tcx, Self::Output>;
}
pub(super) struct CustomTypeOp<F> {
closure: F
closure: F,
}
impl<F> CustomTypeOp<F>
{
impl<F> CustomTypeOp<F> {
pub(super) fn new<'gcx, 'tcx, R>(closure: F) -> Self
where
F: FnOnce(&mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, R>,
@ -41,3 +45,25 @@ fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<
(self.closure)(type_checker)
}
}
pub(super) struct Subtype<'tcx> {
sub: Ty<'tcx>,
sup: Ty<'tcx>,
}
impl<'tcx> Subtype<'tcx> {
pub(super) fn new(sub: Ty<'tcx>, sup: Ty<'tcx>) -> Self {
Self { sub, sup }
}
}
impl<'gcx, 'tcx> TypeOp<'gcx, 'tcx> for Subtype<'tcx> {
type Output = ();
fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output> {
type_checker.infcx
.at(&ObligationCause::dummy(), type_checker.param_env)
.sup(self.sup, self.sub)
}
}