Skip to content

Commit ca1c17c

Browse files
committed
Revert "Auto merge of #134330 - scottmcm:no-more-rvalue-len, r=matthewjasper"
This reverts commit e108481, reversing changes made to 303e8bd.
1 parent efc2576 commit ca1c17c

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
@@ -829,6 +829,7 @@ use self::ReadOrWrite::{Activation, Read, Reservation, Write};
829829

830830
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
831831
enum ArtificialField {
832+
ArrayLength,
832833
FakeBorrow,
833834
}
834835

@@ -1338,11 +1339,16 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
13381339
);
13391340
}
13401341

1341-
&Rvalue::Discriminant(place) => {
1342+
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
1343+
let af = match *rvalue {
1344+
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1345+
Rvalue::Discriminant(..) => None,
1346+
_ => unreachable!(),
1347+
};
13421348
self.access_place(
13431349
location,
13441350
(place, span),
1345-
(Shallow(None), Read(ReadKind::Copy)),
1351+
(Shallow(af), Read(ReadKind::Copy)),
13461352
LocalMutationIsAllowed::No,
13471353
state,
13481354
);

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
@@ -300,11 +300,16 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
300300
self.consume_operand(location, op);
301301
}
302302

303-
&Rvalue::Discriminant(place) => {
303+
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
304+
let af = match rvalue {
305+
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
306+
Rvalue::Discriminant(..) => None,
307+
_ => unreachable!(),
308+
};
304309
self.access_place(
305310
location,
306311
place,
307-
(Shallow(None), Read(ReadKind::Copy)),
312+
(Shallow(af), Read(ReadKind::Copy)),
308313
LocalMutationIsAllowed::No,
309314
);
310315
}

compiler/rustc_borrowck/src/type_check/mod.rs

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

22362236
Rvalue::RawPtr(..)
22372237
| Rvalue::ThreadLocalRef(..)
2238+
| Rvalue::Len(..)
22382239
| Rvalue::Discriminant(..)
22392240
| Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
22402241
}
@@ -2250,6 +2251,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22502251
| Rvalue::Repeat(..)
22512252
| Rvalue::Ref(..)
22522253
| Rvalue::RawPtr(..)
2254+
| Rvalue::Len(..)
22532255
| Rvalue::Cast(..)
22542256
| Rvalue::ShallowInitBox(..)
22552257
| 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};
@@ -607,6 +607,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
607607
self.codegen_place_to_pointer(bx, place, mk_ptr)
608608
}
609609

610+
mir::Rvalue::Len(place) => {
611+
let size = self.evaluate_array_len(bx, place);
612+
OperandRef {
613+
val: OperandValue::Immediate(size),
614+
layout: bx.cx().layout_of(bx.tcx().types.usize),
615+
}
616+
}
617+
610618
mir::Rvalue::BinaryOp(op_with_overflow, box (ref lhs, ref rhs))
611619
if let Some(op) = op_with_overflow.overflowing_to_wrapping() =>
612620
{
@@ -806,6 +814,24 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
806814
}
807815
}
808816

817+
fn evaluate_array_len(&mut self, bx: &mut Bx, place: mir::Place<'tcx>) -> Bx::Value {
818+
// ZST are passed as operands and require special handling
819+
// because codegen_place() panics if Local is operand.
820+
if let Some(index) = place.as_local() {
821+
if let LocalRef::Operand(op) = self.locals[index] {
822+
if let ty::Array(_, n) = op.layout.ty.kind() {
823+
let n = n
824+
.try_to_target_usize(bx.tcx())
825+
.expect("expected monomorphic const in codegen");
826+
return bx.cx().const_usize(n);
827+
}
828+
}
829+
}
830+
// use common size calculation for non zero-sized types
831+
let cg_value = self.codegen_place(bx, place.as_ref());
832+
cg_value.len(bx.cx())
833+
}
834+
809835
/// Codegen an `Rvalue::RawPtr` or `Rvalue::Ref`
810836
fn codegen_place_to_pointer(
811837
&mut self,
@@ -1077,6 +1103,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10771103
mir::Rvalue::Ref(..) |
10781104
mir::Rvalue::CopyForDeref(..) |
10791105
mir::Rvalue::RawPtr(..) |
1106+
mir::Rvalue::Len(..) |
10801107
mir::Rvalue::Cast(..) | // (*)
10811108
mir::Rvalue::ShallowInitBox(..) | // (*)
10821109
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
@@ -495,7 +495,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
495495
Rvalue::Use(_)
496496
| Rvalue::CopyForDeref(..)
497497
| Rvalue::Repeat(..)
498-
| Rvalue::Discriminant(..) => {}
498+
| Rvalue::Discriminant(..)
499+
| Rvalue::Len(_) => {}
499500

500501
Rvalue::Aggregate(kind, ..) => {
501502
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)