Skip to content

Commit

Permalink
Auto merge of #60280 - varkor:const-param-invariance, r=eddyb
Browse files Browse the repository at this point in the history
Fix indexing issue for const parameter invariance

We were previously not taking account of the parent parameters.

r? @eddyb
cc @Zoxc
  • Loading branch information
bors committed May 1, 2019
2 parents 96ee0ba + f024196 commit 834bd19
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/librustc_typeck/variance/solve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
}
}

fn enforce_const_invariance(&self, generics: &ty::Generics, variances: &mut Vec<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, Lrc<Vec<ty::Variance>>> {
let tcx = self.terms_cx.tcx;

Expand All @@ -91,11 +107,7 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
debug!("id={} variances={:?}", id, variances);

// 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, &mut variances);

// Functions are permitted to have unused generic parameters: make those invariant.
if let ty::FnDef(..) = tcx.type_of(def_id).sty {
Expand Down

0 comments on commit 834bd19

Please sign in to comment.