From f024196dd5468aeb684ac7437dc51dbf84dab67d Mon Sep 17 00:00:00 2001 From: varkor Date: Thu, 25 Apr 2019 20:16:11 +0100 Subject: [PATCH] Fix indexing issue for const parameter invariance --- src/librustc_typeck/variance/solve.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/librustc_typeck/variance/solve.rs b/src/librustc_typeck/variance/solve.rs index b783bbfad16e9..51a1d088ddccb 100644 --- a/src/librustc_typeck/variance/solve.rs +++ b/src/librustc_typeck/variance/solve.rs @@ -78,6 +78,22 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> { } } + fn enforce_const_invariance(&self, generics: &ty::Generics, variances: &mut Vec) { + 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>> { let tcx = self.terms_cx.tcx; @@ -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 {