Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ca1c17c

Browse files
committedJan 18, 2025
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

37 files changed

+272
-51
lines changed
 

‎compiler/rustc_borrowck/src/lib.rs

+8-2
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

+2-1
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

+7-2
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

+2
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

+6
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

+28-1
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

+2-1
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

+3-1
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

+1
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

+7-1
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)?;

‎compiler/rustc_middle/src/mir/pretty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
10681068
pretty_print_const(b, fmt, false)?;
10691069
write!(fmt, "]")
10701070
}
1071+
Len(ref a) => write!(fmt, "Len({a:?})"),
10711072
Cast(ref kind, ref place, ref ty) => {
10721073
with_no_trimmed_paths!(write!(fmt, "{place:?} as {ty} ({kind:?})"))
10731074
}

‎compiler/rustc_middle/src/mir/statement.rs

+1
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ impl<'tcx> Rvalue<'tcx> {
424424
| Rvalue::Ref(_, _, _)
425425
| Rvalue::ThreadLocalRef(_)
426426
| Rvalue::RawPtr(_, _)
427+
| Rvalue::Len(_)
427428
| Rvalue::Cast(
428429
CastKind::IntToInt
429430
| CastKind::FloatToInt

‎compiler/rustc_middle/src/mir/syntax.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,16 @@ pub enum Rvalue<'tcx> {
13511351
/// model.
13521352
RawPtr(Mutability, Place<'tcx>),
13531353

1354+
/// Yields the length of the place, as a `usize`.
1355+
///
1356+
/// If the type of the place is an array, this is the array length. For slices (`[T]`, not
1357+
/// `&[T]`) this accesses the place's metadata to determine the length. This rvalue is
1358+
/// ill-formed for places of other types.
1359+
///
1360+
/// This cannot be a `UnOp(PtrMetadata, _)` because that expects a value, and we only
1361+
/// have a place, and `UnOp(PtrMetadata, RawPtr(place))` is not a thing.
1362+
Len(Place<'tcx>),
1363+
13541364
/// Performs essentially all of the casts that can be performed via `as`.
13551365
///
13561366
/// This allows for casts from/to a variety of types.

‎compiler/rustc_middle/src/mir/tcx.rs

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl<'tcx> Rvalue<'tcx> {
210210
let place_ty = place.ty(local_decls, tcx).ty;
211211
Ty::new_ptr(tcx, place_ty, mutability)
212212
}
213+
Rvalue::Len(..) => tcx.types.usize,
213214
Rvalue::Cast(.., ty) => ty,
214215
Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) => {
215216
let lhs_ty = lhs.ty(local_decls, tcx);

‎compiler/rustc_middle/src/mir/visit.rs

+8
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,14 @@ macro_rules! make_mir_visitor {
695695
self.visit_place(path, ctx, location);
696696
}
697697

698+
Rvalue::Len(path) => {
699+
self.visit_place(
700+
path,
701+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
702+
location
703+
);
704+
}
705+
698706
Rvalue::Cast(_cast_kind, operand, ty) => {
699707
self.visit_operand(operand, location);
700708
self.visit_ty($(& $mutability)? *ty, TyContext::Location(location));

‎compiler/rustc_mir_build/src/builder/custom/parse/instruction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
246246
let offset = self.parse_operand(args[1])?;
247247
Ok(Rvalue::BinaryOp(BinOp::Offset, Box::new((ptr, offset))))
248248
},
249+
@call(mir_len, args) => Ok(Rvalue::Len(self.parse_place(args[0])?)),
249250
@call(mir_ptr_metadata, args) => Ok(Rvalue::UnaryOp(UnOp::PtrMetadata, self.parse_operand(args[0])?)),
250251
@call(mir_copy_for_deref, args) => Ok(Rvalue::CopyForDeref(self.parse_place(args[0])?)),
251252
ExprKind::Borrow { borrow_kind, arg } => Ok(

‎compiler/rustc_mir_build/src/builder/expr/as_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
635635
///
636636
/// For arrays it'll be `Operand::Constant` with the actual length;
637637
/// For slices it'll be `Operand::Move` of a local using `PtrMetadata`.
638-
pub(in crate::builder) fn len_of_slice_or_array(
638+
fn len_of_slice_or_array(
639639
&mut self,
640640
block: BasicBlock,
641641
place: Place<'tcx>,

‎compiler/rustc_mir_build/src/builder/matches/test.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
243243
}
244244

245245
TestKind::Len { len, op } => {
246+
let usize_ty = self.tcx.types.usize;
247+
let actual = self.temp(usize_ty, test.span);
248+
246249
// actual = len(place)
247-
let actual = self.len_of_slice_or_array(block, place, test.span, source_info);
250+
self.cfg.push_assign(block, source_info, actual, Rvalue::Len(place));
248251

249252
// expected = <N>
250253
let expected = self.push_usize(block, source_info, len);
@@ -259,7 +262,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
259262
fail_block,
260263
source_info,
261264
op,
262-
actual,
265+
Operand::Move(actual),
263266
Operand::Move(expected),
264267
);
265268
}

‎compiler/rustc_mir_dataflow/src/impls/borrowed_locals.rs

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ where
9191
| Rvalue::Use(..)
9292
| Rvalue::ThreadLocalRef(..)
9393
| Rvalue::Repeat(..)
94+
| Rvalue::Len(..)
9495
| Rvalue::BinaryOp(..)
9596
| Rvalue::NullaryOp(..)
9697
| Rvalue::UnaryOp(..)

‎compiler/rustc_mir_dataflow/src/move_paths/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
413413
Rvalue::Ref(..)
414414
| Rvalue::RawPtr(..)
415415
| Rvalue::Discriminant(..)
416+
| Rvalue::Len(..)
416417
| Rvalue::NullaryOp(
417418
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::UbChecks,
418419
_,

‎compiler/rustc_mir_transform/src/dataflow_const_prop.rs

+12
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
408408
state: &mut State<FlatSet<Scalar>>,
409409
) -> ValueOrPlace<FlatSet<Scalar>> {
410410
let val = match rvalue {
411+
Rvalue::Len(place) => {
412+
let place_ty = place.ty(self.local_decls, self.tcx);
413+
if let ty::Array(_, len) = place_ty.ty.kind() {
414+
Const::Ty(self.tcx.types.usize, *len)
415+
.try_eval_scalar(self.tcx, self.typing_env)
416+
.map_or(FlatSet::Top, FlatSet::Elem)
417+
} else if let [ProjectionElem::Deref] = place.projection[..] {
418+
state.get_len(place.local.into(), &self.map)
419+
} else {
420+
FlatSet::Top
421+
}
422+
}
411423
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
412424
let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(*ty)) else {
413425
return ValueOrPlace::Value(FlatSet::Top);

‎compiler/rustc_mir_transform/src/dest_prop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ impl WriteInfo {
575575
| Rvalue::NullaryOp(_, _)
576576
| Rvalue::Ref(_, _, _)
577577
| Rvalue::RawPtr(_, _)
578+
| Rvalue::Len(_)
578579
| Rvalue::Discriminant(_)
579580
| Rvalue::CopyForDeref(_) => {}
580581
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.