Skip to content

Commit 15033a9

Browse files
committed
Const parameters should impose no variance constraints
1 parent 3b7dd97 commit 15033a9

File tree

3 files changed

+16
-29
lines changed

3 files changed

+16
-29
lines changed

src/librustc_typeck/variance/constraints.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! We walk the set of items and, for each member, generate new constraints.
55
66
use hir::def_id::DefId;
7-
use rustc::mir::interpret::ConstValue;
87
use rustc::ty::subst::{SubstsRef, UnpackedKind};
98
use rustc::ty::{self, Ty, TyCtxt};
109
use rustc::hir;
@@ -239,8 +238,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
239238
UnpackedKind::Type(ty) => {
240239
self.add_constraints_from_ty(current, ty, variance_i)
241240
}
242-
UnpackedKind::Const(ct) => {
243-
self.add_constraints_from_const(current, ct, variance_i)
241+
UnpackedKind::Const(_) => {
242+
// Consts impose no constraints.
244243
}
245244
}
246245
}
@@ -275,9 +274,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
275274
self.add_constraints_from_mt(current, &ty::TypeAndMut { ty, mutbl }, variance);
276275
}
277276

278-
ty::Array(typ, len) => {
277+
ty::Array(typ, _) => {
279278
self.add_constraints_from_ty(current, typ, variance);
280-
self.add_constraints_from_const(current, len, variance);
281279
}
282280

283281
ty::Slice(typ) => {
@@ -395,8 +393,8 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
395393
UnpackedKind::Type(ty) => {
396394
self.add_constraints_from_ty(current, ty, variance_i)
397395
}
398-
UnpackedKind::Const(ct) => {
399-
self.add_constraints_from_const(current, ct, variance_i)
396+
UnpackedKind::Const(_) => {
397+
// Consts impose no constraints.
400398
}
401399
}
402400
}
@@ -449,24 +447,6 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
449447
}
450448
}
451449

452-
fn add_constraints_from_const(
453-
&mut self,
454-
current: &CurrentItem,
455-
ct: &ty::Const<'tcx>,
456-
variance: VarianceTermPtr<'a>
457-
) {
458-
debug!(
459-
"add_constraints_from_const(ct={:?}, variance={:?})",
460-
ct,
461-
variance
462-
);
463-
464-
self.add_constraints_from_ty(current, ct.ty, variance);
465-
if let ConstValue::Param(ref data) = ct.val {
466-
self.add_constraint(current, data.index, variance);
467-
}
468-
}
469-
470450
/// Adds constraints appropriate for a mutability-type pair
471451
/// appearing in a context with ambient variance `variance`
472452
fn add_constraints_from_mt(&mut self,

src/librustc_typeck/variance/solve.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,19 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
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);
88+
let count = generics.count();
8889

89-
let mut variances = solutions[start..start+generics.count()].to_vec();
90-
90+
let mut variances = solutions[start..(start + count)].to_vec();
9191
debug!("id={} variances={:?}", id, variances);
9292

93-
// Functions can have unused type parameters: make those invariant.
93+
// 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+
}
99+
100+
// Functions are permitted to have unused generic parameters: make those invariant.
94101
if let ty::FnDef(..) = tcx.type_of(def_id).sty {
95102
for variance in &mut variances {
96103
if *variance == ty::Bivariant {

src/librustc_typeck/variance/terms.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'tcx> TermsContext<'a, 'tcx> {
119119
// for a particular item are assigned continuous indices.
120120

121121
let arena = self.arena;
122-
self.inferred_terms.extend((start..start+count).map(|i| {
122+
self.inferred_terms.extend((start..(start + count)).map(|i| {
123123
&*arena.alloc(InferredTerm(InferredIndex(i)))
124124
}));
125125
}

0 commit comments

Comments
 (0)