Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4a413d8

Browse files
authoredOct 22, 2016
Rollup merge of rust-lang#37294 - nikomatsakis:issue-37154, r=nikomatsakis
remove keys w/ skolemized regions from proj cache when popping skolemized regions This addresses rust-lang#37154 (a regression). The projection cache was incorrectly caching the results for skolemized regions -- when we pop skolemized regions, we are supposed to drop cache keys for them (just as we remove those skolemized regions from the region inference graph). This is because those skolemized region numbers will be reused later with different meaning (and we have determined that the old ones don't leak out in any meaningful way). I did a *somewhat* aggressive fix here of only removing keys that mention the skolemized regions. One could imagine just removing all keys added since we started the skolemization (as indeed I did in my initial commit). This more aggressive fix required fixing a latent bug in `TypeFlags`, as an aside. I believe the more aggressive fix is correct; clearly there can be entries that are unrelated to the skoelemized region, and it's a shame to remove them. My one concern was that it *is* possible I believe to have some region variables that are created and related to skolemized regions, and maybe some of them could end up in the cache. However, that seems harmless enough to me-- those relations will be removed, and couldn't have impacted how trait resolution proceeded anyway (iow, the cache entry is not wrong, though it is kind of useless). r? @pnkfelix cc @arielb1
2 parents f136e9e + 483bc86 commit 4a413d8

File tree

8 files changed

+130
-53
lines changed

8 files changed

+130
-53
lines changed
 

‎src/librustc/infer/higher_ranked/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -839,5 +839,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
839839
debug!("pop_skolemized({:?})", skol_map);
840840
let skol_regions: FnvHashSet<_> = skol_map.values().cloned().collect();
841841
self.region_vars.pop_skolemized(&skol_regions, &snapshot.region_vars_snapshot);
842+
if !skol_map.is_empty() {
843+
self.projection_cache.borrow_mut().rollback_skolemized(
844+
&snapshot.projection_cache_snapshot);
845+
}
842846
}
843847
}

‎src/librustc/traits/project.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn poly_project_and_unify_type<'cx, 'gcx, 'tcx>(
167167
infcx.skolemize_late_bound_regions(&obligation.predicate, snapshot);
168168

169169
let skol_obligation = obligation.with(skol_predicate);
170-
match project_and_unify_type(selcx, &skol_obligation) {
170+
let r = match project_and_unify_type(selcx, &skol_obligation) {
171171
Ok(result) => {
172172
let span = obligation.cause.span;
173173
match infcx.leak_check(false, span, &skol_map, snapshot) {
@@ -178,7 +178,9 @@ pub fn poly_project_and_unify_type<'cx, 'gcx, 'tcx>(
178178
Err(e) => {
179179
Err(e)
180180
}
181-
}
181+
};
182+
183+
r
182184
})
183185
}
184186

@@ -1396,6 +1398,10 @@ impl<'tcx> ProjectionCache<'tcx> {
13961398
self.map.rollback_to(snapshot.snapshot);
13971399
}
13981400

1401+
pub fn rollback_skolemized(&mut self, snapshot: &ProjectionCacheSnapshot) {
1402+
self.map.partial_rollback(&snapshot.snapshot, &|k| k.has_re_skol());
1403+
}
1404+
13991405
pub fn commit(&mut self, snapshot: ProjectionCacheSnapshot) {
14001406
self.map.commit(snapshot.snapshot);
14011407
}

‎src/librustc/ty/flags.rs

+4-18
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use ty::subst::Substs;
1212
use ty::{self, Ty, TypeFlags, TypeFoldable};
1313

14+
#[derive(Debug)]
1415
pub struct FlagComputation {
1516
pub flags: TypeFlags,
1617

@@ -182,24 +183,9 @@ impl FlagComputation {
182183
}
183184

184185
fn add_region(&mut self, r: &ty::Region) {
185-
match *r {
186-
ty::ReVar(..) => {
187-
self.add_flags(TypeFlags::HAS_RE_INFER);
188-
self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
189-
}
190-
ty::ReSkolemized(..) => {
191-
self.add_flags(TypeFlags::HAS_RE_INFER);
192-
self.add_flags(TypeFlags::HAS_RE_SKOL);
193-
self.add_flags(TypeFlags::KEEP_IN_LOCAL_TCX);
194-
}
195-
ty::ReLateBound(debruijn, _) => { self.add_depth(debruijn.depth); }
196-
ty::ReEarlyBound(..) => { self.add_flags(TypeFlags::HAS_RE_EARLY_BOUND); }
197-
ty::ReStatic | ty::ReErased => {}
198-
_ => { self.add_flags(TypeFlags::HAS_FREE_REGIONS); }
199-
}
200-
201-
if !r.is_global() {
202-
self.add_flags(TypeFlags::HAS_LOCAL_NAMES);
186+
self.add_flags(r.type_flags());
187+
if let ty::ReLateBound(debruijn, _) = *r {
188+
self.add_depth(debruijn.depth);
203189
}
204190
}
205191

‎src/librustc/ty/fold.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ pub trait TypeFoldable<'tcx>: fmt::Debug + Clone {
9191
fn needs_subst(&self) -> bool {
9292
self.has_type_flags(TypeFlags::NEEDS_SUBST)
9393
}
94+
fn has_re_skol(&self) -> bool {
95+
self.has_type_flags(TypeFlags::HAS_RE_SKOL)
96+
}
9497
fn has_closure_types(&self) -> bool {
9598
self.has_type_flags(TypeFlags::HAS_TY_CLOSURE)
9699
}
@@ -632,26 +635,15 @@ struct HasTypeFlagsVisitor {
632635

633636
impl<'tcx> TypeVisitor<'tcx> for HasTypeFlagsVisitor {
634637
fn visit_ty(&mut self, t: Ty) -> bool {
635-
t.flags.get().intersects(self.flags)
638+
let flags = t.flags.get();
639+
debug!("HasTypeFlagsVisitor: t={:?} t.flags={:?} self.flags={:?}", t, flags, self.flags);
640+
flags.intersects(self.flags)
636641
}
637642

638643
fn visit_region(&mut self, r: &'tcx ty::Region) -> bool {
639-
if self.flags.intersects(ty::TypeFlags::HAS_LOCAL_NAMES) {
640-
// does this represent a region that cannot be named
641-
// in a global way? used in fulfillment caching.
642-
match *r {
643-
ty::ReStatic | ty::ReEmpty | ty::ReErased => {}
644-
_ => return true,
645-
}
646-
}
647-
if self.flags.intersects(ty::TypeFlags::HAS_RE_INFER |
648-
ty::TypeFlags::KEEP_IN_LOCAL_TCX) {
649-
match *r {
650-
ty::ReVar(_) | ty::ReSkolemized(..) => { return true }
651-
_ => {}
652-
}
653-
}
654-
false
644+
let flags = r.type_flags();
645+
debug!("HasTypeFlagsVisitor: r={:?} r.flags={:?} self.flags={:?}", r, flags, self.flags);
646+
flags.intersects(self.flags)
655647
}
656648
}
657649

‎src/librustc/ty/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ bitflags! {
477477
TypeFlags::HAS_SELF.bits |
478478
TypeFlags::HAS_TY_INFER.bits |
479479
TypeFlags::HAS_RE_INFER.bits |
480+
TypeFlags::HAS_RE_SKOL.bits |
480481
TypeFlags::HAS_RE_EARLY_BOUND.bits |
481482
TypeFlags::HAS_FREE_REGIONS.bits |
482483
TypeFlags::HAS_TY_ERR.bits |

‎src/librustc/ty/sty.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl<T> Binder<T> {
406406

407407
impl fmt::Debug for TypeFlags {
408408
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
409-
write!(f, "{}", self.bits)
409+
write!(f, "{:x}", self.bits)
410410
}
411411
}
412412

@@ -866,6 +866,35 @@ impl Region {
866866
r => r
867867
}
868868
}
869+
870+
pub fn type_flags(&self) -> TypeFlags {
871+
let mut flags = TypeFlags::empty();
872+
873+
match *self {
874+
ty::ReVar(..) => {
875+
flags = flags | TypeFlags::HAS_RE_INFER;
876+
flags = flags | TypeFlags::KEEP_IN_LOCAL_TCX;
877+
}
878+
ty::ReSkolemized(..) => {
879+
flags = flags | TypeFlags::HAS_RE_INFER;
880+
flags = flags | TypeFlags::HAS_RE_SKOL;
881+
flags = flags | TypeFlags::KEEP_IN_LOCAL_TCX;
882+
}
883+
ty::ReLateBound(..) => { }
884+
ty::ReEarlyBound(..) => { flags = flags | TypeFlags::HAS_RE_EARLY_BOUND; }
885+
ty::ReStatic | ty::ReErased => { }
886+
_ => { flags = flags | TypeFlags::HAS_FREE_REGIONS; }
887+
}
888+
889+
match *self {
890+
ty::ReStatic | ty::ReEmpty | ty::ReErased => (),
891+
_ => flags = flags | TypeFlags::HAS_LOCAL_NAMES,
892+
}
893+
894+
debug!("type_flags({:?}) = {:?}", self, flags);
895+
896+
flags
897+
}
869898
}
870899

871900
// Type utilities

‎src/librustc_data_structures/snapshot_map/mod.rs

+46-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use fnv::FnvHashMap;
1212
use std::hash::Hash;
1313
use std::ops;
14+
use std::mem;
1415

1516
#[cfg(test)]
1617
mod test;
@@ -31,6 +32,7 @@ enum UndoLog<K, V> {
3132
CommittedSnapshot,
3233
Inserted(K),
3334
Overwrite(K, V),
35+
Noop,
3436
}
3537

3638
impl<K, V> SnapshotMap<K, V>
@@ -100,24 +102,33 @@ impl<K, V> SnapshotMap<K, V>
100102
}
101103
}
102104

105+
pub fn partial_rollback<F>(&mut self,
106+
snapshot: &Snapshot,
107+
should_revert_key: &F)
108+
where F: Fn(&K) -> bool
109+
{
110+
self.assert_open_snapshot(snapshot);
111+
for i in (snapshot.len + 1..self.undo_log.len()).rev() {
112+
let reverse = match self.undo_log[i] {
113+
UndoLog::OpenSnapshot => false,
114+
UndoLog::CommittedSnapshot => false,
115+
UndoLog::Noop => false,
116+
UndoLog::Inserted(ref k) => should_revert_key(k),
117+
UndoLog::Overwrite(ref k, _) => should_revert_key(k),
118+
};
119+
120+
if reverse {
121+
let entry = mem::replace(&mut self.undo_log[i], UndoLog::Noop);
122+
self.reverse(entry);
123+
}
124+
}
125+
}
126+
103127
pub fn rollback_to(&mut self, snapshot: Snapshot) {
104128
self.assert_open_snapshot(&snapshot);
105129
while self.undo_log.len() > snapshot.len + 1 {
106-
match self.undo_log.pop().unwrap() {
107-
UndoLog::OpenSnapshot => {
108-
panic!("cannot rollback an uncommitted snapshot");
109-
}
110-
111-
UndoLog::CommittedSnapshot => {}
112-
113-
UndoLog::Inserted(key) => {
114-
self.map.remove(&key);
115-
}
116-
117-
UndoLog::Overwrite(key, old_value) => {
118-
self.map.insert(key, old_value);
119-
}
120-
}
130+
let entry = self.undo_log.pop().unwrap();
131+
self.reverse(entry);
121132
}
122133

123134
let v = self.undo_log.pop().unwrap();
@@ -127,6 +138,26 @@ impl<K, V> SnapshotMap<K, V>
127138
});
128139
assert!(self.undo_log.len() == snapshot.len);
129140
}
141+
142+
fn reverse(&mut self, entry: UndoLog<K, V>) {
143+
match entry {
144+
UndoLog::OpenSnapshot => {
145+
panic!("cannot rollback an uncommitted snapshot");
146+
}
147+
148+
UndoLog::CommittedSnapshot => {}
149+
150+
UndoLog::Inserted(key) => {
151+
self.map.remove(&key);
152+
}
153+
154+
UndoLog::Overwrite(key, old_value) => {
155+
self.map.insert(key, old_value);
156+
}
157+
158+
UndoLog::Noop => {}
159+
}
160+
}
130161
}
131162

132163
impl<'k, K, V> ops::Index<&'k K> for SnapshotMap<K, V>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 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 #37154: the problem here was that the cache
12+
// results in a false error because it was caching skolemized results
13+
// even after those skolemized regions had been popped.
14+
15+
trait Foo {
16+
fn method(&self) {}
17+
}
18+
19+
struct Wrapper<T>(T);
20+
21+
impl<T> Foo for Wrapper<T> where for<'a> &'a T: IntoIterator<Item=&'a ()> {}
22+
23+
fn f(x: Wrapper<Vec<()>>) {
24+
x.method(); // This works.
25+
x.method(); // error: no method named `method`
26+
}
27+
28+
fn main() { }

0 commit comments

Comments
 (0)
Please sign in to comment.