1
Fork 0

Rollup merge of #135748 - compiler-errors:len-2, r=RalfJung,oli-obk

Lower index bounds checking to `PtrMetadata`, this time with the right fake borrow semantics 😸

Change `Rvalue::RawRef` to take a `RawRefKind` instead of just a `Mutability`. Then introduce `RawRefKind::FakeForPtrMetadata` and use that for lowering index bounds checking to a `PtrMetadata`. This new `RawRefKind::FakeForPtrMetadata` acts like a shallow fake borrow in borrowck, which mimics the semantics of the old `Rvalue::Len` operation we're replacing.

We can then use this `RawRefKind` instead of using a span desugaring hack in CTFE.

cc ``@scottmcm`` ``@RalfJung``
This commit is contained in:
Matthias Krüger 2025-01-28 14:23:22 +01:00 committed by GitHub
commit 21ddd7ab89
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
117 changed files with 1591 additions and 1704 deletions

View file

@ -192,7 +192,7 @@ enum AggregateTy<'tcx> {
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
enum AddressKind {
Ref(BorrowKind),
Address(Mutability),
Address(RawPtrKind),
}
#[derive(Debug, PartialEq, Eq, Hash)]
@ -504,7 +504,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
mplace.layout.ty,
bk.to_mutbl_lossy(),
),
AddressKind::Address(mutbl) => Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl),
AddressKind::Address(mutbl) => {
Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl.to_mutbl_lossy())
}
};
let layout = self.ecx.layout_of(ty).ok()?;
ImmTy::from_immediate(pointer, layout).into()

View file

@ -46,7 +46,6 @@ impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
}
ctx.simplify_bool_cmp(rvalue);
ctx.simplify_ref_deref(rvalue);
ctx.simplify_len(rvalue);
ctx.simplify_ptr_aggregate(rvalue);
ctx.simplify_cast(rvalue);
ctx.simplify_repeated_aggregate(rvalue);
@ -166,18 +165,6 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
}
}
/// Transform `Len([_; N])` ==> `N`.
fn simplify_len(&self, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Len(ref place) = *rvalue {
let place_ty = place.ty(self.local_decls, self.tcx).ty;
if let ty::Array(_, len) = *place_ty.kind() {
let const_ = Const::Ty(self.tcx.types.usize, len);
let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None };
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
}
}
}
/// Transform `Aggregate(RawPtr, [p, ()])` ==> `Cast(PtrToPtr, p)`.
fn simplify_ptr_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue

View file

@ -125,7 +125,7 @@ impl<'tcx> crate::MirPass<'tcx> for EnumSizeOpt {
source_info,
kind: StatementKind::Assign(Box::new((
dst,
Rvalue::RawPtr(Mutability::Mut, *lhs),
Rvalue::RawPtr(RawPtrKind::Mut, *lhs),
))),
};
@ -146,7 +146,7 @@ impl<'tcx> crate::MirPass<'tcx> for EnumSizeOpt {
source_info,
kind: StatementKind::Assign(Box::new((
src,
Rvalue::RawPtr(Mutability::Not, *rhs),
Rvalue::RawPtr(RawPtrKind::Const, *rhs),
))),
};

View file

@ -2,17 +2,11 @@ use std::iter;
use itertools::Itertools;
use rustc_abi::{FieldIdx, VariantIdx};
use rustc_ast::Mutability;
use rustc_const_eval::interpret;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::LangItem;
use rustc_index::{Idx, IndexVec};
use rustc_middle::mir::{
BasicBlock, BasicBlockData, Body, CallSource, CastKind, CoercionSource, Const, ConstOperand,
ConstValue, Local, LocalDecl, MirSource, Operand, Place, PlaceElem, RETURN_PLACE, Rvalue,
SourceInfo, Statement, StatementKind, Terminator, TerminatorKind, UnwindAction,
UnwindTerminateReason,
};
use rustc_middle::mir::*;
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::util::{AsyncDropGlueMorphology, Discr};
use rustc_middle::ty::{self, Ty, TyCtxt};
@ -345,7 +339,7 @@ impl<'tcx> AsyncDestructorCtorShimBuilder<'tcx> {
.tcx
.mk_place_elems(&[PlaceElem::Deref, PlaceElem::Field(field, field_ty)]),
};
self.put_temp_rvalue(Rvalue::RawPtr(Mutability::Mut, place))
self.put_temp_rvalue(Rvalue::RawPtr(RawPtrKind::Mut, place))
}
/// If given Self is an enum puts `to_drop: *mut FieldTy` on top of
@ -365,7 +359,7 @@ impl<'tcx> AsyncDestructorCtorShimBuilder<'tcx> {
PlaceElem::Field(field, field_ty),
]),
};
self.put_temp_rvalue(Rvalue::RawPtr(Mutability::Mut, place))
self.put_temp_rvalue(Rvalue::RawPtr(RawPtrKind::Mut, place))
}
/// If given Self is an enum puts `to_drop: *mut FieldTy` on top of

View file

@ -1128,14 +1128,6 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
);
}
UnOp::PtrMetadata => {
if !matches!(self.body.phase, MirPhase::Runtime(_)) {
// It would probably be fine to support this in earlier phases, but at
// the time of writing it's only ever introduced from intrinsic
// lowering or other runtime-phase optimization passes, so earlier
// things can just `bug!` on it.
self.fail(location, "PtrMetadata should be in runtime MIR only");
}
check_kinds!(
a,
"Cannot PtrMetadata non-pointer non-reference type {:?}",