diff options
Diffstat (limited to 'src/librustc/traits/mod.rs')
| -rw-r--r-- | src/librustc/traits/mod.rs | 186 |
1 files changed, 107 insertions, 79 deletions
diff --git a/src/librustc/traits/mod.rs b/src/librustc/traits/mod.rs index a1817f18106..d6f8a5f9cc6 100644 --- a/src/librustc/traits/mod.rs +++ b/src/librustc/traits/mod.rs @@ -17,9 +17,9 @@ pub use self::ObligationCauseCode::*; use hir; use hir::def_id::DefId; +use infer::outlives::env::OutlivesEnvironment; use middle::const_val::ConstEvalErr; use middle::region; -use middle::free_region::FreeRegionMap; use ty::subst::Substs; use ty::{self, AdtKind, Ty, TyCtxt, TypeFoldable, ToPredicate}; use ty::error::{ExpectedFound, TypeError}; @@ -30,7 +30,7 @@ use syntax::ast; use syntax_pos::{Span, DUMMY_SP}; pub use self::coherence::{orphan_check, overlapping_impls, OrphanCheckErr, OverlapResult}; -pub use self::fulfill::{FulfillmentContext, RegionObligation}; +pub use self::fulfill::FulfillmentContext; pub use self::project::MismatchedProjectionTypes; pub use self::project::{normalize, normalize_projection_type, Normalized}; pub use self::project::{ProjectionCache, ProjectionCacheSnapshot, Reveal}; @@ -152,7 +152,6 @@ pub enum ObligationCauseCode<'tcx> { item_name: ast::Name, impl_item_def_id: DefId, trait_item_def_id: DefId, - lint_id: Option<ast::NodeId>, }, /// Checking that this expression can be assigned where it needs to be @@ -283,16 +282,16 @@ pub type SelectionResult<'tcx, T> = Result<Option<T>, SelectionError<'tcx>>; /// ### The type parameter `N` /// /// See explanation on `VtableImplData`. -#[derive(Clone)] +#[derive(Clone, RustcEncodable, RustcDecodable)] pub enum Vtable<'tcx, N> { /// Vtable identifying a particular impl. VtableImpl(VtableImplData<'tcx, N>), - /// Vtable for default trait implementations + /// Vtable for auto trait implementations /// This carries the information and nested obligations with regards - /// to a default implementation for a trait `Trait`. The nested obligations + /// to an auto implementation for a trait `Trait`. The nested obligations /// ensure the trait implementation holds for all the constituent types. - VtableDefaultImpl(VtableDefaultImplData<N>), + VtableAutoImpl(VtableAutoImplData<N>), /// Successful resolution to an obligation provided by the caller /// for some type parameter. The `Vec<N>` represents the @@ -328,14 +327,14 @@ pub enum Vtable<'tcx, N> { /// is `Obligation`, as one might expect. During trans, however, this /// is `()`, because trans only requires a shallow resolution of an /// impl, and nested obligations are satisfied later. -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)] pub struct VtableImplData<'tcx, N> { pub impl_def_id: DefId, pub substs: &'tcx Substs<'tcx>, pub nested: Vec<N> } -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)] pub struct VtableGeneratorData<'tcx, N> { pub closure_def_id: DefId, pub substs: ty::ClosureSubsts<'tcx>, @@ -344,7 +343,7 @@ pub struct VtableGeneratorData<'tcx, N> { pub nested: Vec<N> } -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)] pub struct VtableClosureData<'tcx, N> { pub closure_def_id: DefId, pub substs: ty::ClosureSubsts<'tcx>, @@ -353,20 +352,20 @@ pub struct VtableClosureData<'tcx, N> { pub nested: Vec<N> } -#[derive(Clone)] -pub struct VtableDefaultImplData<N> { +#[derive(Clone, RustcEncodable, RustcDecodable)] +pub struct VtableAutoImplData<N> { pub trait_def_id: DefId, pub nested: Vec<N> } -#[derive(Clone)] +#[derive(Clone, RustcEncodable, RustcDecodable)] pub struct VtableBuiltinData<N> { pub nested: Vec<N> } /// A vtable for some object-safe trait `Foo` automatically derived /// for the object type `Foo`. -#[derive(PartialEq,Eq,Clone)] +#[derive(PartialEq, Eq, Clone, RustcEncodable, RustcDecodable)] pub struct VtableObjectData<'tcx, N> { /// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`. pub upcast_trait_ref: ty::PolyTraitRef<'tcx>, @@ -379,7 +378,7 @@ pub struct VtableObjectData<'tcx, N> { pub nested: Vec<N>, } -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable)] pub struct VtableFnPointerData<'tcx, N> { pub fn_ty: Ty<'tcx>, pub nested: Vec<N> @@ -432,7 +431,10 @@ pub fn type_known_to_meet_bound<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx // this function's result remains infallible, we must confirm // that guess. While imperfect, I believe this is sound. - let mut fulfill_cx = FulfillmentContext::new(); + // The handling of regions in this area of the code is terrible, + // see issue #29149. We should be able to improve on this with + // NLL. + let mut fulfill_cx = FulfillmentContext::new_ignoring_regions(); // We can use a dummy node-id here because we won't pay any mind // to region obligations that arise (there shouldn't really be any @@ -512,8 +514,24 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, unnormalized_env.reveal); tcx.infer_ctxt().enter(|infcx| { - let predicates = match fully_normalize( + // FIXME. We should really... do something with these region + // obligations. But this call just continues the older + // behavior (i.e., doesn't cause any new bugs), and it would + // take some further refactoring to actually solve them. In + // particular, we would have to handle implied bounds + // properly, and that code is currently largely confined to + // regionck (though I made some efforts to extract it + // out). -nmatsakis + // + // @arielby: In any case, these obligations are checked + // by wfcheck anyway, so I'm not sure we have to check + // them here too, and we will remove this function when + // we move over to lazy normalization *anyway*. + let fulfill_cx = FulfillmentContext::new_ignoring_regions(); + + let predicates = match fully_normalize_with_fulfillcx( &infcx, + fulfill_cx, cause, elaborated_env, // You would really want to pass infcx.param_env.caller_bounds here, @@ -536,8 +554,13 @@ pub fn normalize_param_env_or_error<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, predicates); let region_scope_tree = region::ScopeTree::default(); - let free_regions = FreeRegionMap::new(); - infcx.resolve_regions_and_report_errors(region_context, ®ion_scope_tree, &free_regions); + + // We can use the `elaborated_env` here; the region code only + // cares about declarations like `'a: 'b`. + let outlives_env = OutlivesEnvironment::new(elaborated_env); + + infcx.resolve_regions_and_report_errors(region_context, ®ion_scope_tree, &outlives_env); + let predicates = match infcx.fully_resolve(&predicates) { Ok(predicates) => predicates, Err(fixup_err) => { @@ -573,9 +596,6 @@ pub fn fully_normalize<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>, -> Result<T, Vec<FulfillmentError<'tcx>>> where T : TypeFoldable<'tcx> { - debug!("fully_normalize(value={:?})", value); - - let selcx = &mut SelectionContext::new(infcx); // FIXME (@jroesch) ISSUE 26721 // I'm not sure if this is a bug or not, needs further investigation. // It appears that by reusing the fulfillment_cx here we incur more @@ -589,8 +609,21 @@ pub fn fully_normalize<'a, 'gcx, 'tcx, T>(infcx: &InferCtxt<'a, 'gcx, 'tcx>, // // I think we should probably land this refactor and then come // back to this is a follow-up patch. - let mut fulfill_cx = FulfillmentContext::new(); + let fulfillcx = FulfillmentContext::new(); + fully_normalize_with_fulfillcx(infcx, fulfillcx, cause, param_env, value) +} +pub fn fully_normalize_with_fulfillcx<'a, 'gcx, 'tcx, T>( + infcx: &InferCtxt<'a, 'gcx, 'tcx>, + mut fulfill_cx: FulfillmentContext<'tcx>, + cause: ObligationCause<'tcx>, + param_env: ty::ParamEnv<'tcx>, + value: &T) + -> Result<T, Vec<FulfillmentError<'tcx>>> + where T : TypeFoldable<'tcx> +{ + debug!("fully_normalize_with_fulfillcx(value={:?})", value); + let selcx = &mut SelectionContext::new(infcx); let Normalized { value: normalized_value, obligations } = project::normalize(selcx, param_env, cause, value); debug!("fully_normalize: normalized_value={:?} obligations={:?}", @@ -650,53 +683,55 @@ pub fn normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, /// Given a trait `trait_ref`, iterates the vtable entries /// that come from `trait_ref`, including its supertraits. #[inline] // FIXME(#35870) Avoid closures being unexported due to impl Trait. -pub fn get_vtable_methods<'a, 'tcx>( +fn vtable_methods<'a, 'tcx>( tcx: TyCtxt<'a, 'tcx, 'tcx>, trait_ref: ty::PolyTraitRef<'tcx>) - -> impl Iterator<Item=Option<(DefId, &'tcx Substs<'tcx>)>> + 'a + -> Rc<Vec<Option<(DefId, &'tcx Substs<'tcx>)>>> { - debug!("get_vtable_methods({:?})", trait_ref); - - supertraits(tcx, trait_ref).flat_map(move |trait_ref| { - let trait_methods = tcx.associated_items(trait_ref.def_id()) - .filter(|item| item.kind == ty::AssociatedKind::Method); - - // Now list each method's DefId and Substs (for within its trait). - // If the method can never be called from this object, produce None. - trait_methods.map(move |trait_method| { - debug!("get_vtable_methods: trait_method={:?}", trait_method); - let def_id = trait_method.def_id; - - // Some methods cannot be called on an object; skip those. - if !tcx.is_vtable_safe_method(trait_ref.def_id(), &trait_method) { - debug!("get_vtable_methods: not vtable safe"); - return None; - } - - // the method may have some early-bound lifetimes, add - // regions for those - let substs = Substs::for_item(tcx, def_id, - |_, _| tcx.types.re_erased, - |def, _| trait_ref.substs().type_for_def(def)); - - // the trait type may have higher-ranked lifetimes in it; - // so erase them if they appear, so that we get the type - // at some particular call site - let substs = tcx.erase_late_bound_regions_and_normalize(&ty::Binder(substs)); - - // It's possible that the method relies on where clauses that - // do not hold for this particular set of type parameters. - // Note that this method could then never be called, so we - // do not want to try and trans it, in that case (see #23435). - let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs); - if !normalize_and_test_predicates(tcx, predicates.predicates) { - debug!("get_vtable_methods: predicates do not hold"); - return None; - } - - Some((def_id, substs)) - }) - }) + debug!("vtable_methods({:?})", trait_ref); + + Rc::new( + supertraits(tcx, trait_ref).flat_map(move |trait_ref| { + let trait_methods = tcx.associated_items(trait_ref.def_id()) + .filter(|item| item.kind == ty::AssociatedKind::Method); + + // Now list each method's DefId and Substs (for within its trait). + // If the method can never be called from this object, produce None. + trait_methods.map(move |trait_method| { + debug!("vtable_methods: trait_method={:?}", trait_method); + let def_id = trait_method.def_id; + + // Some methods cannot be called on an object; skip those. + if !tcx.is_vtable_safe_method(trait_ref.def_id(), &trait_method) { + debug!("vtable_methods: not vtable safe"); + return None; + } + + // the method may have some early-bound lifetimes, add + // regions for those + let substs = Substs::for_item(tcx, def_id, + |_, _| tcx.types.re_erased, + |def, _| trait_ref.substs().type_for_def(def)); + + // the trait type may have higher-ranked lifetimes in it; + // so erase them if they appear, so that we get the type + // at some particular call site + let substs = tcx.erase_late_bound_regions_and_normalize(&ty::Binder(substs)); + + // It's possible that the method relies on where clauses that + // do not hold for this particular set of type parameters. + // Note that this method could then never be called, so we + // do not want to try and trans it, in that case (see #23435). + let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs); + if !normalize_and_test_predicates(tcx, predicates.predicates) { + debug!("vtable_methods: predicates do not hold"); + return None; + } + + Some((def_id, substs)) + }) + }).collect() + ) } impl<'tcx,O> Obligation<'tcx,O> { @@ -756,7 +791,7 @@ impl<'tcx, N> Vtable<'tcx, N> { VtableImpl(i) => i.nested, VtableParam(n) => n, VtableBuiltin(i) => i.nested, - VtableDefaultImpl(d) => d.nested, + VtableAutoImpl(d) => d.nested, VtableClosure(c) => c.nested, VtableGenerator(c) => c.nested, VtableObject(d) => d.nested, @@ -769,7 +804,7 @@ impl<'tcx, N> Vtable<'tcx, N> { &mut VtableImpl(ref mut i) => &mut i.nested, &mut VtableParam(ref mut n) => n, &mut VtableBuiltin(ref mut i) => &mut i.nested, - &mut VtableDefaultImpl(ref mut d) => &mut d.nested, + &mut VtableAutoImpl(ref mut d) => &mut d.nested, &mut VtableGenerator(ref mut c) => &mut c.nested, &mut VtableClosure(ref mut c) => &mut c.nested, &mut VtableObject(ref mut d) => &mut d.nested, @@ -793,7 +828,7 @@ impl<'tcx, N> Vtable<'tcx, N> { vtable_base: o.vtable_base, nested: o.nested.into_iter().map(f).collect(), }), - VtableDefaultImpl(d) => VtableDefaultImpl(VtableDefaultImplData { + VtableAutoImpl(d) => VtableAutoImpl(VtableAutoImplData { trait_def_id: d.trait_def_id, nested: d.nested.into_iter().map(f).collect(), }), @@ -835,15 +870,8 @@ pub fn provide(providers: &mut ty::maps::Providers) { is_object_safe: object_safety::is_object_safe_provider, specialization_graph_of: specialize::specialization_graph_provider, specializes: specialize::specializes, - ..*providers - }; -} - -pub fn provide_extern(providers: &mut ty::maps::Providers) { - *providers = ty::maps::Providers { - is_object_safe: object_safety::is_object_safe_provider, - specialization_graph_of: specialize::specialization_graph_provider, - specializes: specialize::specializes, + trans_fulfill_obligation: trans::trans_fulfill_obligation, + vtable_methods, ..*providers }; } |
