Skip to content

Commit 54b935b

Browse files
varkoryodaldevoid
andcommitted
Handle const generics elsewhere
Co-Authored-By: Gabriel Smith <[email protected]>
1 parent c236c24 commit 54b935b

File tree

9 files changed

+48
-12
lines changed

9 files changed

+48
-12
lines changed

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
19741974
object_lifetime_default,
19751975
..
19761976
} => Some(object_lifetime_default),
1977-
GenericParamDefKind::Lifetime => None,
1977+
GenericParamDefKind::Lifetime | GenericParamDefKind::Const => None,
19781978
})
19791979
.collect()
19801980
})

src/librustc/traits/error_reporting.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
389389

390390
for param in generics.params.iter() {
391391
let value = match param.kind {
392-
GenericParamDefKind::Type {..} => {
392+
GenericParamDefKind::Type { .. } |
393+
GenericParamDefKind::Const => {
393394
trait_ref.substs[param.index as usize].to_string()
394395
},
395396
GenericParamDefKind::Lifetime => continue,

src/librustc/traits/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ fn vtable_methods<'a, 'tcx>(
10101010
InternalSubsts::for_item(tcx, def_id, |param, _|
10111011
match param.kind {
10121012
GenericParamDefKind::Lifetime => tcx.types.re_erased.into(),
1013-
GenericParamDefKind::Type {..} => {
1013+
GenericParamDefKind::Type { .. } |
1014+
GenericParamDefKind::Const => {
10141015
trait_ref.substs[param.index as usize]
10151016
}
10161017
}

src/librustc/traits/object_safety.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
310310
}
311311

312312
// We can't monomorphize things like `fn foo<A>(...)`.
313-
if self.generics_of(method.def_id).own_counts().types != 0 {
313+
let own_counts = self.generics_of(method.def_id).own_counts();
314+
if own_counts.types + own_counts.consts != 0 {
314315
return Some(MethodViolationCode::Generic);
315316
}
316317

src/librustc/traits/on_unimplemented.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
280280
let generics = tcx.generics_of(trait_ref.def_id);
281281
let generic_map = generics.params.iter().filter_map(|param| {
282282
let value = match param.kind {
283-
GenericParamDefKind::Type {..} => {
283+
GenericParamDefKind::Type { .. } |
284+
GenericParamDefKind::Const => {
284285
trait_ref.substs[param.index as usize].to_string()
285286
},
286287
GenericParamDefKind::Lifetime => return None

src/librustc/ty/relate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ impl<'tcx> Relate<'tcx> for Kind<'tcx> {
705705
(UnpackedKind::Type(unpacked), x) => {
706706
bug!("impossible case reached: can't relate: {:?} with {:?}", unpacked, x)
707707
}
708+
(UnpackedKind::Const(_), _) => {
709+
unimplemented!() // FIXME(const_generics)
710+
}
708711
}
709712
}
710713
}

src/librustc/ty/util.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::ty::subst::{Subst, InternalSubsts, SubstsRef, UnpackedKind};
1212
use crate::ty::query::TyCtxtAt;
1313
use crate::ty::TyKind::*;
1414
use crate::ty::layout::{Integer, IntegerExt};
15+
use crate::mir::interpret::ConstValue;
1516
use crate::util::common::ErrorReported;
1617
use crate::middle::lang_items;
1718

@@ -495,8 +496,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
495496
}) => {
496497
!impl_generics.type_param(pt, self).pure_wrt_drop
497498
}
498-
UnpackedKind::Lifetime(_) | UnpackedKind::Type(_) => {
499-
// not a type or region param - this should be reported
499+
UnpackedKind::Const(&ty::LazyConst::Evaluated(ty::Const {
500+
val: ConstValue::Param(ref pc),
501+
..
502+
})) => {
503+
!impl_generics.const_param(pc, self).pure_wrt_drop
504+
}
505+
UnpackedKind::Lifetime(_) |
506+
UnpackedKind::Type(_) |
507+
UnpackedKind::Const(_) => {
508+
// Not a type, const or region param: this should be reported
500509
// as an error.
501510
false
502511
}
@@ -587,15 +596,18 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
587596
Some(ty::Binder::bind(env_ty))
588597
}
589598

590-
/// Given the `DefId` of some item that has no type parameters, make
599+
/// Given the `DefId` of some item that has no type or const parameters, make
591600
/// a suitable "empty substs" for it.
592601
pub fn empty_substs_for_def_id(self, item_def_id: DefId) -> SubstsRef<'tcx> {
593602
InternalSubsts::for_item(self, item_def_id, |param, _| {
594603
match param.kind {
595604
GenericParamDefKind::Lifetime => self.types.re_erased.into(),
596-
GenericParamDefKind::Type {..} => {
605+
GenericParamDefKind::Type { .. } => {
597606
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
598607
}
608+
GenericParamDefKind::Const { .. } => {
609+
bug!("empty_substs_for_def_id: {:?} has const parameters", item_def_id)
610+
}
599611
}
600612
})
601613
}

src/librustc_privacy/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,12 +748,15 @@ impl<'a, 'tcx> ReachEverythingInTheInterfaceVisitor<'_, 'a, 'tcx> {
748748
fn generics(&mut self) -> &mut Self {
749749
for param in &self.ev.tcx.generics_of(self.item_def_id).params {
750750
match param.kind {
751+
GenericParamDefKind::Lifetime => {}
751752
GenericParamDefKind::Type { has_default, .. } => {
752753
if has_default {
753754
self.visit(self.ev.tcx.type_of(param.def_id));
754755
}
755756
}
756-
GenericParamDefKind::Lifetime => {}
757+
GenericParamDefKind::Const => {
758+
self.visit(self.ev.tcx.type_of(param.def_id));
759+
}
757760
}
758761
}
759762
self
@@ -1517,12 +1520,15 @@ impl<'a, 'tcx: 'a> SearchInterfaceForPrivateItemsVisitor<'a, 'tcx> {
15171520
fn generics(&mut self) -> &mut Self {
15181521
for param in &self.tcx.generics_of(self.item_def_id).params {
15191522
match param.kind {
1523+
GenericParamDefKind::Lifetime => {}
15201524
GenericParamDefKind::Type { has_default, .. } => {
15211525
if has_default {
15221526
self.visit(self.tcx.type_of(param.def_id));
15231527
}
15241528
}
1525-
GenericParamDefKind::Lifetime => {}
1529+
GenericParamDefKind::Const => {
1530+
self.visit(self.tcx.type_of(param.def_id));
1531+
}
15261532
}
15271533
}
15281534
self

src/librustc_traits/chalk_context/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ use rustc::traits::{
3232
InEnvironment,
3333
ChalkCanonicalGoal,
3434
};
35-
use rustc::ty::{self, TyCtxt};
35+
use rustc::ty::{self, TyCtxt, InferConst};
3636
use rustc::ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
3737
use rustc::ty::query::Providers;
3838
use rustc::ty::subst::{Kind, UnpackedKind};
3939
use rustc_data_structures::sync::Lrc;
40+
use rustc::mir::interpret::ConstValue;
4041
use syntax_pos::DUMMY_SP;
4142

4243
use std::fmt::{self, Debug};
@@ -287,6 +288,16 @@ impl context::ContextOps<ChalkArenas<'gcx>> for ChalkContext<'cx, 'gcx> {
287288
}
288289
_ => false,
289290
},
291+
UnpackedKind::Const(ct) => match ct {
292+
ty::LazyConst::Evaluated(ty::Const {
293+
val: ConstValue::Infer(InferConst::Canonical(debruijn, bound_ct)),
294+
..
295+
}) => {
296+
debug_assert_eq!(*debruijn, ty::INNERMOST);
297+
cvar == *bound_ct
298+
}
299+
_ => false,
300+
}
290301
})
291302
}
292303

0 commit comments

Comments
 (0)