Skip to content

Use references for variances_of #60287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ impl<'tcx> Arena<'tcx> {
}
}

#[inline]
pub fn alloc_slice<T: Copy>(&self, value: &[T]) -> &mut [T] {
if value.len() == 0 {
return &mut []
}
self.dropless.alloc_slice(value)
}

pub fn alloc_from_iter<
T: ArenaAllocatable,
I: IntoIterator<Item = T>
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,13 @@ rustc_queries! {

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

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

TypeChecking {
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,11 @@ pub enum Variance {
/// `tcx.variances_of()` to get the variance for a *particular*
/// item.
#[derive(HashStable)]
pub struct CrateVariancesMap {
pub struct CrateVariancesMap<'tcx> {
/// For each item with generics, maps to a vector of the variance
/// of its generics. If an item has no generics, it will have no
/// entry.
pub variances: FxHashMap<DefId, Lrc<Vec<ty::Variance>>>,

/// An empty vector, useful for cloning.
#[stable_hasher(ignore)]
pub empty_variance: Lrc<Vec<ty::Variance>>,
pub variances: FxHashMap<DefId, &'tcx [ty::Variance]>,
}

impl Variance {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub trait TypeRelation<'a, 'gcx: 'a+'tcx, 'tcx: 'a> : Sized {
b_subst);

let opt_variances = self.tcx().variances_of(item_def_id);
relate_substs(self, Some(&opt_variances), a_subst, b_subst)
relate_substs(self, Some(opt_variances), a_subst, b_subst)
}

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

pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
variances: Option<&Vec<ty::Variance>>,
variances: Option<&[ty::Variance]>,
a_subst: SubstsRef<'tcx>,
b_subst: SubstsRef<'tcx>)
-> RelateResult<'tcx, SubstsRef<'tcx>>
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
let _ = cdata;
tcx.calculate_dtor(def_id, &mut |_,_| Ok(()))
}
variances_of => { Lrc::new(cdata.get_item_variances(def_id.index)) }
variances_of => { tcx.arena.alloc_from_iter(cdata.get_item_variances(def_id.index)) }
associated_item_def_ids => {
let mut result = vec![];
cdata.each_child_of_item(def_id.index,
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_typeck/variance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn provide(providers: &mut Providers<'_>) {
}

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

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

let crate_map = tcx.crate_variances(LOCAL_CRATE);
crate_map.variances.get(&item_def_id)
.unwrap_or(&crate_map.empty_variance)
.clone()
.map(|p| *p)
.unwrap_or(&[])
}
39 changes: 25 additions & 14 deletions src/librustc_typeck/variance/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use rustc::hir::def_id::DefId;
use rustc::ty;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc;

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

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

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

ty::CrateVariancesMap { variances, empty_variance }
ty::CrateVariancesMap { variances }
}

impl<'a, 'tcx> SolveContext<'a, 'tcx> {
Expand Down Expand Up @@ -78,7 +78,23 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
}
}

fn create_map(&self) -> FxHashMap<DefId, Lrc<Vec<ty::Variance>>> {
fn enforce_const_invariance(&self, generics: &ty::Generics, variances: &mut [ty::Variance]) {
let tcx = self.terms_cx.tcx;

// Make all const parameters invariant.
for param in generics.params.iter() {
if let ty::GenericParamDefKind::Const = param.kind {
variances[param.index as usize] = ty::Invariant;
}
}

// Make all the const parameters in the parent invariant (recursively).
if let Some(def_id) = generics.parent {
self.enforce_const_invariance(tcx.generics_of(def_id), variances);
}
}

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

let solutions = &self.solutions;
Expand All @@ -87,26 +103,21 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
let generics = tcx.generics_of(def_id);
let count = generics.count();

let mut variances = solutions[start..(start + count)].to_vec();
debug!("id={} variances={:?}", id, variances);
let variances = tcx.arena.alloc_slice(&solutions[start..(start + count)]);

// Const parameters are always invariant.
for (idx, param) in generics.params.iter().enumerate() {
if let ty::GenericParamDefKind::Const = param.kind {
variances[idx] = ty::Invariant;
}
}
self.enforce_const_invariance(generics, variances);

// Functions are permitted to have unused generic parameters: make those invariant.
if let ty::FnDef(..) = tcx.type_of(def_id).sty {
for variance in &mut variances {
for variance in variances.iter_mut() {
if *variance == ty::Bivariant {
*variance = ty::Invariant;
}
}
}

(def_id, Lrc::new(variances))
(def_id, &*variances)
}).collect()
}

Expand Down