Skip to content

Commit 58031c7

Browse files
committed
ConstKind::Unevaluated
1 parent 08865d9 commit 58031c7

File tree

14 files changed

+53
-37
lines changed

14 files changed

+53
-37
lines changed

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2525
constant: &mir::Constant<'tcx>,
2626
) -> Result<ConstValue<'tcx>, ErrorHandled> {
2727
match self.monomorphize(&constant.literal).val {
28-
ty::ConstKind::Unevaluated(def_id, substs, promoted) => self
28+
ty::ConstKind::Unevaluated(def, substs, promoted) => self
2929
.cx
3030
.tcx()
31-
.const_eval_resolve(ty::ParamEnv::reveal_all(), def_id, substs, promoted, None)
31+
.const_eval_resolve(ty::ParamEnv::reveal_all(), def.did, substs, promoted, None)
3232
.map_err(|err| {
3333
if promoted.is_none() {
3434
self.cx

src/librustc_middle/ty/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,12 +1586,24 @@ impl<T> WithOptParam<T> {
15861586
}
15871587

15881588
impl WithOptParam<LocalDefId> {
1589+
pub fn to_global(self) -> WithOptParam<DefId> {
1590+
WithOptParam { did: self.did.to_def_id(), param_did: self.param_did }
1591+
}
1592+
15891593
pub fn ty_def_id(self) -> DefId {
15901594
if let Some(did) = self.param_did { did } else { self.did.to_def_id() }
15911595
}
15921596
}
15931597

15941598
impl WithOptParam<DefId> {
1599+
pub fn as_local(self) -> Option<WithOptParam<LocalDefId>> {
1600+
self.did.as_local().map(|did| WithOptParam { did, param_did: self.param_did })
1601+
}
1602+
1603+
pub fn is_local(self) -> bool {
1604+
self.did.is_local()
1605+
}
1606+
15951607
pub fn ty_def_id(self) -> DefId {
15961608
self.param_did.unwrap_or(self.did)
15971609
}

src/librustc_middle/ty/print/pretty.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -883,18 +883,18 @@ pub trait PrettyPrinter<'tcx>:
883883
}
884884

885885
match ct.val {
886-
ty::ConstKind::Unevaluated(did, substs, promoted) => {
886+
ty::ConstKind::Unevaluated(def, substs, promoted) => {
887887
if let Some(promoted) = promoted {
888-
p!(print_value_path(did, substs));
888+
p!(print_value_path(def.did, substs));
889889
p!(write("::{:?}", promoted));
890890
} else {
891-
match self.tcx().def_kind(did) {
891+
match self.tcx().def_kind(def.did) {
892892
DefKind::Static | DefKind::Const | DefKind::AssocConst => {
893-
p!(print_value_path(did, substs))
893+
p!(print_value_path(def.did, substs))
894894
}
895895
_ => {
896-
if did.is_local() {
897-
let span = self.tcx().def_span(did);
896+
if def.is_local() {
897+
let span = self.tcx().def_span(def.did);
898898
if let Ok(snip) = self.tcx().sess.source_map().span_to_snippet(span)
899899
{
900900
p!(write("{}", snip))

src/librustc_middle/ty/sty.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2278,7 +2278,7 @@ impl<'tcx> Const<'tcx> {
22782278
ty::ConstKind::Param(ty::ParamConst::new(index, name))
22792279
}
22802280
_ => ty::ConstKind::Unevaluated(
2281-
def.did.to_def_id(),
2281+
def.to_global(),
22822282
InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
22832283
None,
22842284
),
@@ -2347,7 +2347,7 @@ impl<'tcx> Const<'tcx> {
23472347
/// Tries to evaluate the constant if it is `Unevaluated`. If that doesn't succeed, return the
23482348
/// unevaluated constant.
23492349
pub fn eval(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> &Const<'tcx> {
2350-
if let ConstKind::Unevaluated(did, substs, promoted) = self.val {
2350+
if let ConstKind::Unevaluated(def, substs, promoted) = self.val {
23512351
use crate::mir::interpret::ErrorHandled;
23522352

23532353
let param_env_and_substs = param_env.with_reveal_all().and(substs);
@@ -2363,7 +2363,7 @@ impl<'tcx> Const<'tcx> {
23632363
// FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that
23642364
// we can call `infcx.const_eval_resolve` which handles inference variables.
23652365
let param_env_and_substs = if param_env_and_substs.needs_infer() {
2366-
tcx.param_env(did).and(InternalSubsts::identity_for_item(tcx, did))
2366+
tcx.param_env(def.did).and(InternalSubsts::identity_for_item(tcx, def.did))
23672367
} else {
23682368
param_env_and_substs
23692369
};
@@ -2373,7 +2373,7 @@ impl<'tcx> Const<'tcx> {
23732373
let (param_env, substs) = param_env_and_substs.into_parts();
23742374
// try to resolve e.g. associated constants to their definition on an impl, and then
23752375
// evaluate the const.
2376-
match tcx.const_eval_resolve(param_env, did, substs, promoted, None) {
2376+
match tcx.const_eval_resolve(param_env, def.did, substs, promoted, None) {
23772377
// NOTE(eddyb) `val` contains no lifetimes/types/consts,
23782378
// and we use the original type, so nothing from `substs`
23792379
// (which may be identity substs, see above),
@@ -2433,7 +2433,7 @@ pub enum ConstKind<'tcx> {
24332433

24342434
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
24352435
/// variants when the code is monomorphic enough for that.
2436-
Unevaluated(DefId, SubstsRef<'tcx>, Option<Promoted>),
2436+
Unevaluated(ty::WithOptParam<DefId>, SubstsRef<'tcx>, Option<Promoted>),
24372437

24382438
/// Used to hold computed value.
24392439
Value(ConstValue<'tcx>),

src/librustc_mir/borrow_check/type_check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
321321
}
322322
} else {
323323
let tcx = self.tcx();
324-
if let ty::ConstKind::Unevaluated(def_id, substs, promoted) = constant.literal.val {
324+
if let ty::ConstKind::Unevaluated(def, substs, promoted) = constant.literal.val {
325325
if let Some(promoted) = promoted {
326326
let check_err = |verifier: &mut TypeVerifier<'a, 'b, 'tcx>,
327327
promoted: &Body<'tcx>,
@@ -357,7 +357,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
357357
ConstraintCategory::Boring,
358358
self.cx.param_env.and(type_op::ascribe_user_type::AscribeUserType::new(
359359
constant.literal.ty,
360-
def_id,
360+
def.did,
361361
UserSubsts { substs, user_self_ty: None },
362362
)),
363363
) {

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
549549
let val_val = match val.val {
550550
ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
551551
ty::ConstKind::Error(_) => throw_inval!(TypeckError(ErrorReported)),
552-
ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
553-
let instance = self.resolve(def_id, substs)?;
552+
ty::ConstKind::Unevaluated(def, substs, promoted) => {
553+
let instance = self.resolve(def.did, substs)?;
554554
// We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
555555
// The reason we use `const_eval_raw` everywhere else is to prevent cycles during
556556
// validation, because validation automatically reads through any references, thus

src/librustc_mir/monomorphize/collector.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,12 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirNeighborCollector<'a, 'tcx> {
622622

623623
match substituted_constant.val {
624624
ty::ConstKind::Value(val) => collect_const_value(self.tcx, val, self.output),
625-
ty::ConstKind::Unevaluated(def_id, substs, promoted) => {
626-
match self.tcx.const_eval_resolve(param_env, def_id, substs, promoted, None) {
625+
ty::ConstKind::Unevaluated(def, substs, promoted) => {
626+
match self.tcx.const_eval_resolve(param_env, def.did, substs, promoted, None) {
627627
Ok(val) => collect_const_value(self.tcx, val, self.output),
628628
Err(ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted) => {}
629629
Err(ErrorHandled::TooGeneric) => span_bug!(
630-
self.tcx.def_span(def_id),
630+
self.tcx.def_span(def.did),
631631
"collection encountered polymorphic constant",
632632
),
633633
}

src/librustc_mir/transform/check_consts/qualifs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ where
244244
};
245245

246246
// Check the qualifs of the value of `const` items.
247-
if let ty::ConstKind::Unevaluated(def_id, _, promoted) = constant.literal.val {
247+
if let ty::ConstKind::Unevaluated(def, _, promoted) = constant.literal.val {
248248
assert!(promoted.is_none());
249249
// Don't peek inside trait associated constants.
250-
if cx.tcx.trait_of_item(def_id).is_none() {
251-
let qualifs = cx.tcx.at(constant.span).mir_const_qualif(def_id);
250+
if cx.tcx.trait_of_item(def.did).is_none() {
251+
let qualifs = cx.tcx.at(constant.span).mir_const_qualif(def.did);
252252
if !Q::in_qualifs(&qualifs) {
253253
return false;
254254
}

src/librustc_mir/transform/promote_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
955955
literal: tcx.mk_const(ty::Const {
956956
ty,
957957
val: ty::ConstKind::Unevaluated(
958-
def_id,
958+
ty::WithOptParam::dummy(def_id),
959959
InternalSubsts::for_item(tcx, def_id, |param, _| {
960960
if let ty::GenericParamDefKind::Lifetime = param.kind {
961961
tcx.lifetimes.re_erased.into()

src/librustc_mir_build/hair/cx/expr.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,11 @@ fn make_mirror_unadjusted<'a, 'tcx>(
600600
// and not the beginning of discriminants (which is always `0`)
601601
let substs = InternalSubsts::identity_for_item(cx.tcx(), did);
602602
let lhs = mk_const(cx.tcx().mk_const(ty::Const {
603-
val: ty::ConstKind::Unevaluated(did, substs, None),
603+
val: ty::ConstKind::Unevaluated(
604+
ty::WithOptParam::dummy(did),
605+
substs,
606+
None,
607+
),
604608
ty: var_ty,
605609
}));
606610
let bin = ExprKind::Binary { op: BinOp::Add, lhs, rhs: offset };
@@ -796,7 +800,7 @@ fn convert_path_expr<'a, 'tcx>(
796800
debug!("convert_path_expr: (const) user_ty={:?}", user_ty);
797801
ExprKind::Literal {
798802
literal: cx.tcx.mk_const(ty::Const {
799-
val: ty::ConstKind::Unevaluated(def_id, substs, None),
803+
val: ty::ConstKind::Unevaluated(ty::WithOptParam::dummy(def_id), substs, None),
800804
ty: cx.tables().node_type(expr.hir_id),
801805
}),
802806
user_ty,

0 commit comments

Comments
 (0)