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 e108481

Browse files
committedDec 22, 2024
Auto merge of #134330 - scottmcm:no-more-rvalue-len, r=matthewjasper
Delete `Rvalue::Len` 🎉 Everything's moved to `PtrMetadata`, so we can get rid of the `Len` variant now. ~~Depends on #134326, so draft until that lands~~ Ready! r? mir
2 parents 303e8bd + 29889fd commit e108481

37 files changed

+51
-272
lines changed
 

‎compiler/rustc_borrowck/src/lib.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,6 @@ use self::ReadOrWrite::{Activation, Read, Reservation, Write};
820820

821821
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
822822
enum ArtificialField {
823-
ArrayLength,
824823
FakeBorrow,
825824
}
826825

@@ -1268,16 +1267,11 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
12681267
);
12691268
}
12701269

1271-
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
1272-
let af = match *rvalue {
1273-
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
1274-
Rvalue::Discriminant(..) => None,
1275-
_ => unreachable!(),
1276-
};
1270+
&Rvalue::Discriminant(place) => {
12771271
self.access_place(
12781272
location,
12791273
(place, span),
1280-
(Shallow(af), Read(ReadKind::Copy)),
1274+
(Shallow(None), Read(ReadKind::Copy)),
12811275
LocalMutationIsAllowed::No,
12821276
state,
12831277
);

‎compiler/rustc_borrowck/src/places_conflict.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ 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::ArrayLength)))
207-
| (_, _, Shallow(Some(ArtificialField::FakeBorrow))) => {
206+
(_, _, Shallow(Some(ArtificialField::FakeBorrow))) => {
208207
// The array length is like additional fields on the
209208
// type; it does not overlap any existing data there.
210209
// Furthermore, if cannot actually be a prefix of any

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,11 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
299299
self.consume_operand(location, op);
300300
}
301301

302-
&(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
303-
let af = match rvalue {
304-
Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
305-
Rvalue::Discriminant(..) => None,
306-
_ => unreachable!(),
307-
};
302+
&Rvalue::Discriminant(place) => {
308303
self.access_place(
309304
location,
310305
place,
311-
(Shallow(af), Read(ReadKind::Copy)),
306+
(Shallow(None), Read(ReadKind::Copy)),
312307
LocalMutationIsAllowed::No,
313308
);
314309
}

‎compiler/rustc_borrowck/src/type_check/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24272427

24282428
Rvalue::RawPtr(..)
24292429
| Rvalue::ThreadLocalRef(..)
2430-
| Rvalue::Len(..)
24312430
| Rvalue::Discriminant(..)
24322431
| Rvalue::NullaryOp(NullOp::OffsetOf(..), _) => {}
24332432
}
@@ -2443,7 +2442,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24432442
| Rvalue::Repeat(..)
24442443
| Rvalue::Ref(..)
24452444
| Rvalue::RawPtr(..)
2446-
| Rvalue::Len(..)
24472445
| Rvalue::Cast(..)
24482446
| Rvalue::ShallowInitBox(..)
24492447
| Rvalue::BinaryOp(..)

‎compiler/rustc_codegen_cranelift/src/base.rs

-6
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,6 @@ 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-
}
837831
Rvalue::ShallowInitBox(ref operand, content_ty) => {
838832
let content_ty = fx.monomorphize(content_ty);
839833
let box_layout = fx.layout_of(Ty::new_box(fx.tcx, content_ty));

‎compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+1-28
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;
1314
use super::operand::{OperandRef, OperandValue};
1415
use super::place::PlaceRef;
15-
use super::{FunctionCx, LocalRef};
1616
use crate::common::IntPredicate;
1717
use crate::traits::*;
1818
use crate::{MemFlags, base};
@@ -593,14 +593,6 @@ 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-
604596
mir::Rvalue::BinaryOp(op_with_overflow, box (ref lhs, ref rhs))
605597
if let Some(op) = op_with_overflow.overflowing_to_wrapping() =>
606598
{
@@ -800,24 +792,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
800792
}
801793
}
802794

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-
821795
/// Codegen an `Rvalue::RawPtr` or `Rvalue::Ref`
822796
fn codegen_place_to_pointer(
823797
&mut self,
@@ -1089,7 +1063,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10891063
mir::Rvalue::Ref(..) |
10901064
mir::Rvalue::CopyForDeref(..) |
10911065
mir::Rvalue::RawPtr(..) |
1092-
mir::Rvalue::Len(..) |
10931066
mir::Rvalue::Cast(..) | // (*)
10941067
mir::Rvalue::ShallowInitBox(..) | // (*)
10951068
mir::Rvalue::BinaryOp(..) |

‎compiler/rustc_const_eval/src/check_consts/check.rs

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

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

‎compiler/rustc_const_eval/src/check_consts/qualifs.rs

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

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

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

‎compiler/rustc_const_eval/src/check_consts/resolver.rs

-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ where
197197
| mir::Rvalue::CopyForDeref(..)
198198
| mir::Rvalue::ThreadLocalRef(..)
199199
| mir::Rvalue::Repeat(..)
200-
| mir::Rvalue::Len(..)
201200
| mir::Rvalue::BinaryOp(..)
202201
| mir::Rvalue::NullaryOp(..)
203202
| mir::Rvalue::UnaryOp(..)

‎compiler/rustc_const_eval/src/interpret/step.rs

+1-7
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, Scalar, interp_ok, throw_ub,
18+
Projectable, interp_ok, throw_ub,
1919
};
2020
use crate::util;
2121

@@ -218,12 +218,6 @@ 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-
227221
Ref(_, borrow_kind, place) => {
228222
let src = self.eval_place(place)?;
229223
let place = self.force_allocation(&src)?;

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

-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,6 @@ 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:?})"),
10721071
Cast(ref kind, ref place, ref ty) => {
10731072
with_no_trimmed_paths!(write!(fmt, "{place:?} as {ty} ({kind:?})"))
10741073
}

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

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

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

-10
Original file line numberDiff line numberDiff line change
@@ -1351,16 +1351,6 @@ 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-
13641354
/// Performs essentially all of the casts that can be performed via `as`.
13651355
///
13661356
/// 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
@@ -175,7 +175,6 @@ impl<'tcx> Rvalue<'tcx> {
175175
let place_ty = place.ty(local_decls, tcx).ty;
176176
Ty::new_ptr(tcx, place_ty, mutability)
177177
}
178-
Rvalue::Len(..) => tcx.types.usize,
179178
Rvalue::Cast(.., ty) => ty,
180179
Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) => {
181180
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,14 +695,6 @@ 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-
706698
Rvalue::Cast(_cast_kind, operand, ty) => {
707699
self.visit_operand(operand, location);
708700
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,7 +246,6 @@ 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])?)),
250249
@call(mir_ptr_metadata, args) => Ok(Rvalue::UnaryOp(UnOp::PtrMetadata, self.parse_operand(args[0])?)),
251250
@call(mir_copy_for_deref, args) => Ok(Rvalue::CopyForDeref(self.parse_place(args[0])?)),
252251
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-
fn len_of_slice_or_array(
638+
pub(in crate::builder) 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

+2-5
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,8 @@ 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-
249246
// actual = len(place)
250-
self.cfg.push_assign(block, source_info, actual, Rvalue::Len(place));
247+
let actual = self.len_of_slice_or_array(block, place, test.span, source_info);
251248

252249
// expected = <N>
253250
let expected = self.push_usize(block, source_info, len);
@@ -262,7 +259,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
262259
fail_block,
263260
source_info,
264261
op,
265-
Operand::Move(actual),
262+
actual,
266263
Operand::Move(expected),
267264
);
268265
}

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

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

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

-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ impl<'a, 'tcx, F: Fn(Ty<'tcx>) -> bool> MoveDataBuilder<'a, 'tcx, F> {
411411
Rvalue::Ref(..)
412412
| Rvalue::RawPtr(..)
413413
| Rvalue::Discriminant(..)
414-
| Rvalue::Len(..)
415414
| Rvalue::NullaryOp(
416415
NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..) | NullOp::UbChecks,
417416
_,

‎compiler/rustc_mir_transform/src/dataflow_const_prop.rs

-12
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,6 @@ 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-
}
423411
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
424412
let Ok(layout) = self.tcx.layout_of(self.typing_env.as_query_input(*ty)) else {
425413
return ValueOrPlace::Value(FlatSet::Top);

‎compiler/rustc_mir_transform/src/dest_prop.rs

-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,6 @@ impl WriteInfo {
574574
| Rvalue::NullaryOp(_, _)
575575
| Rvalue::Ref(_, _, _)
576576
| Rvalue::RawPtr(_, _)
577-
| Rvalue::Len(_)
578577
| Rvalue::Discriminant(_)
579578
| Rvalue::CopyForDeref(_) => {}
580579
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.