Skip to content

Commit 8121db3

Browse files
committed
Merge branch 'refactor-select' of https://github.com/aravind-pg/rust into update-cargo
2 parents 53cda8e + 81f0b96 commit 8121db3

File tree

2 files changed

+39
-84
lines changed

2 files changed

+39
-84
lines changed

src/librustc/traits/mod.rs

-13
Original file line numberDiff line numberDiff line change
@@ -851,19 +851,6 @@ impl<'tcx, N> Vtable<'tcx, N> {
851851
}
852852
}
853853

854-
fn nested_obligations_mut(&mut self) -> &mut Vec<N> {
855-
match self {
856-
&mut VtableImpl(ref mut i) => &mut i.nested,
857-
&mut VtableParam(ref mut n) => n,
858-
&mut VtableBuiltin(ref mut i) => &mut i.nested,
859-
&mut VtableAutoImpl(ref mut d) => &mut d.nested,
860-
&mut VtableGenerator(ref mut c) => &mut c.nested,
861-
&mut VtableClosure(ref mut c) => &mut c.nested,
862-
&mut VtableObject(ref mut d) => &mut d.nested,
863-
&mut VtableFnPointer(ref mut d) => &mut d.nested,
864-
}
865-
}
866-
867854
pub fn map<M, F>(self, f: F) -> Vtable<'tcx, M> where F: FnMut(N) -> M {
868855
match self {
869856
VtableImpl(i) => VtableImpl(VtableImplData {

src/librustc/traits/select.rs

+39-71
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,17 @@ use ty::relate::TypeRelation;
4444
use middle::lang_items;
4545

4646
use rustc_data_structures::bitvec::BitVector;
47-
use rustc_data_structures::snapshot_vec::{SnapshotVecDelegate, SnapshotVec};
4847
use std::iter;
4948
use std::cell::RefCell;
5049
use std::cmp;
5150
use std::fmt;
52-
use std::marker::PhantomData;
5351
use std::mem;
5452
use std::rc::Rc;
5553
use syntax::abi::Abi;
5654
use hir;
5755
use lint;
5856
use util::nodemap::{FxHashMap, FxHashSet};
5957

60-
struct InferredObligationsSnapshotVecDelegate<'tcx> {
61-
phantom: PhantomData<&'tcx i32>,
62-
}
63-
impl<'tcx> SnapshotVecDelegate for InferredObligationsSnapshotVecDelegate<'tcx> {
64-
type Value = PredicateObligation<'tcx>;
65-
type Undo = ();
66-
fn reverse(_: &mut Vec<Self::Value>, _: Self::Undo) {}
67-
}
6858

6959
pub struct SelectionContext<'cx, 'gcx: 'cx+'tcx, 'tcx: 'cx> {
7060
infcx: &'cx InferCtxt<'cx, 'gcx, 'tcx>,
@@ -92,8 +82,6 @@ pub struct SelectionContext<'cx, 'gcx: 'cx+'tcx, 'tcx: 'cx> {
9282
/// would satisfy it. This avoids crippling inference, basically.
9383
intercrate: Option<IntercrateMode>,
9484

95-
inferred_obligations: SnapshotVec<InferredObligationsSnapshotVecDelegate<'tcx>>,
96-
9785
intercrate_ambiguity_causes: Option<Vec<IntercrateAmbiguityCause>>,
9886

9987
/// Controls whether or not to filter out negative impls when selecting.
@@ -429,7 +417,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
429417
infcx,
430418
freshener: infcx.freshener(),
431419
intercrate: None,
432-
inferred_obligations: SnapshotVec::new(),
433420
intercrate_ambiguity_causes: None,
434421
allow_negative_impls: false,
435422
}
@@ -442,7 +429,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
442429
infcx,
443430
freshener: infcx.freshener(),
444431
intercrate: Some(mode),
445-
inferred_obligations: SnapshotVec::new(),
446432
intercrate_ambiguity_causes: None,
447433
allow_negative_impls: false,
448434
}
@@ -455,7 +441,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
455441
infcx,
456442
freshener: infcx.freshener(),
457443
intercrate: None,
458-
inferred_obligations: SnapshotVec::new(),
459444
intercrate_ambiguity_causes: None,
460445
allow_negative_impls,
461446
}
@@ -498,8 +483,6 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
498483
fn in_snapshot<R, F>(&mut self, f: F) -> R
499484
where F: FnOnce(&mut Self, &infer::CombinedSnapshot<'cx, 'tcx>) -> R
500485
{
501-
// The irrefutable nature of the operation means we don't need to snapshot the
502-
// inferred_obligations vector.
503486
self.infcx.in_snapshot(|snapshot| f(self, snapshot))
504487
}
505488

@@ -508,28 +491,15 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
508491
fn probe<R, F>(&mut self, f: F) -> R
509492
where F: FnOnce(&mut Self, &infer::CombinedSnapshot<'cx, 'tcx>) -> R
510493
{
511-
let inferred_obligations_snapshot = self.inferred_obligations.start_snapshot();
512-
let result = self.infcx.probe(|snapshot| f(self, snapshot));
513-
self.inferred_obligations.rollback_to(inferred_obligations_snapshot);
514-
result
494+
self.infcx.probe(|snapshot| f(self, snapshot))
515495
}
516496

517497
/// Wraps a commit_if_ok s.t. obligations collected during it are not returned in selection if
518498
/// the transaction fails and s.t. old obligations are retained.
519499
fn commit_if_ok<T, E, F>(&mut self, f: F) -> Result<T, E> where
520500
F: FnOnce(&mut Self, &infer::CombinedSnapshot) -> Result<T, E>
521501
{
522-
let inferred_obligations_snapshot = self.inferred_obligations.start_snapshot();
523-
match self.infcx.commit_if_ok(|snapshot| f(self, snapshot)) {
524-
Ok(ok) => {
525-
self.inferred_obligations.commit(inferred_obligations_snapshot);
526-
Ok(ok)
527-
},
528-
Err(err) => {
529-
self.inferred_obligations.rollback_to(inferred_obligations_snapshot);
530-
Err(err)
531-
}
532-
}
502+
self.infcx.commit_if_ok(|snapshot| f(self, snapshot))
533503
}
534504

535505

@@ -560,12 +530,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
560530
let stack = self.push_stack(TraitObligationStackList::empty(), obligation);
561531
let ret = match self.candidate_from_obligation(&stack)? {
562532
None => None,
563-
Some(candidate) => {
564-
let mut candidate = self.confirm_candidate(obligation, candidate)?;
565-
let inferred_obligations = (*self.inferred_obligations).into_iter().cloned();
566-
candidate.nested_obligations_mut().extend(inferred_obligations);
567-
Some(candidate)
568-
},
533+
Some(candidate) => Some(self.confirm_candidate(obligation, candidate)?)
569534
};
570535

571536
// Test whether this is a `()` which was produced by defaulting a
@@ -658,7 +623,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
658623
stack: TraitObligationStackList<'o, 'tcx>,
659624
predicates: I)
660625
-> EvaluationResult
661-
where I : Iterator<Item=&'a PredicateObligation<'tcx>>, 'tcx:'a
626+
where I : IntoIterator<Item=&'a PredicateObligation<'tcx>>, 'tcx:'a
662627
{
663628
let mut result = EvaluatedToOk;
664629
for obligation in predicates {
@@ -695,7 +660,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
695660
// does this code ever run?
696661
match self.infcx.subtype_predicate(&obligation.cause, obligation.param_env, p) {
697662
Some(Ok(InferOk { obligations, .. })) => {
698-
self.inferred_obligations.extend(obligations);
663+
self.evaluate_predicates_recursively(previous_stack, &obligations);
699664
EvaluatedToOk
700665
},
701666
Some(Err(_)) => EvaluatedToErr,
@@ -1542,12 +1507,9 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
15421507
-> bool
15431508
{
15441509
assert!(!skol_trait_ref.has_escaping_regions());
1545-
match self.infcx.at(&obligation.cause, obligation.param_env)
1546-
.sup(ty::Binder(skol_trait_ref), trait_bound) {
1547-
Ok(InferOk { obligations, .. }) => {
1548-
self.inferred_obligations.extend(obligations);
1549-
}
1550-
Err(_) => { return false; }
1510+
if let Err(_) = self.infcx.at(&obligation.cause, obligation.param_env)
1511+
.sup(ty::Binder(skol_trait_ref), trait_bound) {
1512+
return false;
15511513
}
15521514

15531515
self.infcx.leak_check(false, obligation.cause.span, skol_map, snapshot).is_ok()
@@ -2633,6 +2595,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
26332595
};
26342596

26352597
let mut upcast_trait_ref = None;
2598+
let mut nested = vec![];
26362599
let vtable_base;
26372600

26382601
{
@@ -2651,7 +2614,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
26512614
self.commit_if_ok(
26522615
|this, _| this.match_poly_trait_ref(obligation, t))
26532616
{
2654-
Ok(_) => { upcast_trait_ref = Some(t); false }
2617+
Ok(obligations) => {
2618+
upcast_trait_ref = Some(t);
2619+
nested.extend(obligations);
2620+
false
2621+
}
26552622
Err(_) => { true }
26562623
}
26572624
});
@@ -2669,7 +2636,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
26692636
VtableObjectData {
26702637
upcast_trait_ref: upcast_trait_ref.unwrap(),
26712638
vtable_base,
2672-
nested: vec![]
2639+
nested,
26732640
}
26742641
}
26752642

@@ -2726,7 +2693,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
27262693
self.generator_trait_ref_unnormalized(obligation, closure_def_id, substs);
27272694
let Normalized {
27282695
value: trait_ref,
2729-
obligations
2696+
mut obligations
27302697
} = normalize_with_depth(self,
27312698
obligation.param_env,
27322699
obligation.cause.clone(),
@@ -2738,10 +2705,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
27382705
trait_ref,
27392706
obligations);
27402707

2741-
self.confirm_poly_trait_refs(obligation.cause.clone(),
2742-
obligation.param_env,
2743-
obligation.predicate.to_poly_trait_ref(),
2744-
trait_ref)?;
2708+
obligations.extend(
2709+
self.confirm_poly_trait_refs(obligation.cause.clone(),
2710+
obligation.param_env,
2711+
obligation.predicate.to_poly_trait_ref(),
2712+
trait_ref)?);
27452713

27462714
Ok(VtableGeneratorData {
27472715
closure_def_id: closure_def_id,
@@ -2787,10 +2755,11 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
27872755
trait_ref,
27882756
obligations);
27892757

2790-
self.confirm_poly_trait_refs(obligation.cause.clone(),
2791-
obligation.param_env,
2792-
obligation.predicate.to_poly_trait_ref(),
2793-
trait_ref)?;
2758+
obligations.extend(
2759+
self.confirm_poly_trait_refs(obligation.cause.clone(),
2760+
obligation.param_env,
2761+
obligation.predicate.to_poly_trait_ref(),
2762+
trait_ref)?);
27942763

27952764
obligations.push(Obligation::new(
27962765
obligation.cause.clone(),
@@ -2834,13 +2803,13 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
28342803
obligation_param_env: ty::ParamEnv<'tcx>,
28352804
obligation_trait_ref: ty::PolyTraitRef<'tcx>,
28362805
expected_trait_ref: ty::PolyTraitRef<'tcx>)
2837-
-> Result<(), SelectionError<'tcx>>
2806+
-> Result<Vec<PredicateObligation<'tcx>>, SelectionError<'tcx>>
28382807
{
28392808
let obligation_trait_ref = obligation_trait_ref.clone();
28402809
self.infcx
28412810
.at(&obligation_cause, obligation_param_env)
28422811
.sup(obligation_trait_ref, expected_trait_ref)
2843-
.map(|InferOk { obligations, .. }| self.inferred_obligations.extend(obligations))
2812+
.map(|InferOk { obligations, .. }| obligations)
28442813
.map_err(|e| OutputTypeParameterMismatch(expected_trait_ref, obligation_trait_ref, e))
28452814
}
28462815

@@ -2877,7 +2846,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
28772846
self.infcx.at(&obligation.cause, obligation.param_env)
28782847
.eq(target, new_trait)
28792848
.map_err(|_| Unimplemented)?;
2880-
self.inferred_obligations.extend(obligations);
2849+
nested.extend(obligations);
28812850

28822851
// Register one obligation for 'a: 'b.
28832852
let cause = ObligationCause::new(obligation.cause.span,
@@ -2939,7 +2908,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
29392908
self.infcx.at(&obligation.cause, obligation.param_env)
29402909
.eq(b, a)
29412910
.map_err(|_| Unimplemented)?;
2942-
self.inferred_obligations.extend(obligations);
2911+
nested.extend(obligations);
29432912
}
29442913

29452914
// Struct<T> -> Struct<U>.
@@ -3003,7 +2972,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
30032972
self.infcx.at(&obligation.cause, obligation.param_env)
30042973
.eq(target, new_struct)
30052974
.map_err(|_| Unimplemented)?;
3006-
self.inferred_obligations.extend(obligations);
2975+
nested.extend(obligations);
30072976

30082977
// Construct the nested Field<T>: Unsize<Field<U>> predicate.
30092978
nested.push(tcx.predicate_for_trait_def(
@@ -3034,7 +3003,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
30343003
self.infcx.at(&obligation.cause, obligation.param_env)
30353004
.eq(target, new_tuple)
30363005
.map_err(|_| Unimplemented)?;
3037-
self.inferred_obligations.extend(obligations);
3006+
nested.extend(obligations);
30383007

30393008
// Construct the nested T: Unsize<U> predicate.
30403009
nested.push(tcx.predicate_for_trait_def(
@@ -3107,7 +3076,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
31073076
let impl_trait_ref = impl_trait_ref.subst(self.tcx(),
31083077
impl_substs);
31093078

3110-
let impl_trait_ref =
3079+
let Normalized { value: impl_trait_ref, obligations: mut nested_obligations } =
31113080
project::normalize_with_depth(self,
31123081
obligation.param_env,
31133082
obligation.cause.clone(),
@@ -3123,12 +3092,12 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
31233092

31243093
let InferOk { obligations, .. } =
31253094
self.infcx.at(&obligation.cause, obligation.param_env)
3126-
.eq(skol_obligation_trait_ref, impl_trait_ref.value)
3095+
.eq(skol_obligation_trait_ref, impl_trait_ref)
31273096
.map_err(|e| {
31283097
debug!("match_impl: failed eq_trait_refs due to `{}`", e);
31293098
()
31303099
})?;
3131-
self.inferred_obligations.extend(obligations);
3100+
nested_obligations.extend(obligations);
31323101

31333102
if let Err(e) = self.infcx.leak_check(false,
31343103
obligation.cause.span,
@@ -3141,7 +3110,7 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
31413110
debug!("match_impl: success impl_substs={:?}", impl_substs);
31423111
Ok((Normalized {
31433112
value: impl_substs,
3144-
obligations: impl_trait_ref.obligations
3113+
obligations: nested_obligations
31453114
}, skol_map))
31463115
}
31473116

@@ -3178,24 +3147,23 @@ impl<'cx, 'gcx, 'tcx> SelectionContext<'cx, 'gcx, 'tcx> {
31783147
where_clause_trait_ref: ty::PolyTraitRef<'tcx>)
31793148
-> Result<Vec<PredicateObligation<'tcx>>,()>
31803149
{
3181-
self.match_poly_trait_ref(obligation, where_clause_trait_ref)?;
3182-
Ok(Vec::new())
3150+
self.match_poly_trait_ref(obligation, where_clause_trait_ref)
31833151
}
31843152

31853153
/// Returns `Ok` if `poly_trait_ref` being true implies that the
31863154
/// obligation is satisfied.
31873155
fn match_poly_trait_ref(&mut self,
31883156
obligation: &TraitObligation<'tcx>,
31893157
poly_trait_ref: ty::PolyTraitRef<'tcx>)
3190-
-> Result<(),()>
3158+
-> Result<Vec<PredicateObligation<'tcx>>,()>
31913159
{
31923160
debug!("match_poly_trait_ref: obligation={:?} poly_trait_ref={:?}",
31933161
obligation,
31943162
poly_trait_ref);
31953163

31963164
self.infcx.at(&obligation.cause, obligation.param_env)
31973165
.sup(obligation.predicate.to_poly_trait_ref(), poly_trait_ref)
3198-
.map(|InferOk { obligations, .. }| self.inferred_obligations.extend(obligations))
3166+
.map(|InferOk { obligations, .. }| obligations)
31993167
.map_err(|_| ())
32003168
}
32013169

0 commit comments

Comments
 (0)