diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2018-06-12 14:33:12 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2018-06-26 10:59:40 -0400 |
| commit | 3e32d42532c9499066c1f45f8a301c2a81e45ec8 (patch) | |
| tree | 78281744d6ba4c190222b4d7eb245b5d8177e5d2 | |
| parent | 2a0b3d6224768119f1d2e9850488c3d184362c1c (diff) | |
| download | rust-3e32d42532c9499066c1f45f8a301c2a81e45ec8.tar.gz rust-3e32d42532c9499066c1f45f8a301c2a81e45ec8.zip | |
transition to `Fallible`
| -rw-r--r-- | src/librustc/traits/query/mod.rs | 7 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/custom.rs | 11 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/eq.rs | 5 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/mod.rs | 26 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/normalize.rs | 21 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/outlives.rs | 8 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/prove_predicate.rs | 5 | ||||
| -rw-r--r-- | src/librustc/traits/query/type_op/subtype.rs | 5 | ||||
| -rw-r--r-- | src/librustc_mir/borrow_check/nll/type_check/mod.rs | 27 |
9 files changed, 69 insertions, 46 deletions
diff --git a/src/librustc/traits/query/mod.rs b/src/librustc/traits/query/mod.rs index dddd05db668..96034dd935c 100644 --- a/src/librustc/traits/query/mod.rs +++ b/src/librustc/traits/query/mod.rs @@ -16,6 +16,7 @@ //! `librustc_traits`. use infer::canonical::Canonical; +use ty::error::TypeError; use ty::{self, Ty}; pub mod dropck_outlives; @@ -49,4 +50,10 @@ pub struct NoSolution; pub type Fallible<T> = Result<T, NoSolution>; +impl<'tcx> From<TypeError<'tcx>> for NoSolution { + fn from(_: TypeError<'tcx>) -> NoSolution { + NoSolution + } +} + impl_stable_hash_for!(struct NoSolution { }); diff --git a/src/librustc/traits/query/type_op/custom.rs b/src/librustc/traits/query/type_op/custom.rs index 65b2ab31eba..d3bc81a09a9 100644 --- a/src/librustc/traits/query/type_op/custom.rs +++ b/src/librustc/traits/query/type_op/custom.rs @@ -8,7 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use infer::{InferCtxt, InferResult}; +use infer::{InferCtxt, InferOk}; +use traits::query::Fallible; use ty::TyCtxt; use std::fmt; @@ -20,7 +21,7 @@ pub struct CustomTypeOp<F, G> { impl<F, G> CustomTypeOp<F, G> { pub fn new<'gcx, 'tcx, R>(closure: F, description: G) -> Self where - F: FnOnce(&InferCtxt<'_, 'gcx, 'tcx>) -> InferResult<'tcx, R>, + F: FnOnce(&InferCtxt<'_, 'gcx, 'tcx>) -> Fallible<InferOk<'tcx, R>>, G: Fn() -> String, { CustomTypeOp { @@ -32,7 +33,7 @@ impl<F, G> CustomTypeOp<F, G> { impl<'gcx, 'tcx, F, R, G> super::TypeOp<'gcx, 'tcx> for CustomTypeOp<F, G> where - F: for<'a, 'cx> FnOnce(&'a InferCtxt<'cx, 'gcx, 'tcx>) -> InferResult<'tcx, R>, + F: for<'a, 'cx> FnOnce(&'a InferCtxt<'cx, 'gcx, 'tcx>) -> Fallible<InferOk<'tcx, R>>, G: Fn() -> String, { type Output = R; @@ -41,8 +42,8 @@ where Err(self) } - fn perform(self, infcx: &InferCtxt<'_, 'gcx, 'tcx>) -> InferResult<'tcx, R> { - (self.closure)(infcx) + fn perform(self, infcx: &InferCtxt<'_, 'gcx, 'tcx>) -> Fallible<InferOk<'tcx, R>> { + Ok((self.closure)(infcx)?) } } diff --git a/src/librustc/traits/query/type_op/eq.rs b/src/librustc/traits/query/type_op/eq.rs index 348283d1af3..cc5f7215184 100644 --- a/src/librustc/traits/query/type_op/eq.rs +++ b/src/librustc/traits/query/type_op/eq.rs @@ -9,6 +9,7 @@ // except according to those terms. use infer::canonical::{Canonical, CanonicalizedQueryResult, QueryResult}; +use traits::query::Fallible; use ty::{self, ParamEnv, Ty, TyCtxt}; #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] @@ -42,8 +43,8 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Eq<'tcx> { fn perform_query( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonical<'gcx, Eq<'gcx>>, - ) -> CanonicalizedQueryResult<'gcx, ()> { - tcx.type_op_eq(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, ()>> { + tcx.type_op_eq(canonicalized) } fn upcast_result( diff --git a/src/librustc/traits/query/type_op/mod.rs b/src/librustc/traits/query/type_op/mod.rs index cddb648f04f..4addb6c027b 100644 --- a/src/librustc/traits/query/type_op/mod.rs +++ b/src/librustc/traits/query/type_op/mod.rs @@ -12,12 +12,12 @@ use infer::canonical::query_result; use infer::canonical::{ Canonical, Canonicalized, CanonicalizedQueryResult, QueryRegionConstraint, QueryResult, }; -use infer::{InferCtxt, InferOk, InferResult}; +use infer::{InferCtxt, InferOk}; use std::fmt; use std::rc::Rc; use syntax::codemap::DUMMY_SP; +use traits::query::Fallible; use traits::{ObligationCause, TraitEngine}; -use ty::error::TypeError; use ty::fold::TypeFoldable; use ty::{Lift, ParamEnv, TyCtxt}; @@ -42,7 +42,10 @@ pub trait TypeOp<'gcx, 'tcx>: Sized + fmt::Debug { /// should use `fully_perform`, which will take those resulting /// obligations and prove them, and then process the combined /// results into region obligations which are returned. - fn perform(self, infcx: &InferCtxt<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output>; + fn perform( + self, + infcx: &InferCtxt<'_, 'gcx, 'tcx>, + ) -> Fallible<InferOk<'tcx, Self::Output>>; /// Processes the operation and all resulting obligations, /// returning the final result along with any region constraints @@ -50,7 +53,7 @@ pub trait TypeOp<'gcx, 'tcx>: Sized + fmt::Debug { fn fully_perform( self, infcx: &InferCtxt<'_, 'gcx, 'tcx>, - ) -> Result<(Self::Output, Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>), TypeError<'tcx>> { + ) -> Fallible<(Self::Output, Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>)> { match self.trivial_noop(infcx.tcx) { Ok(r) => Ok((r, None)), Err(op) => op.fully_perform_nontrivial(infcx), @@ -62,7 +65,7 @@ pub trait TypeOp<'gcx, 'tcx>: Sized + fmt::Debug { fn fully_perform_nontrivial( self, infcx: &InferCtxt<'_, 'gcx, 'tcx>, - ) -> Result<(Self::Output, Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>), TypeError<'tcx>> { + ) -> Fallible<(Self::Output, Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>)> { if cfg!(debug_assertions) { info!( "fully_perform_op_and_get_region_constraint_data({:?})", @@ -112,7 +115,7 @@ pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>: TypeFoldable<'tcx> + Lift<'gcx> { fn perform_query( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Self>, - ) -> CanonicalizedQueryResult<'gcx, Self::QueryResult>; + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self::QueryResult>>; /// "Upcasts" a lifted query result (which is in the gcx lifetime) /// into the tcx lifetime. This is always just an identity cast, @@ -136,7 +139,10 @@ where QueryTypeOp::trivial_noop(self, tcx) } - fn perform(self, infcx: &InferCtxt<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output> { + fn perform( + self, + infcx: &InferCtxt<'_, 'gcx, 'tcx>, + ) -> Fallible<InferOk<'tcx, Self::Output>> { let param_env = self.param_env(); // FIXME(#33684) -- We need to use @@ -144,7 +150,7 @@ where // the subtype query, which go awry around `'static` // otherwise. let (canonical_self, canonical_var_values) = infcx.canonicalize_hr_query_hack(&self); - let canonical_result = Q::perform_query(infcx.tcx, canonical_self); + let canonical_result = Q::perform_query(infcx.tcx, canonical_self)?; // FIXME: This is not the most efficient setup. The // `instantiate_query_result_and_region_obligations` basically @@ -155,11 +161,11 @@ where // `QueryRegionConstraint` and ultimately into NLL // constraints. We should cut out the middleman but that will // take a bit of refactoring. - infcx.instantiate_query_result_and_region_obligations( + Ok(infcx.instantiate_query_result_and_region_obligations( &ObligationCause::dummy(), param_env, &canonical_var_values, Q::upcast_result(&canonical_result), - ) + )?) } } diff --git a/src/librustc/traits/query/type_op/normalize.rs b/src/librustc/traits/query/type_op/normalize.rs index d63997fe40c..80bcc20f842 100644 --- a/src/librustc/traits/query/type_op/normalize.rs +++ b/src/librustc/traits/query/type_op/normalize.rs @@ -10,6 +10,7 @@ use infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResult, QueryResult}; use std::fmt; +use traits::query::Fallible; use ty::fold::TypeFoldable; use ty::{self, Lift, ParamEnv, Ty, TyCtxt}; @@ -49,7 +50,7 @@ where fn perform_query( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Self>, - ) -> CanonicalizedQueryResult<'gcx, Self::QueryResult> { + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self::QueryResult>> { T::type_op_method(tcx, canonicalized) } @@ -64,7 +65,7 @@ pub trait Normalizable<'gcx, 'tcx>: fmt::Debug + TypeFoldable<'tcx> + Lift<'gcx> fn type_op_method( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>, - ) -> CanonicalizedQueryResult<'gcx, Self>; + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self>>; /// Convert from the `'gcx` (lifted) form of `Self` into the `tcx` /// form of `Self`. @@ -80,8 +81,8 @@ where fn type_op_method( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>, - ) -> CanonicalizedQueryResult<'gcx, Self> { - tcx.type_op_normalize_ty(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> { + tcx.type_op_normalize_ty(canonicalized) } fn upcast_result( @@ -98,8 +99,8 @@ where fn type_op_method( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>, - ) -> CanonicalizedQueryResult<'gcx, Self> { - tcx.type_op_normalize_predicate(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> { + tcx.type_op_normalize_predicate(canonicalized) } fn upcast_result( @@ -116,8 +117,8 @@ where fn type_op_method( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>, - ) -> CanonicalizedQueryResult<'gcx, Self> { - tcx.type_op_normalize_poly_fn_sig(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> { + tcx.type_op_normalize_poly_fn_sig(canonicalized) } fn upcast_result( @@ -134,8 +135,8 @@ where fn type_op_method( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>, - ) -> CanonicalizedQueryResult<'gcx, Self> { - tcx.type_op_normalize_fn_sig(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> { + tcx.type_op_normalize_fn_sig(canonicalized) } fn upcast_result( diff --git a/src/librustc/traits/query/type_op/outlives.rs b/src/librustc/traits/query/type_op/outlives.rs index f6c60bcaf38..419146d82ec 100644 --- a/src/librustc/traits/query/type_op/outlives.rs +++ b/src/librustc/traits/query/type_op/outlives.rs @@ -8,8 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use infer::{InferCtxt, InferResult}; +use infer::{InferCtxt, InferOk}; use traits::query::dropck_outlives::trivial_dropck_outlives; +use traits::query::Fallible; use traits::ObligationCause; use ty::subst::Kind; use ty::{ParamEnv, Ty, TyCtxt}; @@ -40,7 +41,10 @@ impl<'gcx, 'tcx> super::TypeOp<'gcx, 'tcx> for DropckOutlives<'tcx> { } } - fn perform(self, infcx: &InferCtxt<'_, 'gcx, 'tcx>) -> InferResult<'tcx, Self::Output> { + fn perform( + self, + infcx: &InferCtxt<'_, 'gcx, 'tcx>, + ) -> Fallible<InferOk<'tcx, Vec<Kind<'tcx>>>> { Ok(infcx .at(&ObligationCause::dummy(), self.param_env) .dropck_outlives(self.dropped_ty)) diff --git a/src/librustc/traits/query/type_op/prove_predicate.rs b/src/librustc/traits/query/type_op/prove_predicate.rs index 866ebd0cc12..d2714803eae 100644 --- a/src/librustc/traits/query/type_op/prove_predicate.rs +++ b/src/librustc/traits/query/type_op/prove_predicate.rs @@ -9,6 +9,7 @@ // except according to those terms. use infer::canonical::{Canonical, CanonicalizedQueryResult, QueryResult}; +use traits::query::Fallible; use ty::{ParamEnv, Predicate, TyCtxt}; #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] @@ -40,8 +41,8 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for ProvePredicate<'tcx> { fn perform_query( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonical<'gcx, ProvePredicate<'gcx>>, - ) -> CanonicalizedQueryResult<'gcx, ()> { - tcx.type_op_prove_predicate(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, ()>> { + tcx.type_op_prove_predicate(canonicalized) } fn upcast_result( diff --git a/src/librustc/traits/query/type_op/subtype.rs b/src/librustc/traits/query/type_op/subtype.rs index a0fb2c2763d..7bb1eda4eec 100644 --- a/src/librustc/traits/query/type_op/subtype.rs +++ b/src/librustc/traits/query/type_op/subtype.rs @@ -9,6 +9,7 @@ // except according to those terms. use infer::canonical::{Canonical, CanonicalizedQueryResult, QueryResult}; +use traits::query::Fallible; use ty::{ParamEnv, Ty, TyCtxt}; #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] @@ -46,8 +47,8 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Subtype<'tcx> { fn perform_query( tcx: TyCtxt<'_, 'gcx, 'tcx>, canonicalized: Canonical<'gcx, Subtype<'gcx>>, - ) -> CanonicalizedQueryResult<'gcx, ()> { - tcx.type_op_subtype(canonicalized).unwrap() + ) -> Fallible<CanonicalizedQueryResult<'gcx, ()>> { + tcx.type_op_subtype(canonicalized) } fn upcast_result( 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 d8818b704bb..7a8337b2498 100644 --- a/src/librustc_mir/borrow_check/nll/type_check/mod.rs +++ b/src/librustc_mir/borrow_check/nll/type_check/mod.rs @@ -22,14 +22,14 @@ use dataflow::MaybeInitializedPlaces; use rustc::hir::def_id::DefId; use rustc::infer::canonical::QueryRegionConstraint; use rustc::infer::region_constraints::GenericKind; -use rustc::infer::{InferCtxt, LateBoundRegionConversionTime, UnitResult}; +use rustc::infer::{InferCtxt, LateBoundRegionConversionTime}; use rustc::mir::interpret::EvalErrorKind::BoundsCheck; use rustc::mir::tcx::PlaceTy; use rustc::mir::visit::{PlaceContext, Visitor}; use rustc::mir::*; use rustc::traits::query::type_op; +use rustc::traits::query::{Fallible, NoSolution}; use rustc::traits::ObligationCause; -use rustc::ty::error::TypeError; use rustc::ty::fold::TypeFoldable; use rustc::ty::{self, ToPolyTraitRef, Ty, TyCtxt, TypeVariants}; use std::fmt; @@ -733,7 +733,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { &mut self, locations: Locations, op: impl type_op::TypeOp<'gcx, 'tcx, Output = R>, - ) -> Result<R, TypeError<'tcx>> { + ) -> Fallible<R> { let (r, opt_data) = op.fully_perform(self.infcx)?; if let Some(data) = &opt_data { @@ -775,7 +775,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { sub: Ty<'tcx>, sup: Ty<'tcx>, locations: Locations, - ) -> UnitResult<'tcx> { + ) -> Fallible<()> { let param_env = self.param_env; self.fully_perform_op( locations, @@ -783,7 +783,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { ) } - fn eq_types(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, locations: Locations) -> UnitResult<'tcx> { + fn eq_types(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, locations: Locations) -> Fallible<()> { let param_env = self.param_env; self.fully_perform_op(locations, type_op::eq::Eq::new(param_env, b, a)) } @@ -1561,11 +1561,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { predicate, location, ); - let param_env = self.param_env; - self.fully_perform_op( - location.at_self(), - type_op::prove_predicate::ProvePredicate::new(param_env, predicate), - ).unwrap() + self.prove_predicate(predicate, location); } } @@ -1579,7 +1575,9 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { self.fully_perform_op( location.at_self(), type_op::prove_predicate::ProvePredicate::new(param_env, predicate), - ).unwrap() + ).unwrap_or_else(|NoSolution| { + span_mirbug!(self, NoSolution, "could not prove {:?}", predicate); + }) } fn typeck_mir(&mut self, mir: &Mir<'tcx>) { @@ -1610,14 +1608,17 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> { fn normalize<T>(&mut self, value: T, location: impl ToLocations) -> T where - T: type_op::normalize::Normalizable<'gcx, 'tcx>, + T: type_op::normalize::Normalizable<'gcx, 'tcx> + Copy, { debug!("normalize(value={:?}, location={:?})", value, location); let param_env = self.param_env; self.fully_perform_op( location.to_locations(), type_op::normalize::Normalize::new(param_env, value), - ).unwrap() + ).unwrap_or_else(|NoSolution| { + span_mirbug!(self, NoSolution, "failed to normalize `{:?}`", value); + value + }) } } |
