Skip to content

Commit 6353e30

Browse files
committed
clear obligations-added flag with nested fulfillcx
This flag is a debugging measure designed to detect cases where we start a snapshot, create type variables, register obligations involving those type variables in the fulfillment cx, and then have to unroll the snapshot, leaving "dangling type variables" behind. HOWEVER, in some cases the flag is wrong. In particular, we sometimes create a "mini-fulfilment-cx" in which we enroll obligations. As long as this fulfillment cx is fully drained before we return, this is not a problem, as there won't be any escaping obligations in the main cx. So we add a fn to save/restore the flag.
1 parent c87ba3f commit 6353e30

File tree

4 files changed

+104
-22
lines changed

4 files changed

+104
-22
lines changed

src/librustc/infer/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,33 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
830830
result.map(move |t| InferOk { value: t, obligations: fields.obligations })
831831
}
832832

833+
// Clear the "obligations in snapshot" flag, invoke the closure,
834+
// then restore the flag to its original value. This flag is a
835+
// debugging measure designed to detect cases where we start a
836+
// snapshot, create type variables, register obligations involving
837+
// those type variables in the fulfillment cx, and then have to
838+
// unroll the snapshot, leaving "dangling type variables" behind.
839+
// In such cases, the flag will be set by the fulfillment cx, and
840+
// an assertion will fail when rolling the snapshot back. Very
841+
// useful, much better than grovelling through megabytes of
842+
// RUST_LOG output.
843+
//
844+
// HOWEVER, in some cases the flag is wrong. In particular, we
845+
// sometimes create a "mini-fulfilment-cx" in which we enroll
846+
// obligations. As long as this fulfillment cx is fully drained
847+
// before we return, this is not a problem, as there won't be any
848+
// escaping obligations in the main cx. In those cases, you can
849+
// use this function.
850+
pub fn save_and_restore_obligations_in_snapshot_flag<F, R>(&self, func: F) -> R
851+
where F: FnOnce(&Self) -> R
852+
{
853+
let flag = self.obligations_in_snapshot.get();
854+
self.obligations_in_snapshot.set(false);
855+
let result = func(self);
856+
self.obligations_in_snapshot.set(flag);
857+
result
858+
}
859+
833860
fn start_snapshot(&self) -> CombinedSnapshot {
834861
debug!("start_snapshot()");
835862

src/librustc/traits/specialize/mod.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -203,32 +203,34 @@ fn fulfill_implication<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>,
203203
// attempt to prove all of the predicates for impl2 given those for impl1
204204
// (which are packed up in penv)
205205

206-
let mut fulfill_cx = FulfillmentContext::new();
207-
for oblig in obligations.into_iter() {
208-
fulfill_cx.register_predicate_obligation(&infcx, oblig);
209-
}
210-
match fulfill_cx.select_all_or_error(infcx) {
211-
Err(errors) => {
212-
// no dice!
213-
debug!("fulfill_implication: for impls on {:?} and {:?}, could not fulfill: {:?} given \
214-
{:?}",
215-
source_trait_ref,
216-
target_trait_ref,
217-
errors,
218-
infcx.parameter_environment.caller_bounds);
219-
Err(())
206+
infcx.save_and_restore_obligations_in_snapshot_flag(|infcx| {
207+
let mut fulfill_cx = FulfillmentContext::new();
208+
for oblig in obligations.into_iter() {
209+
fulfill_cx.register_predicate_obligation(&infcx, oblig);
220210
}
211+
match fulfill_cx.select_all_or_error(infcx) {
212+
Err(errors) => {
213+
// no dice!
214+
debug!("fulfill_implication: for impls on {:?} and {:?}, \
215+
could not fulfill: {:?} given {:?}",
216+
source_trait_ref,
217+
target_trait_ref,
218+
errors,
219+
infcx.parameter_environment.caller_bounds);
220+
Err(())
221+
}
221222

222-
Ok(()) => {
223-
debug!("fulfill_implication: an impl for {:?} specializes {:?}",
224-
source_trait_ref,
225-
target_trait_ref);
223+
Ok(()) => {
224+
debug!("fulfill_implication: an impl for {:?} specializes {:?}",
225+
source_trait_ref,
226+
target_trait_ref);
226227

227-
// Now resolve the *substitution* we built for the target earlier, replacing
228-
// the inference variables inside with whatever we got from fulfillment.
229-
Ok(infcx.resolve_type_vars_if_possible(&target_substs))
228+
// Now resolve the *substitution* we built for the target earlier, replacing
229+
// the inference variables inside with whatever we got from fulfillment.
230+
Ok(infcx.resolve_type_vars_if_possible(&target_substs))
231+
}
230232
}
231-
}
233+
})
232234
}
233235

234236
pub struct SpecializesCache {
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #36053. ICE was caused due to obligations
12+
// being added to a special, dedicated fulfillment cx during
13+
// a probe.
14+
15+
use std::iter::once;
16+
fn main() {
17+
once::<&str>("str").fuse().filter(|a: &str| true).count();
18+
//~^ ERROR no method named `count`
19+
//~| ERROR E0281
20+
//~| ERROR E0281
21+
}

src/test/run-pass/issue-36053.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #36053. ICE was caused due to obligations being
12+
// added to a special, dedicated fulfillment cx during a
13+
// probe. Problem seems to be related to the particular definition of
14+
// `FusedIterator` in std but I was not able to isolate that into an
15+
// external crate.
16+
17+
#![feature(fused)]
18+
use std::iter::FusedIterator;
19+
20+
struct Thing<'a>(&'a str);
21+
impl<'a> Iterator for Thing<'a> {
22+
type Item = &'a str;
23+
fn next(&mut self) -> Option<&'a str> {
24+
None
25+
}
26+
}
27+
28+
impl<'a> FusedIterator for Thing<'a> {}
29+
30+
fn main() {
31+
Thing("test").fuse().filter(|_| true).count();
32+
}

0 commit comments

Comments
 (0)