diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2018-06-06 09:09:28 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2018-06-26 10:31:49 -0400 |
| commit | 909b10c33ba51ac704fa909395c58de3e4aca71f (patch) | |
| tree | e5378c62075b211be4eb2f905d65188ded5d2732 | |
| parent | 764232cb2a8407c72b9fea68835e686240e30ef3 (diff) | |
| download | rust-909b10c33ba51ac704fa909395c58de3e4aca71f.tar.gz rust-909b10c33ba51ac704fa909395c58de3e4aca71f.zip | |
introduce `type_op`
4 files changed, 65 insertions, 18 deletions
diff --git a/src/librustc_mir/borrow_check/nll/type_check/input_output.rs b/src/librustc_mir/borrow_check/nll/type_check/input_output.rs index d44eed65201..be0a2494b04 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/input_output.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/input_output.rs @@ -18,6 +18,7 @@ //! contain revealed `impl Trait` values). use borrow_check::nll::renumber; +use borrow_check::nll::type_check::type_op::CustomTypeOp; use borrow_check::nll::universal_regions::UniversalRegions; use rustc::hir::def_id::DefId; use rustc::infer::InferOk; @@ -80,7 +81,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( Locations::All, || format!("input_output"), - |cx| { + CustomTypeOp::new(|cx| { let mut obligations = ObligationAccumulator::default(); let dummy_body_id = ObligationCause::dummy().body_id; @@ -135,7 +136,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { value: Some(anon_type_map), obligations: obligations.into_vec(), }) - }, + }), ).unwrap_or_else(|terr| { span_mirbug!( self, @@ -156,13 +157,13 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( Locations::All, || format!("anon_type_map"), - |_cx| { + CustomTypeOp::new(|_cx| { infcx.constrain_anon_types(&anon_type_map, universal_regions); Ok(InferOk { value: (), obligations: vec![], }) - }, + }), ).unwrap(); } } diff --git a/src/librustc_mir/borrow_check/nll/type_check/liveness.rs b/src/librustc_mir/borrow_check/nll/type_check/liveness.rs index 80f5fe4184f..d58dc1a601a 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/liveness.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/liveness.rs @@ -10,6 +10,7 @@ use borrow_check::nll::region_infer::Cause; use borrow_check::nll::type_check::AtLocation; +use borrow_check::nll::type_check::type_op::CustomTypeOp; use dataflow::move_paths::{HasMoveData, MoveData}; use dataflow::MaybeInitializedPlaces; use dataflow::{FlowAtLocation, FlowsAtLocation}; @@ -220,12 +221,12 @@ impl<'gen, 'typeck, 'flow, 'gcx, 'tcx> TypeLivenessGenerator<'gen, 'typeck, 'flo let (dropped_kinds, region_constraint_data) = cx.fully_perform_op_and_get_region_constraint_data( || format!("compute_drop_data(dropped_ty={:?})", dropped_ty), - |cx| { + CustomTypeOp::new(|cx| { Ok(cx .infcx .at(&ObligationCause::dummy(), cx.param_env) .dropck_outlives(dropped_ty)) - }, + }), ).unwrap(); DropData { diff --git a/src/librustc_mir/borrow_check/nll/type_check/mod.rs b/src/librustc_mir/borrow_check/nll/type_check/mod.rs index d25cec79791..5d06513e2a8 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -15,13 +15,14 @@ use borrow_check::location::LocationTable; use borrow_check::nll::facts::AllFacts; use borrow_check::nll::region_infer::Cause; use borrow_check::nll::region_infer::{ClosureRegionRequirementsExt, OutlivesConstraint, TypeTest}; +use borrow_check::nll::type_check::type_op::{CustomTypeOp, TypeOp}; use borrow_check::nll::universal_regions::UniversalRegions; use dataflow::move_paths::MoveData; use dataflow::FlowAtLocation; use dataflow::MaybeInitializedPlaces; use rustc::hir::def_id::DefId; use rustc::infer::region_constraints::{GenericKind, RegionConstraintData}; -use rustc::infer::{InferCtxt, InferOk, InferResult, LateBoundRegionConversionTime, UnitResult}; +use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, UnitResult}; use rustc::mir::interpret::EvalErrorKind::BoundsCheck; use rustc::mir::tcx::PlaceTy; use rustc::mir::visit::{PlaceContext, Visitor}; @@ -67,6 +68,7 @@ macro_rules! span_mirbug_and_err { mod constraint_conversion; mod input_output; mod liveness; +mod type_op; /// Type checks the given `mir` in the context of the inference /// context `infcx`. Returns any region constraints that have yet to @@ -732,7 +734,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { &mut self, locations: Locations, describe_op: impl Fn() -> String, - op: impl FnOnce(&mut Self) -> InferResult<'tcx, R>, + op: impl TypeOp<'gcx, 'tcx, Output = R>, ) -> Result<R, TypeError<'tcx>> { let (r, opt_data) = self.fully_perform_op_and_get_region_constraint_data( || format!("{} at {:?}", describe_op(), locations), @@ -777,7 +779,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { fn fully_perform_op_and_get_region_constraint_data<R>( &mut self, describe_op: impl Fn() -> String, - op: impl FnOnce(&mut Self) -> InferResult<'tcx, R>, + op: impl TypeOp<'gcx, 'tcx, Output = R>, ) -> Result<(R, Option<Rc<RegionConstraintData<'tcx>>>), TypeError<'tcx>> { if cfg!(debug_assertions) { info!( @@ -788,7 +790,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { let mut fulfill_cx = TraitEngine::new(self.infcx.tcx); let dummy_body_id = ObligationCause::dummy().body_id; - let InferOk { value, obligations } = self.infcx.commit_if_ok(|_| op(self))?; + let InferOk { value, obligations } = self.infcx.commit_if_ok(|_| op.perform(self))?; debug_assert!(obligations.iter().all(|o| o.cause.body_id == dummy_body_id)); fulfill_cx.register_predicate_obligations(self.infcx, obligations); if let Err(e) = fulfill_cx.select_all_or_error(self.infcx) { @@ -824,11 +826,11 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( locations, || format!("sub_types({:?} <: {:?})", sub, sup), - |this| { + CustomTypeOp::new(|this| { this.infcx .at(&ObligationCause::dummy(), this.param_env) .sup(sup, sub) - }, + }), ) } @@ -841,11 +843,11 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( locations, || format!("eq_types({:?} = {:?})", a, b), - |this| { + CustomTypeOp::new(|this| { this.infcx .at(&ObligationCause::dummy(), this.param_env) .eq(b, a) - }, + }), ) } @@ -1635,12 +1637,12 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( location.at_self(), || format!("prove_predicates({:?})", predicates_vec), - |_this| { + CustomTypeOp::new(|_this| { Ok(InferOk { value: (), obligations, }) - }, + }), ).unwrap() } @@ -1683,7 +1685,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( location.to_locations(), || format!("normalize(value={:?})", value), - |this| { + CustomTypeOp::new(|this| { let Normalized { value, obligations } = this .infcx .at(&ObligationCause::dummy(), this.param_env) @@ -1697,7 +1699,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { ); }); Ok(InferOk { value, obligations }) - }, + }), ).unwrap() } } diff --git a/src/librustc_mir/borrow_check/nll/type_check/type_op.rs b/src/librustc_mir/borrow_check/nll/type_check/type_op.rs new file mode 100644 index 00000000000..b4c71344cfc --- /dev/null +++ b/src/librustc_mir/borrow_check/nll/type_check/type_op.rs @@ -0,0 +1,43 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use borrow_check::nll::type_check::TypeChecker; +use rustc::infer::InferResult; + +pub(super) trait TypeOp<'gcx, 'tcx> { + type Output; + + fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output>; +} + +pub(super) struct CustomTypeOp<F> { + closure: 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>, + { + CustomTypeOp { closure } + } +} + +impl<'gcx, 'tcx, F, R> TypeOp<'gcx, 'tcx> for CustomTypeOp<F> +where + F: FnOnce(&mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, R>, +{ + type Output = R; + + fn perform(self, type_checker: &mut TypeChecker<'_, 'gcx, 'tcx>) -> InferResult<'tcx, R> { + (self.closure)(type_checker) + } +} |
