From 09bba9b89d05d3441d8c1acf092e7037e6e11295 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 3 Jun 2019 18:25:21 -0400 Subject: [PATCH] introduce `QueryRegionConstraints` struct --- src/librustc/infer/canonical/mod.rs | 10 +++++++++- src/librustc/infer/canonical/query_response.rs | 14 ++++++-------- src/librustc/traits/query/type_op/custom.rs | 12 ++++++------ src/librustc/traits/query/type_op/mod.rs | 14 +++++++------- .../nll/type_check/free_region_relations.rs | 7 ++++--- .../borrow_check/nll/type_check/liveness/trace.rs | 4 ++-- .../borrow_check/nll/type_check/mod.rs | 14 +++++++++----- 7 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/librustc/infer/canonical/mod.rs b/src/librustc/infer/canonical/mod.rs index 9c616edba9f..f18eeca3610 100644 --- a/src/librustc/infer/canonical/mod.rs +++ b/src/librustc/infer/canonical/mod.rs @@ -196,7 +196,15 @@ pub struct QueryResponse<'tcx, R> { #[derive(Clone, Debug, Default, HashStable)] pub struct QueryRegionConstraints<'tcx> { - outlives: Vec>, + pub outlives: Vec>, +} + +impl QueryRegionConstraints<'_> { + /// Represents an empty (trivially true) set of region + /// constraints. + pub fn is_empty(&self) -> bool { + self.outlives.is_empty() + } } pub type Canonicalized<'tcx, V> = Canonical<'tcx, V>; diff --git a/src/librustc/infer/canonical/query_response.rs b/src/librustc/infer/canonical/query_response.rs index acaed189397..b4c46e71b16 100644 --- a/src/librustc/infer/canonical/query_response.rs +++ b/src/librustc/infer/canonical/query_response.rs @@ -173,8 +173,8 @@ fn make_query_response( debug!("ambig_errors = {:#?}", ambig_errors); let region_obligations = self.take_registered_region_obligations(); - let outlives_constraints = self.with_region_constraints(|region_constraints| { - make_query_outlives( + let region_constraints = self.with_region_constraints(|region_constraints| { + make_query_region_constraints( tcx, region_obligations .iter() @@ -191,9 +191,7 @@ fn make_query_response( Ok(QueryResponse { var_values: inference_vars, - region_constraints: QueryRegionConstraints { - outlives: outlives_constraints, - }, + region_constraints, certainty, value: answer, }) @@ -647,11 +645,11 @@ fn unify_canonical_vars( /// Given the region obligations and constraints scraped from the infcx, /// creates query region constraints. -pub fn make_query_outlives<'tcx>( +pub fn make_query_region_constraints<'tcx>( tcx: TyCtxt<'tcx>, outlives_obligations: impl Iterator, ty::Region<'tcx>)>, region_constraints: &RegionConstraintData<'tcx>, -) -> Vec> { +) -> QueryRegionConstraints<'tcx> { let RegionConstraintData { constraints, verifys, @@ -690,5 +688,5 @@ pub fn make_query_outlives<'tcx>( ) .collect(); - outlives + QueryRegionConstraints { outlives } } diff --git a/src/librustc/traits/query/type_op/custom.rs b/src/librustc/traits/query/type_op/custom.rs index 42d0608d358..a2a5f3f950c 100644 --- a/src/librustc/traits/query/type_op/custom.rs +++ b/src/librustc/traits/query/type_op/custom.rs @@ -3,7 +3,7 @@ use crate::traits::query::Fallible; use crate::infer::canonical::query_response; -use crate::infer::canonical::QueryOutlivesConstraint; +use crate::infer::canonical::QueryRegionConstraints; use std::rc::Rc; use syntax::source_map::DUMMY_SP; use crate::traits::{ObligationCause, TraitEngine, TraitEngineExt}; @@ -39,7 +39,7 @@ impl<'tcx, F, R, G> super::TypeOp<'tcx> for CustomTypeOp fn fully_perform( self, infcx: &InferCtxt<'_, 'tcx>, - ) -> Fallible<(Self::Output, Option>>>)> { + ) -> Fallible<(Self::Output, Option>>)> { if cfg!(debug_assertions) { info!("fully_perform({:?})", self); } @@ -62,7 +62,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn scrape_region_constraints<'tcx, R>( infcx: &InferCtxt<'_, 'tcx>, op: impl FnOnce() -> Fallible>, -) -> Fallible<(R, Option>>>)> { +) -> Fallible<(R, Option>>)> { let mut fulfill_cx = TraitEngine::new(infcx.tcx); let dummy_body_id = ObligationCause::dummy().body_id; @@ -92,7 +92,7 @@ fn scrape_region_constraints<'tcx, R>( let region_constraint_data = infcx.take_and_reset_region_constraints(); - let outlives = query_response::make_query_outlives( + let region_constraints = query_response::make_query_region_constraints( infcx.tcx, region_obligations .iter() @@ -101,9 +101,9 @@ fn scrape_region_constraints<'tcx, R>( ®ion_constraint_data, ); - if outlives.is_empty() { + if region_constraints.is_empty() { Ok((value, None)) } else { - Ok((value, Some(Rc::new(outlives)))) + Ok((value, Some(Rc::new(region_constraints)))) } } diff --git a/src/librustc/traits/query/type_op/mod.rs b/src/librustc/traits/query/type_op/mod.rs index 0c415876247..bf8cace3a1b 100644 --- a/src/librustc/traits/query/type_op/mod.rs +++ b/src/librustc/traits/query/type_op/mod.rs @@ -1,6 +1,6 @@ use crate::infer::canonical::{ Canonical, Canonicalized, CanonicalizedQueryResponse, OriginalQueryValues, - QueryOutlivesConstraint, QueryResponse, + QueryRegionConstraints, QueryOutlivesConstraint, QueryResponse, }; use crate::infer::{InferCtxt, InferOk}; use std::fmt; @@ -32,7 +32,7 @@ pub trait TypeOp<'tcx>: Sized + fmt::Debug { fn fully_perform( self, infcx: &InferCtxt<'_, 'tcx>, - ) -> Fallible<(Self::Output, Option>>>)>; + ) -> Fallible<(Self::Output, Option>>)>; } /// "Query type ops" are type ops that are implemented using a @@ -140,16 +140,16 @@ impl<'tcx, Q> TypeOp<'tcx> for ParamEnvAnd<'tcx, Q> fn fully_perform( self, infcx: &InferCtxt<'_, 'tcx>, - ) -> Fallible<(Self::Output, Option>>>)> { - let mut qrc = vec![]; - let r = Q::fully_perform_into(self, infcx, &mut qrc)?; + ) -> Fallible<(Self::Output, Option>>)> { + let mut outlives = vec![]; + let r = Q::fully_perform_into(self, infcx, &mut outlives)?; // Promote the final query-region-constraints into a // (optional) ref-counted vector: - let opt_qrc = if qrc.is_empty() { + let opt_qrc = if outlives.is_empty() { None } else { - Some(Rc::new(qrc)) + Some(Rc::new(QueryRegionConstraints { outlives })) }; Ok((r, opt_qrc)) diff --git a/src/librustc_mir/borrow_check/nll/type_check/free_region_relations.rs b/src/librustc_mir/borrow_check/nll/type_check/free_region_relations.rs index 09ea8dfd61c..2a21da064fa 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/free_region_relations.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/free_region_relations.rs @@ -2,7 +2,7 @@ use crate::borrow_check::nll::type_check::{Locations, MirTypeckRegionConstraints}; use crate::borrow_check::nll::universal_regions::UniversalRegions; use crate::borrow_check::nll::ToRegionVid; -use rustc::infer::canonical::QueryOutlivesConstraint; +use rustc::infer::canonical::QueryRegionConstraints; use rustc::infer::outlives::free_region_map::FreeRegionRelations; use rustc::infer::region_constraints::GenericKind; use rustc::infer::InferCtxt; @@ -288,6 +288,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> { } for data in constraint_sets { + let QueryRegionConstraints { outlives } = &*data; constraint_conversion::ConstraintConversion::new( self.infcx, &self.universal_regions, @@ -297,7 +298,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> { Locations::All(DUMMY_SP), ConstraintCategory::Internal, &mut self.constraints, - ).convert_all(&data); + ).convert_all(outlives); } CreateResult { @@ -311,7 +312,7 @@ impl UniversalRegionRelationsBuilder<'cx, 'tcx> { /// either the return type of the MIR or one of its arguments. At /// the same time, compute and add any implied bounds that come /// from this local. - fn add_implied_bounds(&mut self, ty: Ty<'tcx>) -> Option>>> { + fn add_implied_bounds(&mut self, ty: Ty<'tcx>) -> Option>> { debug!("add_implied_bounds(ty={:?})", ty); let (bounds, constraints) = self.param_env diff --git a/src/librustc_mir/borrow_check/nll/type_check/liveness/trace.rs b/src/librustc_mir/borrow_check/nll/type_check/liveness/trace.rs index 70441cd258e..f160f658f55 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/liveness/trace.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/liveness/trace.rs @@ -6,7 +6,7 @@ use crate::dataflow::indexes::MovePathIndex; use crate::dataflow::move_paths::MoveData; use crate::dataflow::{FlowAtLocation, FlowsAtLocation, MaybeInitializedPlaces}; -use rustc::infer::canonical::QueryOutlivesConstraint; +use rustc::infer::canonical::QueryRegionConstraints; use rustc::mir::{BasicBlock, ConstraintCategory, Local, Location, Body}; use rustc::traits::query::dropck_outlives::DropckOutlivesResult; use rustc::traits::query::type_op::outlives::DropckOutlives; @@ -88,7 +88,7 @@ struct LivenessContext<'me, 'typeck, 'flow, 'tcx> { struct DropData<'tcx> { dropck_result: DropckOutlivesResult<'tcx>, - region_constraint_data: Option>>>, + region_constraint_data: Option>>, } struct LivenessResults<'me, 'typeck, 'flow, 'tcx> { 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 134b51c4b79..758c8df1d18 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -23,7 +23,7 @@ use either::Either; use rustc::hir; use rustc::hir::def_id::DefId; -use rustc::infer::canonical::QueryOutlivesConstraint; +use rustc::infer::canonical::QueryRegionConstraints; use rustc::infer::outlives::env::RegionBoundPairs; use rustc::infer::{InferCtxt, InferOk, LateBoundRegionConversionTime, NLLRegionVariableOrigin}; use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; @@ -1093,13 +1093,15 @@ fn push_region_constraints( &mut self, locations: Locations, category: ConstraintCategory, - data: &[QueryOutlivesConstraint<'tcx>], + data: &QueryRegionConstraints<'tcx>, ) { debug!( "push_region_constraints: constraints generated at {:?} are {:#?}", locations, data ); + let QueryRegionConstraints { outlives } = data; + constraint_conversion::ConstraintConversion::new( self.infcx, self.borrowck_context.universal_regions, @@ -1109,7 +1111,7 @@ fn push_region_constraints( locations, category, &mut self.borrowck_context.constraints, - ).convert_all(&data); + ).convert_all(outlives); } /// Convenient wrapper around `relate_tys::relate_types` -- see @@ -2508,10 +2510,12 @@ fn prove_closure_bounds( location: Location, ) -> ty::InstantiatedPredicates<'tcx> { if let Some(closure_region_requirements) = tcx.mir_borrowck(def_id).closure_requirements { - let closure_constraints = - closure_region_requirements.apply_requirements(tcx, def_id, substs); + let closure_constraints = QueryRegionConstraints { + outlives: closure_region_requirements.apply_requirements(tcx, def_id, substs) + }; let bounds_mapping = closure_constraints + .outlives .iter() .enumerate() .filter_map(|(idx, constraint)| {