Skip to content

Commit c213717

Browse files
committed
Update variances_of
1 parent de007e0 commit c213717

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

src/librustc/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,13 @@ rustc_queries! {
240240

241241
/// Get a map with the variance of every item; use `item_variance`
242242
/// instead.
243-
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap> {
243+
query crate_variances(_: CrateNum) -> Lrc<ty::CrateVariancesMap<'tcx>> {
244244
desc { "computing the variances for items in this crate" }
245245
}
246246

247247
/// Maps from def-id of a type or region parameter to its
248248
/// (inferred) variance.
249-
query variances_of(_: DefId) -> Lrc<Vec<ty::Variance>> {}
249+
query variances_of(_: DefId) -> &'tcx [ty::Variance] {}
250250
}
251251

252252
TypeChecking {

src/librustc/ty/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,11 @@ pub enum Variance {
330330
/// `tcx.variances_of()` to get the variance for a *particular*
331331
/// item.
332332
#[derive(HashStable)]
333-
pub struct CrateVariancesMap {
333+
pub struct CrateVariancesMap<'tcx> {
334334
/// For each item with generics, maps to a vector of the variance
335335
/// of its generics. If an item has no generics, it will have no
336336
/// entry.
337-
pub variances: FxHashMap<DefId, Lrc<Vec<ty::Variance>>>,
338-
339-
/// An empty vector, useful for cloning.
340-
#[stable_hasher(ignore)]
341-
pub empty_variance: Lrc<Vec<ty::Variance>>,
337+
pub variances: FxHashMap<DefId, &'tcx [ty::Variance]>,
342338
}
343339

344340
impl Variance {

src/librustc/ty/relate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub trait TypeRelation<'a, 'gcx: 'a+'tcx, 'tcx: 'a> : Sized {
6060
b_subst);
6161

6262
let opt_variances = self.tcx().variances_of(item_def_id);
63-
relate_substs(self, Some(&opt_variances), a_subst, b_subst)
63+
relate_substs(self, Some(opt_variances), a_subst, b_subst)
6464
}
6565

6666
/// Switch variance for the purpose of relating `a` and `b`.
@@ -122,7 +122,7 @@ impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
122122
}
123123

124124
pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
125-
variances: Option<&Vec<ty::Variance>>,
125+
variances: Option<&[ty::Variance]>,
126126
a_subst: SubstsRef<'tcx>,
127127
b_subst: SubstsRef<'tcx>)
128128
-> RelateResult<'tcx, SubstsRef<'tcx>>

src/librustc_metadata/cstore_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
105105
let _ = cdata;
106106
tcx.calculate_dtor(def_id, &mut |_,_| Ok(()))
107107
}
108-
variances_of => { Lrc::new(cdata.get_item_variances(def_id.index)) }
108+
variances_of => { tcx.arena.alloc_from_iter(cdata.get_item_variances(def_id.index)) }
109109
associated_item_def_ids => {
110110
let mut result = vec![];
111111
cdata.each_child_of_item(def_id.index,

src/librustc_typeck/variance/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn provide(providers: &mut Providers<'_>) {
3636
}
3737

3838
fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
39-
-> Lrc<CrateVariancesMap> {
39+
-> Lrc<CrateVariancesMap<'tcx>> {
4040
assert_eq!(crate_num, LOCAL_CRATE);
4141
let mut arena = arena::TypedArena::default();
4242
let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &mut arena);
@@ -45,7 +45,7 @@ fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
4545
}
4646

4747
fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
48-
-> Lrc<Vec<ty::Variance>> {
48+
-> &'tcx [ty::Variance] {
4949
let id = tcx.hir().as_local_hir_id(item_def_id).expect("expected local def-id");
5050
let unsupported = || {
5151
// Variance not relevant.
@@ -88,6 +88,6 @@ fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
8888

8989
let crate_map = tcx.crate_variances(LOCAL_CRATE);
9090
crate_map.variances.get(&item_def_id)
91-
.unwrap_or(&crate_map.empty_variance)
92-
.clone()
91+
.map(|p| *p)
92+
.unwrap_or(&[])
9393
}

src/librustc_typeck/variance/solve.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use rustc::hir::def_id::DefId;
99
use rustc::ty;
1010
use rustc_data_structures::fx::FxHashMap;
11-
use rustc_data_structures::sync::Lrc;
1211

1312
use super::constraints::*;
1413
use super::terms::*;
@@ -23,7 +22,9 @@ struct SolveContext<'a, 'tcx: 'a> {
2322
solutions: Vec<ty::Variance>,
2423
}
2524

26-
pub fn solve_constraints(constraints_cx: ConstraintContext<'_, '_>) -> ty::CrateVariancesMap {
25+
pub fn solve_constraints<'tcx>(
26+
constraints_cx: ConstraintContext<'_, 'tcx>
27+
) -> ty::CrateVariancesMap<'tcx> {
2728
let ConstraintContext { terms_cx, constraints, .. } = constraints_cx;
2829

2930
let mut solutions = vec![ty::Bivariant; terms_cx.inferred_terms.len()];
@@ -41,9 +42,8 @@ pub fn solve_constraints(constraints_cx: ConstraintContext<'_, '_>) -> ty::Crate
4142
};
4243
solutions_cx.solve();
4344
let variances = solutions_cx.create_map();
44-
let empty_variance = Lrc::new(Vec::new());
4545

46-
ty::CrateVariancesMap { variances, empty_variance }
46+
ty::CrateVariancesMap { variances }
4747
}
4848

4949
impl<'a, 'tcx> SolveContext<'a, 'tcx> {
@@ -78,28 +78,32 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
7878
}
7979
}
8080

81-
fn create_map(&self) -> FxHashMap<DefId, Lrc<Vec<ty::Variance>>> {
81+
fn create_map(&self) -> FxHashMap<DefId, &'tcx [ty::Variance]> {
8282
let tcx = self.terms_cx.tcx;
8383

8484
let solutions = &self.solutions;
8585
self.terms_cx.inferred_starts.iter().map(|(&id, &InferredIndex(start))| {
8686
let def_id = tcx.hir().local_def_id_from_hir_id(id);
8787
let generics = tcx.generics_of(def_id);
8888

89-
let mut variances = solutions[start..start+generics.count()].to_vec();
89+
let variances = solutions[start..start+generics.count()].iter().cloned();
9090

9191
debug!("id={} variances={:?}", id, variances);
9292

93-
// Functions can have unused type parameters: make those invariant.
94-
if let ty::FnDef(..) = tcx.type_of(def_id).sty {
95-
for variance in &mut variances {
96-
if *variance == ty::Bivariant {
97-
*variance = ty::Invariant;
93+
let variances = if let ty::FnDef(..) = tcx.type_of(def_id).sty {
94+
// Functions can have unused type parameters: make those invariant.
95+
tcx.arena.alloc_from_iter(variances.map(|variance| {
96+
if variance == ty::Bivariant {
97+
ty::Invariant
98+
} else {
99+
variance
98100
}
99-
}
100-
}
101+
}))
102+
} else {
103+
tcx.arena.alloc_from_iter(variances)
104+
};
101105

102-
(def_id, Lrc::new(variances))
106+
(def_id, &*variances)
103107
}).collect()
104108
}
105109

0 commit comments

Comments
 (0)