Skip to content

Commit

Permalink
const_eval: Consider array length constant even if array is not
Browse files Browse the repository at this point in the history
  • Loading branch information
TheWastl committed Jun 25, 2022
1 parent ca7b7a6 commit 090872e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions compiler/rustc_const_eval/src/interpret/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use rustc_middle::mir;
use rustc_middle::mir::interpret::{InterpResult, Scalar};
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::TyKind;

use super::{InterpCx, Machine};

Expand Down Expand Up @@ -244,9 +245,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {

Len(place) => {
let src = self.eval_place(place)?;
let mplace = self.force_allocation(&src)?;
let len = mplace.len(self)?;
self.write_scalar(Scalar::from_machine_usize(len, self), &dest)?;
if let &TyKind::Array(_, len) = src.layout.ty.kind() {
let len = self.const_to_op(len, None)?;
self.copy_op(&len, &dest)?;
} else {
let mplace = self.force_allocation(&src)?;
let len = mplace.len(self)?;
self.write_scalar(Scalar::from_machine_usize(len, self), &dest)?;
}
}

AddressOf(_, place) | Ref(_, _, place) => {
Expand Down

0 comments on commit 090872e

Please sign in to comment.