Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 55e4e7d

Browse files
lqdcuviper
authored andcommitted
Revert "Auto merge of rust-lang#134330 - scottmcm:no-more-rvalue-len, r=matthewjasper"
This reverts commit e108481, reversing changes made to 303e8bd. (cherry picked from commit ca1c17c)
1 parent 4595e11 commit 55e4e7d

File tree

37 files changed

+272
-51
lines changed

37 files changed

+272
-51
lines changed

compiler/rustc_borrowck/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ use self::ReadOrWrite::{Activation, Read, Reservation, Write};
828828

829829
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
830830
enum ArtificialField {
831+
ArrayLength,
831832
FakeBorrow,
832833
}
833834

@@ -1345,11 +1346,16 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
13451346
);
13461347
}
13471348

1348-
&Rvalue::Discriminant(place) => {
1349+
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
1350+
let af = match *rvalue {
1351+
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1352+
Rvalue::Discriminant(..) => None,
1353+
_ => unreachable!(),
1354+
};
13491355
self.access_place(
13501356
location,
13511357
(place, span),
1352-
(Shallow(None), Read(ReadKind::Copy)),
1358+
(Shallow(af), Read(ReadKind::Copy)),
13531359
LocalMutationIsAllowed::No,
13541360
state,
13551361
);

compiler/rustc_borrowck/src/places_conflict.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ fn place_components_conflict<'tcx>(
203203
let base_ty = base.ty(body, tcx).ty;
204204

205205
match (elem, base_ty.kind(), access) {
206-
(_, _, Shallow(Some(ArtificialField::FakeBorrow))) => {
206+
(_, _, Shallow(Some(ArtificialField::ArrayLength)))
207+
| (_, _, Shallow(Some(ArtificialField::FakeBorrow))) => {
207208
// The array length is like additional fields on the
208209
// type; it does not overlap any existing data there.
209210
// Furthermore, if cannot actually be a prefix of any

compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,16 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
298298
self.consume_operand(location, op);
299299
}
300300

301-
&Rvalue::Discriminant(place) => {
301+
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
302+
let af = match rvalue {
303+
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
304+
Rvalue::Discriminant(..) => None,
305+
_ => unreachable!(),
306+
};
302307
self.access_place(
303308
location,
304309
place,
305-
(Shallow(None), Read(ReadKind::Copy)),
310+
(Shallow(af), Read(ReadKind::Copy)),
306311
LocalMutationIsAllowed::No,
307312
);
308313
}

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22212221

22222222
Rvalue::RawPtr(..)
22232223
| Rvalue::ThreadLocalRef(..)
2224+
| Rvalue::Len(..)
22242225
| Rvalue::Discriminant(..)
22252226
| Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
22262227
}
@@ -2236,6 +2237,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22362237
| Rvalue::Repeat(..)
22372238
| Rvalue::Ref(..)
22382239
| Rvalue::RawPtr(..)
2240+
| Rvalue::Len(..)
22392241
| Rvalue::Cast(..)
22402242
| Rvalue::ShallowInitBox(..)
22412243
| Rvalue::BinaryOp(..)

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,12 @@ fn codegen_stmt<'tcx>(
828828
fx.bcx.ins().nop();
829829
}
830830
}
831+
Rvalue::Len(place) => {
832+
let place = codegen_place(fx, place);
833+
let usize_layout = fx.layout_of(fx.tcx.types.usize);
834+
let len = codegen_array_len(fx, place);
835+
lval.write_cvalue(fx, CValue::by_val(len, usize_layout));
836+
}
831837
Rvalue::ShallowInitBox(ref operand, content_ty) => {
832838
let content_ty = fx.monomorphize(content_ty);
833839
let box_layout = fx.layout_of(Ty::new_box(fx.tcx, content_ty));

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use rustc_session::config::OptLevel;
1010
use rustc_span::{DUMMY_SP, Span};
1111
use tracing::{debug, instrument};
1212

13-
use super::FunctionCx;
1413
use super::operand::{OperandRef, OperandValue};
1514
use super::place::PlaceRef;
15+
use super::{FunctionCx, LocalRef};
1616
use crate::common::IntPredicate;
1717
use crate::traits::*;
1818
use crate::{MemFlags, base};
@@ -593,6 +593,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
593593
self.codegen_place_to_pointer(bx, place, mk_ptr)
594594
}
595595

596+
mir::Rvalue::Len(place) => {
597+
let size = self.evaluate_array_len(bx, place);
598+
OperandRef {
599+
val: OperandValue::Immediate(size),
600+
layout: bx.cx().layout_of(bx.tcx().types.usize),
601+
}
602+
}
603+
596604
mir::Rvalue::BinaryOp(op_with_overflow, box (ref lhs, ref rhs))
597605
if let Some(op) = op_with_overflow.overflowing_to_wrapping() =>
598606
{
@@ -792,6 +800,24 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
792800
}
793801
}
794802

803+
fn evaluate_array_len(&mut self, bx: &mut Bx, place: mir::Place<'tcx>) -> Bx::Value {
804+
// ZST are passed as operands and require special handling
805+
// because codegen_place() panics if Local is operand.
806+
if let Some(index) = place.as_local() {
807+
if let LocalRef::Operand(op) = self.locals[index] {
808+
if let ty::Array(_, n) = op.layout.ty.kind() {
809+
let n = n
810+
.try_to_target_usize(bx.tcx())
811+
.expect("expected monomorphic const in codegen");
812+
return bx.cx().const_usize(n);
813+
}
814+
}
815+
}
816+
// use common size calculation for non zero-sized types
817+
let cg_value = self.codegen_place(bx, place.as_ref());
818+
cg_value.len(bx.cx())
819+
}
820+
795821
/// Codegen an `Rvalue::RawPtr` or `Rvalue::Ref`
796822
fn codegen_place_to_pointer(
797823
&mut self,
@@ -1063,6 +1089,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10631089
mir::Rvalue::Ref(..) |
10641090
mir::Rvalue::CopyForDeref(..) |
10651091
mir::Rvalue::RawPtr(..) |
1092+
mir::Rvalue::Len(..) |
10661093
mir::Rvalue::Cast(..) | // (*)
10671094
mir::Rvalue::ShallowInitBox(..) | // (*)
10681095
mir::Rvalue::BinaryOp(..) |

compiler/rustc_const_eval/src/check_consts/check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
488488
Rvalue::Use(_)
489489
| Rvalue::CopyForDeref(..)
490490
| Rvalue::Repeat(..)
491-
| Rvalue::Discriminant(..) => {}
491+
| Rvalue::Discriminant(..)
492+
| Rvalue::Len(_) => {}
492493

493494
Rvalue::Aggregate(kind, ..) => {
494495
if let AggregateKind::Coroutine(def_id, ..) = kind.as_ref()

compiler/rustc_const_eval/src/check_consts/qualifs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,9 @@ where
230230
Q::in_any_value_of_ty(cx, rvalue.ty(cx.body, cx.tcx))
231231
}
232232

233-
Rvalue::Discriminant(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
233+
Rvalue::Discriminant(place) | Rvalue::Len(place) => {
234+
in_place::<Q, _>(cx, in_local, place.as_ref())
235+
}
234236

235237
Rvalue::CopyForDeref(place) => in_place::<Q, _>(cx, in_local, place.as_ref()),
236238

compiler/rustc_const_eval/src/check_consts/resolver.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ where
197197
| mir::Rvalue::CopyForDeref(..)
198198
| mir::Rvalue::ThreadLocalRef(..)
199199
| mir::Rvalue::Repeat(..)
200+
| mir::Rvalue::Len(..)
200201
| mir::Rvalue::BinaryOp(..)
201202
| mir::Rvalue::NullaryOp(..)
202203
| mir::Rvalue::UnaryOp(..)

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use tracing::{info, instrument, trace};
1515

1616
use super::{
1717
FnArg, FnVal, ImmTy, Immediate, InterpCx, InterpResult, Machine, MemPlaceMeta, PlaceTy,
18-
Projectable, interp_ok, throw_ub,
18+
Projectable, Scalar, interp_ok, throw_ub,
1919
};
2020
use crate::util;
2121

@@ -218,6 +218,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
218218
self.write_repeat(operand, &dest)?;
219219
}
220220

221+
Len(place) => {
222+
let src = self.eval_place(place)?;
223+
let len = src.len(self)?;
224+
self.write_scalar(Scalar::from_target_usize(len, self), &dest)?;
225+
}
226+
221227
Ref(_, borrow_kind, place) => {
222228
let src = self.eval_place(place)?;
223229
let place = self.force_allocation(&src)?;

0 commit comments

Comments
 (0)