Skip to content

Commit 385003e

Browse files
committed
Sync from rust 8e9c93d
2 parents d82b696 + 7e250da commit 385003e

File tree

3 files changed

+32
-43
lines changed

3 files changed

+32
-43
lines changed

src/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ pub(crate) fn codegen_place<'tcx>(
850850
PlaceElem::Deref => {
851851
cplace = cplace.place_deref(fx);
852852
}
853+
PlaceElem::OpaqueCast(ty) => cplace = cplace.place_opaque_cast(fx, ty),
853854
PlaceElem::Field(field, _ty) => {
854855
cplace = cplace.place_field(fx, field);
855856
}

src/constant.rs

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
55
use rustc_middle::mir::interpret::{
66
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
77
};
8-
use rustc_middle::ty::ConstKind;
98
use rustc_span::DUMMY_SP;
109

1110
use cranelift_codegen::ir::GlobalValueData;
@@ -41,36 +40,22 @@ impl ConstantCx {
4140
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
4241
let mut all_constants_ok = true;
4342
for constant in &fx.mir.required_consts {
44-
let const_ = match fx.monomorphize(constant.literal) {
45-
ConstantKind::Ty(ct) => ct,
43+
let unevaluated = match fx.monomorphize(constant.literal) {
44+
ConstantKind::Ty(_) => unreachable!(),
45+
ConstantKind::Unevaluated(uv, _) => uv,
4646
ConstantKind::Val(..) => continue,
4747
};
48-
match const_.kind() {
49-
ConstKind::Value(_) => {}
50-
ConstKind::Unevaluated(unevaluated) => {
51-
if let Err(err) =
52-
fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None)
53-
{
54-
all_constants_ok = false;
55-
match err {
56-
ErrorHandled::Reported(_) | ErrorHandled::Linted => {
57-
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
58-
}
59-
ErrorHandled::TooGeneric => {
60-
span_bug!(
61-
constant.span,
62-
"codegen encountered polymorphic constant: {:?}",
63-
err
64-
);
65-
}
66-
}
48+
49+
if let Err(err) = fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
50+
all_constants_ok = false;
51+
match err {
52+
ErrorHandled::Reported(_) | ErrorHandled::Linted => {
53+
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
54+
}
55+
ErrorHandled::TooGeneric => {
56+
span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
6757
}
6858
}
69-
ConstKind::Param(_)
70-
| ConstKind::Infer(_)
71-
| ConstKind::Bound(_, _)
72-
| ConstKind::Placeholder(_)
73-
| ConstKind::Error(_) => unreachable!("{:?}", const_),
7459
}
7560
}
7661
all_constants_ok
@@ -122,36 +107,28 @@ pub(crate) fn codegen_constant<'tcx>(
122107
fx: &mut FunctionCx<'_, '_, 'tcx>,
123108
constant: &Constant<'tcx>,
124109
) -> CValue<'tcx> {
125-
let const_ = match fx.monomorphize(constant.literal) {
126-
ConstantKind::Ty(ct) => ct,
127-
ConstantKind::Val(val, ty) => return codegen_const_value(fx, val, ty),
128-
};
129-
let const_val = match const_.kind() {
130-
ConstKind::Value(valtree) => fx.tcx.valtree_to_const_val((const_.ty(), valtree)),
131-
ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted })
110+
let (const_val, ty) = match fx.monomorphize(constant.literal) {
111+
ConstantKind::Ty(const_) => unreachable!("{:?}", const_),
112+
ConstantKind::Unevaluated(mir::UnevaluatedConst { def, substs, promoted }, ty)
132113
if fx.tcx.is_static(def.did) =>
133114
{
134115
assert!(substs.is_empty());
135116
assert!(promoted.is_none());
136117

137-
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty())).to_cvalue(fx);
118+
return codegen_static_ref(fx, def.did, fx.layout_of(ty)).to_cvalue(fx);
138119
}
139-
ConstKind::Unevaluated(unevaluated) => {
120+
ConstantKind::Unevaluated(unevaluated, ty) => {
140121
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
141-
Ok(const_val) => const_val,
122+
Ok(const_val) => (const_val, ty),
142123
Err(_) => {
143124
span_bug!(constant.span, "erroneous constant not captured by required_consts");
144125
}
145126
}
146127
}
147-
ConstKind::Param(_)
148-
| ConstKind::Infer(_)
149-
| ConstKind::Bound(_, _)
150-
| ConstKind::Placeholder(_)
151-
| ConstKind::Error(_) => unreachable!("{:?}", const_),
128+
ConstantKind::Val(val, ty) => (val, ty),
152129
};
153130

154-
codegen_const_value(fx, const_val, const_.ty())
131+
codegen_const_value(fx, const_val, ty)
155132
}
156133

157134
pub(crate) fn codegen_const_value<'tcx>(
@@ -496,6 +473,9 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
496473
.eval_for_mir(fx.tcx, ParamEnv::reveal_all())
497474
.try_to_value(fx.tcx),
498475
ConstantKind::Val(val, _) => Some(val),
476+
ConstantKind::Unevaluated(uv, _) => {
477+
fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), uv, None).ok()
478+
}
499479
},
500480
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
501481
// inside a temporary before being passed to the intrinsic requiring the const argument.

src/value_and_place.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ impl<'tcx> CPlace<'tcx> {
625625
}
626626
}
627627

628+
pub(crate) fn place_opaque_cast(
629+
self,
630+
fx: &mut FunctionCx<'_, '_, 'tcx>,
631+
ty: Ty<'tcx>,
632+
) -> CPlace<'tcx> {
633+
CPlace { inner: self.inner, layout: fx.layout_of(ty) }
634+
}
635+
628636
pub(crate) fn place_field(
629637
self,
630638
fx: &mut FunctionCx<'_, '_, 'tcx>,

0 commit comments

Comments
 (0)