Skip to content

Commit a987e1a

Browse files
committed
Const prop is only interested in immediate constants right now
1 parent d3fa68e commit a987e1a

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ pub(super) fn from_known_layout<'tcx>(
359359
impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
360360
/// Try reading a value in memory; this is interesting particularily for ScalarPair.
361361
/// Return None if the layout does not permit loading this as a value.
362-
pub(super) fn try_read_value_from_mplace(
362+
pub(crate) fn try_read_value_from_mplace(
363363
&self,
364364
mplace: MPlaceTy<'tcx, M::PointerTag>,
365365
) -> EvalResult<'tcx, Option<Value<M::PointerTag>>> {
@@ -403,7 +403,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M>
403403
/// Note that for a given layout, this operation will either always fail or always
404404
/// succeed! Whether it succeeds depends on whether the layout can be represented
405405
/// in a `Value`, not on which data is stored there currently.
406-
pub(crate) fn try_read_value(
406+
pub(super)fn try_read_value(
407407
&self,
408408
src: OpTy<'tcx, M::PointerTag>,
409409
) -> EvalResult<'tcx, Result<Value<M::PointerTag>, MemPlace<M::PointerTag>>> {

src/librustc_mir/transform/const_prop.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -259,24 +259,28 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
259259
source_info: SourceInfo,
260260
) -> Option<Const<'tcx>> {
261261
self.ecx.tcx.span = source_info.span;
262-
match self.ecx.const_to_mplace(c.literal) {
263-
Ok(mplace) => {
264-
Some((mplace.into(), c.span))
265-
},
266-
Err(error) => {
267-
let (stacktrace, span) = self.ecx.generate_stacktrace(None);
268-
let err = ConstEvalErr {
269-
span,
270-
error,
271-
stacktrace,
272-
};
273-
err.report_as_error(
274-
self.tcx.at(source_info.span),
275-
"could not evaluate constant",
276-
);
277-
None
262+
let error = match self.ecx.const_to_mplace(c.literal) {
263+
Ok(mplacety) => match self.ecx.try_read_value_from_mplace(mplacety) {
264+
Err(error) => error,
265+
Ok(Some(val)) => return Some((OpTy {
266+
op: interpret::Operand::Immediate(val),
267+
layout: mplacety.layout,
268+
}, c.span)),
269+
Ok(None) => return Some((mplacety.into(), c.span)),
278270
},
279-
}
271+
Err(error) => error,
272+
};
273+
let (stacktrace, span) = self.ecx.generate_stacktrace(None);
274+
let err = ConstEvalErr {
275+
span,
276+
error,
277+
stacktrace,
278+
};
279+
err.report_as_error(
280+
self.tcx.at(source_info.span),
281+
"could not evaluate constant",
282+
);
283+
None
280284
}
281285

282286
fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {

0 commit comments

Comments
 (0)