Skip to content

Commit b247805

Browse files
committed
Auto merge of rust-lang#45473 - SimonSapin:variance-red-green, r=nikomatsakis
Remove dependency tracking for variance computation This custom tracking is now replaced by the red/green algorithm. Fix rust-lang#45471
2 parents 6e61bba + 94edd8f commit b247805

File tree

6 files changed

+11
-50
lines changed

6 files changed

+11
-50
lines changed

src/librustc/ich/impls_ty.rs

-2
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,11 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for ty::CrateVariancesMap {
755755
hcx: &mut StableHashingContext<'gcx>,
756756
hasher: &mut StableHasher<W>) {
757757
let ty::CrateVariancesMap {
758-
ref dependencies,
759758
ref variances,
760759
// This is just an irrelevant helper value.
761760
empty_variance: _,
762761
} = *self;
763762

764-
dependencies.hash_stable(hcx, hasher);
765763
variances.hash_stable(hcx, hasher);
766764
}
767765
}

src/librustc/ty/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use rustc_const_math::ConstInt;
5555
use rustc_data_structures::accumulate_vec::IntoIter as AccIntoIter;
5656
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult,
5757
HashStable};
58-
use rustc_data_structures::transitive_relation::TransitiveRelation;
5958

6059
use hir;
6160

@@ -313,11 +312,6 @@ pub enum Variance {
313312
/// `tcx.variances_of()` to get the variance for a *particular*
314313
/// item.
315314
pub struct CrateVariancesMap {
316-
/// This relation tracks the dependencies between the variance of
317-
/// various items. In particular, if `a < b`, then the variance of
318-
/// `a` depends on the sources of `b`.
319-
pub dependencies: TransitiveRelation<DefId>,
320-
321315
/// For each item with generics, maps to a vector of the variance
322316
/// of its generics. If an item has no generics, it will have no
323317
/// entry.

src/librustc_typeck/variance/README.md

+7-13
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,16 @@ into two queries:
104104
- `crate_variances` computes the variance for all items in the current crate.
105105
- `variances_of` accesses the variance for an individual reading; it
106106
works by requesting `crate_variances` and extracting the relevant data.
107-
107+
108108
If you limit yourself to reading `variances_of`, your code will only
109109
depend then on the inference inferred for that particular item.
110110

111-
Eventually, the goal is to rely on the red-green dependency management
112-
algorithm. At the moment, however, we rely instead on a hack, where
113-
`variances_of` ignores the dependencies of accessing
114-
`crate_variances` and instead computes the *correct* dependencies
115-
itself. To this end, when we build up the constraints in the system,
116-
we also built up a transitive `dependencies` relation as part of the
117-
crate map. A `(X, Y)` pair is added to the map each time we have a
118-
constraint that the variance of some inferred for the item `X` depends
119-
on the variance of some element of `Y`. This is to some extent a
120-
mirroring of the inference graph in the dependency graph. This means
121-
we can just completely ignore the fixed-point iteration, since it is
122-
just shuffling values along this graph.
111+
Ultimately, this setup relies on the red-green algorithm.
112+
In particular, every variance query ultimately depends on -- effectively --
113+
all type definitions in the entire crate (through `crate_variances`),
114+
but since most changes will not result in a change
115+
to the actual results from variance inference,
116+
the `variances_of` query will wind up being considered green after it is re-evaluated.
123117

124118
### Addendum: Variance on traits
125119

src/librustc_typeck/variance/constraints.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use syntax::ast;
2222
use rustc::hir;
2323
use rustc::hir::itemlikevisit::ItemLikeVisitor;
2424

25-
use rustc_data_structures::transitive_relation::TransitiveRelation;
2625
use rustc_data_structures::stable_hasher::StableHashingContextProvider;
2726

2827
use super::terms::*;
@@ -38,11 +37,6 @@ pub struct ConstraintContext<'a, 'tcx: 'a> {
3837
bivariant: VarianceTermPtr<'a>,
3938

4039
pub constraints: Vec<Constraint<'a>>,
41-
42-
/// This relation tracks the dependencies between the variance of
43-
/// various items. In particular, if `a < b`, then the variance of
44-
/// `a` depends on the sources of `b`.
45-
pub dependencies: TransitiveRelation<DefId>,
4640
}
4741

4842
/// Declares that the variable `decl_id` appears in a location with
@@ -63,7 +57,6 @@ pub struct Constraint<'a> {
6357
/// then while we are visiting `Bar<T>`, the `CurrentItem` would have
6458
/// the def-id and the start of `Foo`'s inferreds.
6559
pub struct CurrentItem {
66-
def_id: DefId,
6760
inferred_start: InferredIndex,
6861
}
6962

@@ -81,7 +74,6 @@ pub fn add_constraints_from_crate<'a, 'tcx>(terms_cx: TermsContext<'a, 'tcx>)
8174
invariant,
8275
bivariant,
8376
constraints: Vec::new(),
84-
dependencies: TransitiveRelation::new(),
8577
};
8678

8779
tcx.hir.krate().visit_all_item_likes(&mut constraint_cx);
@@ -201,7 +193,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
201193

202194
let id = tcx.hir.as_local_node_id(def_id).unwrap();
203195
let inferred_start = self.terms_cx.inferred_starts[&id];
204-
let current_item = &CurrentItem { def_id, inferred_start };
196+
let current_item = &CurrentItem { inferred_start };
205197
match tcx.type_of(def_id).sty {
206198
ty::TyAdt(def, _) => {
207199
// Not entirely obvious: constraints on structs/enums do not
@@ -410,12 +402,6 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
410402
return;
411403
}
412404

413-
// Add a corresponding relation into the dependencies to
414-
// indicate that the variance for `current` relies on `def_id`.
415-
if self.tcx().dep_graph.is_fully_enabled() {
416-
self.dependencies.add(current.def_id, def_id);
417-
}
418-
419405
let (local, remote) = if let Some(id) = self.tcx().hir.as_local_node_id(def_id) {
420406
(Some(self.terms_cx.inferred_starts[&id]), None)
421407
} else {

src/librustc_typeck/variance/mod.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,9 @@ fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
9494

9595
// Everything else must be inferred.
9696

97-
// Lacking red/green, we read the variances for all items here
98-
// but ignore the dependencies, then re-synthesize the ones we need.
99-
let crate_map = tcx.dep_graph.with_ignore(|| tcx.crate_variances(LOCAL_CRATE));
97+
let crate_map = tcx.crate_variances(LOCAL_CRATE);
10098
let dep_node = item_def_id.to_dep_node(tcx, DepKind::ItemVarianceConstraints);
10199
tcx.dep_graph.read(dep_node);
102-
for &dep_def_id in crate_map.dependencies.less_than(&item_def_id) {
103-
if dep_def_id.is_local() {
104-
let dep_node = dep_def_id.to_dep_node(tcx, DepKind::ItemVarianceConstraints);
105-
tcx.dep_graph.read(dep_node);
106-
} else {
107-
let dep_node = dep_def_id.to_dep_node(tcx, DepKind::ItemVariances);
108-
tcx.dep_graph.read(dep_node);
109-
}
110-
}
111100

112101
crate_map.variances.get(&item_def_id)
113102
.unwrap_or(&crate_map.empty_variance)

src/librustc_typeck/variance/solve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct SolveContext<'a, 'tcx: 'a> {
3434
}
3535

3636
pub fn solve_constraints(constraints_cx: ConstraintContext) -> ty::CrateVariancesMap {
37-
let ConstraintContext { terms_cx, dependencies, constraints, .. } = constraints_cx;
37+
let ConstraintContext { terms_cx, constraints, .. } = constraints_cx;
3838

3939
let mut solutions = vec![ty::Bivariant; terms_cx.inferred_terms.len()];
4040
for &(id, ref variances) in &terms_cx.lang_items {
@@ -53,7 +53,7 @@ pub fn solve_constraints(constraints_cx: ConstraintContext) -> ty::CrateVariance
5353
let variances = solutions_cx.create_map();
5454
let empty_variance = Rc::new(Vec::new());
5555

56-
ty::CrateVariancesMap { dependencies, variances, empty_variance }
56+
ty::CrateVariancesMap { variances, empty_variance }
5757
}
5858

5959
impl<'a, 'tcx> SolveContext<'a, 'tcx> {

0 commit comments

Comments
 (0)