Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the global fulfillment cache with the evaluation cache #42840

Merged
merged 4 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 19 additions & 112 deletions src/librustc/traits/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use dep_graph::DepGraph;
use infer::{InferCtxt, InferOk};
use ty::{self, Ty, TypeFoldable, ToPolyTraitRef, TyCtxt, ToPredicate};
use ty::{self, Ty, TypeFoldable, ToPolyTraitRef, ToPredicate};
use ty::error::ExpectedFound;
use rustc_data_structures::obligation_forest::{ObligationForest, Error};
use rustc_data_structures::obligation_forest::{ForestObligation, ObligationProcessor};
use std::marker::PhantomData;
use syntax::ast;
use util::nodemap::{FxHashSet, NodeMap};
use util::nodemap::NodeMap;
use hir::def_id::DefId;

use super::CodeAmbiguity;
Expand All @@ -34,11 +33,6 @@ impl<'tcx> ForestObligation for PendingPredicateObligation<'tcx> {
fn as_predicate(&self) -> &Self::Predicate { &self.obligation.predicate }
}

pub struct GlobalFulfilledPredicates<'tcx> {
set: FxHashSet<ty::PolyTraitPredicate<'tcx>>,
dep_graph: DepGraph,
}

/// The fulfillment context is used to drive trait resolution. It
/// consists of a list of obligations that must be (eventually)
/// satisfied. The job is to track which are satisfied, which yielded
Expand Down Expand Up @@ -183,13 +177,6 @@ impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {

assert!(!infcx.is_in_snapshot());

let tcx = infcx.tcx;

if tcx.fulfilled_predicates.borrow().check_duplicate(tcx, &obligation.predicate) {
debug!("register_predicate_obligation: duplicate");
return
}

self.predicates.register_obligation(PendingPredicateObligation {
obligation,
stalled_on: vec![]
Expand Down Expand Up @@ -264,13 +251,6 @@ impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
});
debug!("select: outcome={:?}", outcome);

// these are obligations that were proven to be true.
for pending_obligation in outcome.completed {
let predicate = &pending_obligation.obligation.predicate;
selcx.tcx().fulfilled_predicates.borrow_mut()
.add_if_global(selcx.tcx(), predicate);
}

errors.extend(
outcome.errors.into_iter()
.map(|e| to_fulfillment_error(e)));
Expand Down Expand Up @@ -318,7 +298,7 @@ impl<'a, 'b, 'gcx, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'gcx,
_marker: PhantomData<&'c PendingPredicateObligation<'tcx>>)
where I: Clone + Iterator<Item=&'c PendingPredicateObligation<'tcx>>,
{
if coinductive_match(self.selcx, cycle.clone()) {
if self.selcx.coinductive_match(cycle.clone().map(|s| s.obligation.predicate)) {
debug!("process_child_obligations: coinductive match");
} else {
let cycle : Vec<_> = cycle.map(|c| c.obligation.clone()).collect();
Expand Down Expand Up @@ -375,21 +355,31 @@ fn process_predicate<'a, 'gcx, 'tcx>(

match obligation.predicate {
ty::Predicate::Trait(ref data) => {
let tcx = selcx.tcx();
if tcx.fulfilled_predicates.borrow().check_duplicate_trait(tcx, data) {
return Ok(Some(vec![]));
let trait_obligation = obligation.with(data.clone());

if data.is_global() {
// no type variables present, can use evaluation for better caching.
// FIXME: consider caching errors too.
if
// make defaulted unit go through the slow path for better warnings,
// please remove this when the warnings are removed.
!trait_obligation.predicate.skip_binder().self_ty().is_defaulted_unit() &&
selcx.evaluate_obligation_conservatively(&obligation) {
debug!("selecting trait `{:?}` at depth {} evaluated to holds",
data, obligation.recursion_depth);
return Ok(Some(vec![]))
}
}

let trait_obligation = obligation.with(data.clone());
match selcx.select(&trait_obligation) {
Ok(Some(vtable)) => {
debug!("selecting trait `{:?}` at depth {} yielded Ok(Some)",
data, obligation.recursion_depth);
data, obligation.recursion_depth);
Ok(Some(vtable.nested_obligations()))
}
Ok(None) => {
debug!("selecting trait `{:?}` at depth {} yielded Ok(None)",
data, obligation.recursion_depth);
data, obligation.recursion_depth);

// This is a bit subtle: for the most part, the
// only reason we can fail to make progress on
Expand Down Expand Up @@ -549,40 +539,6 @@ fn process_predicate<'a, 'gcx, 'tcx>(
}
}

/// For defaulted traits, we use a co-inductive strategy to solve, so
/// that recursion is ok. This routine returns true if the top of the
/// stack (`cycle[0]`):
/// - is a defaulted trait, and
/// - it also appears in the backtrace at some position `X`; and,
/// - all the predicates at positions `X..` between `X` an the top are
/// also defaulted traits.
fn coinductive_match<'a,'c,'gcx,'tcx,I>(selcx: &mut SelectionContext<'a,'gcx,'tcx>,
cycle: I) -> bool
where I: Iterator<Item=&'c PendingPredicateObligation<'tcx>>,
'tcx: 'c
{
let mut cycle = cycle;
cycle
.all(|bt_obligation| {
let result = coinductive_obligation(selcx, &bt_obligation.obligation);
debug!("coinductive_match: bt_obligation={:?} coinductive={}",
bt_obligation, result);
result
})
}

fn coinductive_obligation<'a,'gcx,'tcx>(selcx: &SelectionContext<'a,'gcx,'tcx>,
obligation: &PredicateObligation<'tcx>)
-> bool {
match obligation.predicate {
ty::Predicate::Trait(ref data) => {
selcx.tcx().trait_has_default_impl(data.def_id())
}
_ => {
false
}
}
}

fn register_region_obligation<'tcx>(t_a: Ty<'tcx>,
r_b: ty::Region<'tcx>,
Expand All @@ -602,55 +558,6 @@ fn register_region_obligation<'tcx>(t_a: Ty<'tcx>,

}

impl<'a, 'gcx, 'tcx> GlobalFulfilledPredicates<'gcx> {
pub fn new(dep_graph: DepGraph) -> GlobalFulfilledPredicates<'gcx> {
GlobalFulfilledPredicates {
set: FxHashSet(),
dep_graph,
}
}

pub fn check_duplicate(&self, tcx: TyCtxt, key: &ty::Predicate<'tcx>) -> bool {
if let ty::Predicate::Trait(ref data) = *key {
self.check_duplicate_trait(tcx, data)
} else {
false
}
}

pub fn check_duplicate_trait(&self, tcx: TyCtxt, data: &ty::PolyTraitPredicate<'tcx>) -> bool {
// For the global predicate registry, when we find a match, it
// may have been computed by some other task, so we want to
// add a read from the node corresponding to the predicate
// processing to make sure we get the transitive dependencies.
if self.set.contains(data) {
debug_assert!(data.is_global());
self.dep_graph.read(data.dep_node(tcx));
debug!("check_duplicate: global predicate `{:?}` already proved elsewhere", data);

true
} else {
false
}
}

fn add_if_global(&mut self, tcx: TyCtxt<'a, 'gcx, 'tcx>, key: &ty::Predicate<'tcx>) {
if let ty::Predicate::Trait(ref data) = *key {
// We only add things to the global predicate registry
// after the current task has proved them, and hence
// already has the required read edges, so we don't need
// to add any more edges here.
if data.is_global() {
if let Some(data) = tcx.lift_to_global(data) {
if self.set.insert(data.clone()) {
debug!("add_if_global: global predicate `{:?}` added", data);
}
}
}
}
}
}

fn to_fulfillment_error<'tcx>(
error: Error<PendingPredicateObligation<'tcx>, FulfillmentErrorCode<'tcx>>)
-> FulfillmentError<'tcx>
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use syntax_pos::{Span, DUMMY_SP};
pub use self::coherence::orphan_check;
pub use self::coherence::overlapping_impls;
pub use self::coherence::OrphanCheckErr;
pub use self::fulfill::{FulfillmentContext, GlobalFulfilledPredicates, RegionObligation};
pub use self::fulfill::{FulfillmentContext, RegionObligation};
pub use self::project::MismatchedProjectionTypes;
pub use self::project::{normalize, normalize_projection_type, Normalized};
pub use self::project::{ProjectionCache, ProjectionCacheSnapshot, Reveal};
Expand Down
Loading