Skip to content

Commit f024196

Browse files
committed
Fix indexing issue for const parameter invariance
1 parent bd31c39 commit f024196

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/librustc_typeck/variance/solve.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
7878
}
7979
}
8080

81+
fn enforce_const_invariance(&self, generics: &ty::Generics, variances: &mut Vec<ty::Variance>) {
82+
let tcx = self.terms_cx.tcx;
83+
84+
// Make all const parameters invariant.
85+
for param in generics.params.iter() {
86+
if let ty::GenericParamDefKind::Const = param.kind {
87+
variances[param.index as usize] = ty::Invariant;
88+
}
89+
}
90+
91+
// Make all the const parameters in the parent invariant (recursively).
92+
if let Some(def_id) = generics.parent {
93+
self.enforce_const_invariance(tcx.generics_of(def_id), variances);
94+
}
95+
}
96+
8197
fn create_map(&self) -> FxHashMap<DefId, Lrc<Vec<ty::Variance>>> {
8298
let tcx = self.terms_cx.tcx;
8399

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

93109
// Const parameters are always invariant.
94-
for (idx, param) in generics.params.iter().enumerate() {
95-
if let ty::GenericParamDefKind::Const = param.kind {
96-
variances[idx] = ty::Invariant;
97-
}
98-
}
110+
self.enforce_const_invariance(generics, &mut variances);
99111

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

0 commit comments

Comments
 (0)