Skip to content

Commit f761c41

Browse files
varkoryodaldevoid
andcommitted
Make a lazy const from a const param
Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
1 parent 0d1c9c0 commit f761c41

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/librustc_mir/hair/cx/expr.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::hair::cx::to_ref::ToRef;
55
use crate::hair::util::UserAnnotatedTyHelpers;
66
use rustc_data_structures::indexed_vec::Idx;
77
use rustc::hir::def::{Def, CtorKind};
8-
use rustc::mir::interpret::{GlobalId, ErrorHandled};
8+
use rustc::mir::interpret::{GlobalId, ErrorHandled, ConstValue};
99
use rustc::ty::{self, AdtKind, Ty};
1010
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
1111
use rustc::ty::cast::CastKind as TyCastKind;
@@ -699,7 +699,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
699699
};
700700

701701
let source = if let Some((did, offset, var_ty)) = var {
702-
let mk_const = |literal| Expr {
702+
let mk_lazy_const = |literal| Expr {
703703
temp_lifetime,
704704
ty: var_ty,
705705
span: expr.span,
@@ -708,7 +708,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
708708
user_ty: None
709709
},
710710
}.to_ref();
711-
let offset = mk_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
711+
let offset = mk_lazy_const(ty::LazyConst::Evaluated(ty::Const::from_bits(
712712
cx.tcx,
713713
offset as u128,
714714
cx.param_env.and(var_ty),
@@ -718,7 +718,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
718718
// in case we are offsetting from a computed discriminant
719719
// and not the beginning of discriminants (which is always `0`)
720720
let substs = InternalSubsts::identity_for_item(cx.tcx(), did);
721-
let lhs = mk_const(ty::LazyConst::Unevaluated(did, substs));
721+
let lhs = mk_lazy_const(ty::LazyConst::Unevaluated(did, substs));
722722
let bin = ExprKind::Binary {
723723
op: BinOp::Add,
724724
lhs,
@@ -925,7 +925,26 @@ fn convert_path_expr<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
925925
))),
926926
user_ty,
927927
}
928-
},
928+
}
929+
930+
Def::ConstParam(def_id) => {
931+
let node_id = cx.tcx.hir().as_local_node_id(def_id).unwrap();
932+
let item_id = cx.tcx.hir().get_parent_node(node_id);
933+
let item_def_id = cx.tcx.hir().local_def_id(item_id);
934+
let generics = cx.tcx.generics_of(item_def_id);
935+
let index = generics.param_def_id_to_index[&cx.tcx.hir().local_def_id(node_id)];
936+
let name = cx.tcx.hir().name(node_id).as_interned_str();
937+
let val = ConstValue::Param(ty::ParamConst::new(index, name));
938+
ExprKind::Literal {
939+
literal: cx.tcx.mk_lazy_const(
940+
ty::LazyConst::Evaluated(ty::Const {
941+
val,
942+
ty: cx.tables().node_type(expr.hir_id),
943+
})
944+
),
945+
user_ty: None,
946+
}
947+
}
929948

930949
Def::Const(def_id) |
931950
Def::AssociatedConst(def_id) => {

0 commit comments

Comments
 (0)