Skip to content

Commit edd7d42

Browse files
committed
Rollup merge of rust-lang#33852 - arielb1:autoderef-iterator, r=eddyb
refactor autoderef to avoid prematurely registering obligations Refactor `FnCtxt::autoderef` to use an external iterator and to not register any obligation from the main autoderef loop, but rather to register them after (and if) the loop successfully completes. Fixes rust-lang#24819 Fixes rust-lang#25801 Fixes rust-lang#27631 Fixes rust-lang#31258 Fixes rust-lang#31964 Fixes rust-lang#32320 Fixes rust-lang#33515 Fixes rust-lang#33755 r? @eddyb
2 parents 6e897d7 + 040fc94 commit edd7d42

18 files changed

+515
-472
lines changed

src/librustc/infer/mod.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
163163
// If the number of errors increases, that's also a sign (line
164164
// `tained_by_errors`) to avoid reporting certain kinds of errors.
165165
err_count_on_creation: usize,
166+
167+
// This flag is used for debugging, and is set to true if there are
168+
// any obligations set during the current snapshot. In that case, the
169+
// snapshot can't be rolled back.
170+
pub obligations_in_snapshot: Cell<bool>,
166171
}
167172

168173
/// A map returned by `skolemize_late_bound_regions()` indicating the skolemized
@@ -476,7 +481,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
476481
normalize: false,
477482
projection_mode: ProjectionMode::AnyFinal,
478483
tainted_by_errors_flag: Cell::new(false),
479-
err_count_on_creation: self.sess.err_count()
484+
err_count_on_creation: self.sess.err_count(),
485+
obligations_in_snapshot: Cell::new(false),
480486
}
481487
}
482488
}
@@ -515,7 +521,8 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
515521
normalize: normalize,
516522
projection_mode: projection_mode,
517523
tainted_by_errors_flag: Cell::new(false),
518-
err_count_on_creation: tcx.sess.err_count()
524+
err_count_on_creation: tcx.sess.err_count(),
525+
obligations_in_snapshot: Cell::new(false),
519526
}))
520527
}
521528
}
@@ -542,6 +549,7 @@ pub struct CombinedSnapshot {
542549
int_snapshot: unify::Snapshot<ty::IntVid>,
543550
float_snapshot: unify::Snapshot<ty::FloatVid>,
544551
region_vars_snapshot: RegionSnapshot,
552+
obligations_in_snapshot: bool,
545553
}
546554

547555
/// Helper trait for shortening the lifetimes inside a
@@ -809,11 +817,15 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
809817
}
810818

811819
fn start_snapshot(&self) -> CombinedSnapshot {
820+
let obligations_in_snapshot = self.obligations_in_snapshot.get();
821+
self.obligations_in_snapshot.set(false);
822+
812823
CombinedSnapshot {
813824
type_snapshot: self.type_variables.borrow_mut().snapshot(),
814825
int_snapshot: self.int_unification_table.borrow_mut().snapshot(),
815826
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
816827
region_vars_snapshot: self.region_vars.start_snapshot(),
828+
obligations_in_snapshot: obligations_in_snapshot,
817829
}
818830
}
819831

@@ -822,7 +834,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
822834
let CombinedSnapshot { type_snapshot,
823835
int_snapshot,
824836
float_snapshot,
825-
region_vars_snapshot } = snapshot;
837+
region_vars_snapshot,
838+
obligations_in_snapshot } = snapshot;
839+
840+
assert!(!self.obligations_in_snapshot.get());
841+
self.obligations_in_snapshot.set(obligations_in_snapshot);
826842

827843
self.type_variables
828844
.borrow_mut()
@@ -842,7 +858,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
842858
let CombinedSnapshot { type_snapshot,
843859
int_snapshot,
844860
float_snapshot,
845-
region_vars_snapshot } = snapshot;
861+
region_vars_snapshot,
862+
obligations_in_snapshot } = snapshot;
863+
864+
self.obligations_in_snapshot.set(obligations_in_snapshot);
846865

847866
self.type_variables
848867
.borrow_mut()
@@ -904,12 +923,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
904923
let CombinedSnapshot { type_snapshot,
905924
int_snapshot,
906925
float_snapshot,
907-
region_vars_snapshot } = self.start_snapshot();
926+
region_vars_snapshot,
927+
obligations_in_snapshot } = self.start_snapshot();
908928

909929
let r = self.commit_if_ok(|_| f());
910930

911931
debug!("commit_regions_if_ok: rolling back everything but regions");
912932

933+
assert!(!self.obligations_in_snapshot.get());
934+
self.obligations_in_snapshot.set(obligations_in_snapshot);
935+
913936
// Roll back any non-region bindings - they should be resolved
914937
// inside `f`, with, e.g. `resolve_type_vars_if_possible`.
915938
self.type_variables

src/librustc/traits/fulfill.rs

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
171171
// debug output much nicer to read and so on.
172172
let obligation = infcx.resolve_type_vars_if_possible(&obligation);
173173

174+
infcx.obligations_in_snapshot.set(true);
175+
174176
if infcx.tcx.fulfilled_predicates.borrow().check_duplicate(&obligation.predicate)
175177
{
176178
return

src/librustc/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub use self::coherence::overlapping_impls;
3131
pub use self::coherence::OrphanCheckErr;
3232
pub use self::fulfill::{FulfillmentContext, GlobalFulfilledPredicates, RegionObligation};
3333
pub use self::project::{MismatchedProjectionTypes, ProjectionMode};
34-
pub use self::project::{normalize, Normalized};
34+
pub use self::project::{normalize, normalize_projection_type, Normalized};
3535
pub use self::object_safety::ObjectSafetyViolation;
3636
pub use self::object_safety::MethodViolationCode;
3737
pub use self::select::{EvaluationCache, SelectionContext, SelectionCache};

src/librustc/traits/specialize/mod.rs

+40-42
Original file line numberDiff line numberDiff line change
@@ -187,51 +187,49 @@ fn fulfill_implication<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
187187
source_trait_ref: ty::TraitRef<'tcx>,
188188
target_impl: DefId)
189189
-> Result<&'tcx Substs<'tcx>, ()> {
190-
infcx.commit_if_ok(|_| {
191-
let selcx = &mut SelectionContext::new(&infcx);
192-
let target_substs = fresh_type_vars_for_impl(&infcx, DUMMY_SP, target_impl);
193-
let (target_trait_ref, obligations) = impl_trait_ref_and_oblig(selcx,
194-
target_impl,
195-
&target_substs);
196-
197-
// do the impls unify? If not, no specialization.
198-
if let Err(_) = infcx.eq_trait_refs(true,
199-
TypeOrigin::Misc(DUMMY_SP),
200-
source_trait_ref,
201-
target_trait_ref) {
202-
debug!("fulfill_implication: {:?} does not unify with {:?}",
203-
source_trait_ref,
204-
target_trait_ref);
205-
return Err(());
206-
}
190+
let selcx = &mut SelectionContext::new(&infcx);
191+
let target_substs = fresh_type_vars_for_impl(&infcx, DUMMY_SP, target_impl);
192+
let (target_trait_ref, obligations) = impl_trait_ref_and_oblig(selcx,
193+
target_impl,
194+
&target_substs);
195+
196+
// do the impls unify? If not, no specialization.
197+
if let Err(_) = infcx.eq_trait_refs(true,
198+
TypeOrigin::Misc(DUMMY_SP),
199+
source_trait_ref,
200+
target_trait_ref) {
201+
debug!("fulfill_implication: {:?} does not unify with {:?}",
202+
source_trait_ref,
203+
target_trait_ref);
204+
return Err(());
205+
}
207206

208-
// attempt to prove all of the predicates for impl2 given those for impl1
209-
// (which are packed up in penv)
207+
// attempt to prove all of the predicates for impl2 given those for impl1
208+
// (which are packed up in penv)
210209

211-
let mut fulfill_cx = FulfillmentContext::new();
212-
for oblig in obligations.into_iter() {
213-
fulfill_cx.register_predicate_obligation(&infcx, oblig);
214-
}
210+
let mut fulfill_cx = FulfillmentContext::new();
211+
for oblig in obligations.into_iter() {
212+
fulfill_cx.register_predicate_obligation(&infcx, oblig);
213+
}
215214

216-
if let Err(errors) = infcx.drain_fulfillment_cx(&mut fulfill_cx, &()) {
217-
// no dice!
218-
debug!("fulfill_implication: for impls on {:?} and {:?}, could not fulfill: {:?} given \
219-
{:?}",
220-
source_trait_ref,
221-
target_trait_ref,
222-
errors,
223-
infcx.parameter_environment.caller_bounds);
224-
Err(())
225-
} else {
226-
debug!("fulfill_implication: an impl for {:?} specializes {:?}",
227-
source_trait_ref,
228-
target_trait_ref);
229-
230-
// Now resolve the *substitution* we built for the target earlier, replacing
231-
// the inference variables inside with whatever we got from fulfillment.
232-
Ok(infcx.resolve_type_vars_if_possible(&target_substs))
233-
}
234-
})
215+
if let Err(errors) = infcx.drain_fulfillment_cx(&mut fulfill_cx, &()) {
216+
// no dice!
217+
debug!("fulfill_implication: for impls on {:?} and {:?}, could not fulfill: {:?} given \
218+
{:?}",
219+
source_trait_ref,
220+
target_trait_ref,
221+
errors,
222+
infcx.parameter_environment.caller_bounds);
223+
Err(())
224+
} else {
225+
debug!("fulfill_implication: an impl for {:?} specializes {:?}",
226+
source_trait_ref,
227+
target_trait_ref);
228+
229+
// Now resolve the *substitution* we built for the target earlier, replacing
230+
// the inference variables inside with whatever we got from fulfillment.
231+
Ok(infcx.resolve_type_vars_if_possible(&target_substs))
232+
}
235233
}
236234

237235
pub struct SpecializesCache {

src/librustc/ty/adjustment.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,9 @@ impl<'a, 'gcx, 'tcx> ty::TyS<'tcx> {
235235
None => {
236236
span_bug!(
237237
expr_span,
238-
"the {}th autoderef failed: {}",
238+
"the {}th autoderef for {} failed: {}",
239239
autoderef,
240+
expr_id,
240241
adjusted_ty);
241242
}
242243
}

0 commit comments

Comments
 (0)