Skip to content

Commit 7176c19

Browse files
Represent the raw pointer for a array length check as a new kind of fake borrow
1 parent c5b76e5 commit 7176c19

File tree

26 files changed

+188
-90
lines changed

26 files changed

+188
-90
lines changed

compiler/rustc_borrowck/src/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1284,15 +1284,18 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, '_, 'tcx> {
12841284
);
12851285
}
12861286

1287-
&Rvalue::RawPtr(mutability, place) => {
1288-
let access_kind = match mutability {
1289-
Mutability::Mut => (
1287+
&Rvalue::RawPtr(kind, place) => {
1288+
let access_kind = match kind {
1289+
RawPtrKind::Mut => (
12901290
Deep,
12911291
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
12921292
kind: MutBorrowKind::Default,
12931293
})),
12941294
),
1295-
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1295+
RawPtrKind::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
1296+
RawPtrKind::FakeForPtrMetadata => {
1297+
(Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))
1298+
}
12961299
};
12971300

12981301
self.access_place(

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

+10-11
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ use std::ops::ControlFlow;
33
use rustc_data_structures::graph::dominators::Dominators;
44
use rustc_middle::bug;
55
use rustc_middle::mir::visit::Visitor;
6-
use rustc_middle::mir::{
7-
self, BasicBlock, Body, BorrowKind, FakeBorrowKind, InlineAsmOperand, Location, Mutability,
8-
NonDivergingIntrinsic, Operand, Place, Rvalue, Statement, StatementKind, Terminator,
9-
TerminatorKind,
10-
};
6+
use rustc_middle::mir::*;
117
use rustc_middle::ty::TyCtxt;
128
use tracing::debug;
139

@@ -60,7 +56,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
6056
StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
6157
self.consume_operand(location, op);
6258
}
63-
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
59+
StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(CopyNonOverlapping {
6460
src,
6561
dst,
6662
count,
@@ -273,15 +269,18 @@ impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
273269
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
274270
}
275271

276-
&Rvalue::RawPtr(mutability, place) => {
277-
let access_kind = match mutability {
278-
Mutability::Mut => (
272+
&Rvalue::RawPtr(kind, place) => {
273+
let access_kind = match kind {
274+
RawPtrKind::Mut => (
279275
Deep,
280276
Write(WriteKind::MutableBorrow(BorrowKind::Mut {
281-
kind: mir::MutBorrowKind::Default,
277+
kind: MutBorrowKind::Default,
282278
})),
283279
),
284-
Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
280+
RawPtrKind::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
281+
RawPtrKind::FakeForPtrMetadata => {
282+
(Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))
283+
}
285284
};
286285

287286
self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
601601
mir::Rvalue::CopyForDeref(place) => {
602602
self.codegen_operand(bx, &mir::Operand::Copy(place))
603603
}
604-
mir::Rvalue::RawPtr(mutability, place) => {
605-
let mk_ptr =
606-
move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| Ty::new_ptr(tcx, ty, mutability);
604+
mir::Rvalue::RawPtr(kind, place) => {
605+
let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
606+
Ty::new_ptr(tcx, ty, kind.to_mutbl_lossy())
607+
};
607608
self.codegen_place_to_pointer(bx, place, mk_ptr)
608609
}
609610

compiler/rustc_const_eval/src/check_consts/check.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
518518
}
519519

520520
Rvalue::Ref(_, BorrowKind::Mut { .. }, place)
521-
| Rvalue::RawPtr(Mutability::Mut, place) => {
521+
| Rvalue::RawPtr(RawPtrKind::Mut, place) => {
522522
// Inside mutable statics, we allow arbitrary mutable references.
523523
// We've allowed `static mut FOO = &mut [elements];` for a long time (the exact
524524
// reasons why are lost to history), and there is no reason to restrict that to
@@ -536,7 +536,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
536536
}
537537

538538
Rvalue::Ref(_, BorrowKind::Shared | BorrowKind::Fake(_), place)
539-
| Rvalue::RawPtr(Mutability::Not, place) => {
539+
| Rvalue::RawPtr(RawPtrKind::Not | RawPtrKind::FakeForPtrMetadata, place) => {
540540
let borrowed_place_has_mut_interior = qualifs::in_place::<HasMutInterior, _>(
541541
self.ccx,
542542
&mut |local| self.qualifs.has_mut_interior(self.ccx, local, location),
@@ -600,12 +600,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
600600
}
601601
}
602602
UnOp::PtrMetadata => {
603-
if !ty.is_ref() && !ty.is_unsafe_ptr() {
604-
span_bug!(
605-
self.span,
606-
"non-pointer type in `Rvalue::UnaryOp({op:?})`: {ty:?}",
607-
);
608-
}
603+
// Getting the metadata from a pointer is always const.
604+
// We already validated the type is valid in the validator.
609605
}
610606
}
611607
}

compiler/rustc_const_eval/src/interpret/step.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_middle::ty::layout::FnAbiOf;
99
use rustc_middle::ty::{self, Instance, Ty};
1010
use rustc_middle::{bug, mir, span_bug};
1111
use rustc_span::source_map::Spanned;
12-
use rustc_span::{DesugaringKind, Span};
1312
use rustc_target::callconv::FnAbi;
1413
use tracing::{info, instrument, trace};
1514

@@ -81,9 +80,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
8180
use rustc_middle::mir::StatementKind::*;
8281

8382
match &stmt.kind {
84-
Assign(box (place, rvalue)) => {
85-
self.eval_rvalue_into_place(rvalue, *place, stmt.source_info.span)?
86-
}
83+
Assign(box (place, rvalue)) => self.eval_rvalue_into_place(rvalue, *place)?,
8784

8885
SetDiscriminant { place, variant_index } => {
8986
let dest = self.eval_place(**place)?;
@@ -162,7 +159,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
162159
&mut self,
163160
rvalue: &mir::Rvalue<'tcx>,
164161
place: mir::Place<'tcx>,
165-
span: Span,
166162
) -> InterpResult<'tcx> {
167163
let dest = self.eval_place(place)?;
168164
// FIXME: ensure some kind of non-aliasing between LHS and RHS?
@@ -241,7 +237,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
241237
self.write_immediate(*val, &dest)?;
242238
}
243239

244-
RawPtr(_, place) => {
240+
RawPtr(kind, place) => {
245241
// Figure out whether this is an addr_of of an already raw place.
246242
let place_base_raw = if place.is_indirect_first_projection() {
247243
let ty = self.frame().body.local_decls[place.local].ty;
@@ -254,13 +250,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
254250
let src = self.eval_place(place)?;
255251
let place = self.force_allocation(&src)?;
256252
let mut val = ImmTy::from_immediate(place.to_ref(self), dest.layout);
257-
if !place_base_raw
258-
&& span.desugaring_kind() != Some(DesugaringKind::IndexBoundsCheckReborrow)
259-
{
260-
// If this was not already raw, it needs retagging.
261-
// As a special hack, we exclude the desugared `PtrMetadata(&raw const *_n)`
262-
// from indexing. (Really we should not do any retag on `&raw` but that does not
263-
// currently work with Stacked Borrows.)
253+
if !place_base_raw && !kind.is_fake() {
254+
// If this was not already raw, it needs retagging -- except for "fake"
255+
// raw borrows whose defining property is that they do not get retagged.
264256
val = M::retag_ptr_value(self, mir::RetagKind::Raw, &val)?;
265257
}
266258
self.write_immediate(*val, &dest)?;

compiler/rustc_middle/src/mir/syntax.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,53 @@ pub enum BorrowKind {
180180
Mut { kind: MutBorrowKind },
181181
}
182182

183+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
184+
#[derive(Hash, HashStable)]
185+
pub enum RawPtrKind {
186+
Mut,
187+
Not,
188+
/// Represents a raw pointer for a place to access its mutability.
189+
/// The data behind this point will not be accessed, just its metadata.
190+
FakeForPtrMetadata,
191+
}
192+
193+
impl From<Mutability> for RawPtrKind {
194+
fn from(other: Mutability) -> Self {
195+
match other {
196+
Mutability::Mut => RawPtrKind::Mut,
197+
Mutability::Not => RawPtrKind::Not,
198+
}
199+
}
200+
}
201+
202+
impl RawPtrKind {
203+
pub fn is_fake(self) -> bool {
204+
match self {
205+
RawPtrKind::Mut | RawPtrKind::Not => false,
206+
RawPtrKind::FakeForPtrMetadata => true,
207+
}
208+
}
209+
210+
pub fn to_mutbl_lossy(self) -> Mutability {
211+
match self {
212+
RawPtrKind::Mut => Mutability::Mut,
213+
RawPtrKind::Not => Mutability::Not,
214+
215+
// We have no type corresponding to a fake borrow, so use
216+
// `*const` as an approximation.
217+
RawPtrKind::FakeForPtrMetadata => Mutability::Not,
218+
}
219+
}
220+
221+
pub fn ptr_str(self) -> &'static str {
222+
match self {
223+
RawPtrKind::Mut => "mut",
224+
RawPtrKind::Not => "const",
225+
RawPtrKind::FakeForPtrMetadata => "const (fake)",
226+
}
227+
}
228+
}
229+
183230
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TyEncodable, TyDecodable)]
184231
#[derive(Hash, HashStable)]
185232
pub enum MutBorrowKind {
@@ -1349,7 +1396,7 @@ pub enum Rvalue<'tcx> {
13491396
///
13501397
/// Like with references, the semantics of this operation are heavily dependent on the aliasing
13511398
/// model.
1352-
RawPtr(Mutability, Place<'tcx>),
1399+
RawPtr(RawPtrKind, Place<'tcx>),
13531400

13541401
/// Yields the length of the place, as a `usize`.
13551402
///

compiler/rustc_middle/src/mir/tcx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ impl<'tcx> Rvalue<'tcx> {
206206
let place_ty = place.ty(local_decls, tcx).ty;
207207
Ty::new_ref(tcx, reg, place_ty, bk.to_mutbl_lossy())
208208
}
209-
Rvalue::RawPtr(mutability, ref place) => {
209+
Rvalue::RawPtr(kind, ref place) => {
210210
let place_ty = place.ty(local_decls, tcx).ty;
211-
Ty::new_ptr(tcx, place_ty, mutability)
211+
Ty::new_ptr(tcx, place_ty, kind.to_mutbl_lossy())
212212
}
213213
Rvalue::Len(..) => tcx.types.usize,
214214
Rvalue::Cast(.., ty) => ty,

compiler/rustc_middle/src/mir/type_foldable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ TrivialTypeTraversalImpls! {
1515
SourceScopeLocalData,
1616
UserTypeAnnotationIndex,
1717
BorrowKind,
18+
RawPtrKind,
1819
CastKind,
1920
BasicBlock,
2021
SwitchTargets,

compiler/rustc_middle/src/mir/visit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -685,12 +685,15 @@ macro_rules! make_mir_visitor {
685685

686686
Rvalue::RawPtr(m, path) => {
687687
let ctx = match m {
688-
Mutability::Mut => PlaceContext::MutatingUse(
688+
RawPtrKind::Mut => PlaceContext::MutatingUse(
689689
MutatingUseContext::RawBorrow
690690
),
691-
Mutability::Not => PlaceContext::NonMutatingUse(
691+
RawPtrKind::Not => PlaceContext::NonMutatingUse(
692692
NonMutatingUseContext::RawBorrow
693693
),
694+
RawPtrKind::FakeForPtrMetadata => PlaceContext::NonMutatingUse(
695+
NonMutatingUseContext::Inspect
696+
),
694697
};
695698
self.visit_place(path, ctx, location);
696699
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl<'a, 'tcx> ParseCtxt<'a, 'tcx> {
253253
Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?)
254254
),
255255
ExprKind::RawBorrow { mutability, arg } => Ok(
256-
Rvalue::RawPtr(*mutability, self.parse_place(*arg)?)
256+
Rvalue::RawPtr((*mutability).into(), self.parse_place(*arg)?)
257257
),
258258
ExprKind::Binary { op, lhs, rhs } => Ok(
259259
Rvalue::BinaryOp(*op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?)))

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

+5-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_middle::mir::*;
1111
use rustc_middle::thir::*;
1212
use rustc_middle::ty::{self, AdtDef, CanonicalUserTypeAnnotation, Ty, Variance};
1313
use rustc_middle::{bug, span_bug};
14-
use rustc_span::{DesugaringKind, Span};
14+
use rustc_span::Span;
1515
use tracing::{debug, instrument, trace};
1616

1717
use crate::builder::ForGuard::{OutsideGuard, RefWithinGuard};
@@ -643,8 +643,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
643643
source_info: SourceInfo,
644644
) -> Operand<'tcx> {
645645
let place_ty = place.ty(&self.local_decls, self.tcx).ty;
646-
let usize_ty = self.tcx.types.usize;
647-
648646
match place_ty.kind() {
649647
ty::Array(_elem_ty, len_const) => {
650648
// We know how long an array is, so just use that as a constant
@@ -653,7 +651,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
653651
// though. FIXME: Do we really *need* to count this as a use?
654652
// Could partial array tracking work off something else instead?
655653
self.cfg.push_fake_read(block, source_info, FakeReadCause::ForIndex, place);
656-
let const_ = Const::from_ty_const(*len_const, usize_ty, self.tcx);
654+
let const_ = Const::from_ty_const(*len_const, self.tcx.types.usize, self.tcx);
657655
Operand::Constant(Box::new(ConstOperand { span, user_ty: None, const_ }))
658656
}
659657
ty::Slice(_elem_ty) => {
@@ -668,27 +666,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
668666
// the MIR we're building here needs to pass NLL later.
669667
Operand::Copy(Place::from(place.local))
670668
} else {
671-
let len_span = self.tcx.with_stable_hashing_context(|hcx| {
672-
let span = source_info.span;
673-
span.mark_with_reason(
674-
None,
675-
DesugaringKind::IndexBoundsCheckReborrow,
676-
span.edition(),
677-
hcx,
678-
)
679-
});
680669
let ptr_ty = Ty::new_imm_ptr(self.tcx, place_ty);
681670
let slice_ptr = self.temp(ptr_ty, span);
682671
self.cfg.push_assign(
683672
block,
684-
SourceInfo { span: len_span, ..source_info },
673+
source_info,
685674
slice_ptr,
686-
Rvalue::RawPtr(Mutability::Not, place),
675+
Rvalue::RawPtr(RawPtrKind::FakeForPtrMetadata, place),
687676
);
688677
Operand::Move(slice_ptr)
689678
};
690679

691-
let len = self.temp(usize_ty, span);
680+
let len = self.temp(self.tcx.types.usize, span);
692681
self.cfg.push_assign(
693682
block,
694683
source_info,

compiler/rustc_mir_build/src/builder/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
303303
hir::Mutability::Not => this.as_read_only_place(block, arg),
304304
hir::Mutability::Mut => this.as_place(block, arg),
305305
};
306-
let address_of = Rvalue::RawPtr(mutability, unpack!(block = place));
306+
let address_of = Rvalue::RawPtr(mutability.into(), unpack!(block = place));
307307
this.cfg.push_assign(block, source_info, destination, address_of);
308308
block.unit()
309309
}

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ where
700700
statements: vec![
701701
self.assign(
702702
ptr,
703-
Rvalue::RawPtr(Mutability::Mut, tcx.mk_place_index(self.place, cur)),
703+
Rvalue::RawPtr(RawPtrKind::Mut, tcx.mk_place_index(self.place, cur)),
704704
),
705705
self.assign(
706706
cur.into(),
@@ -816,7 +816,7 @@ where
816816

817817
let mut delegate_block = BasicBlockData {
818818
statements: vec![
819-
self.assign(Place::from(array_ptr), Rvalue::RawPtr(Mutability::Mut, self.place)),
819+
self.assign(Place::from(array_ptr), Rvalue::RawPtr(RawPtrKind::Mut, self.place)),
820820
self.assign(
821821
Place::from(slice_ptr),
822822
Rvalue::Cast(

compiler/rustc_mir_transform/src/gvn.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ enum AggregateTy<'tcx> {
188188
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
189189
enum AddressKind {
190190
Ref(BorrowKind),
191-
Address(Mutability),
191+
Address(RawPtrKind),
192192
}
193193

194194
#[derive(Debug, PartialEq, Eq, Hash)]
@@ -500,7 +500,9 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
500500
mplace.layout.ty,
501501
bk.to_mutbl_lossy(),
502502
),
503-
AddressKind::Address(mutbl) => Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl),
503+
AddressKind::Address(mutbl) => {
504+
Ty::new_ptr(self.tcx, mplace.layout.ty, mutbl.to_mutbl_lossy())
505+
}
504506
};
505507
let layout = self.ecx.layout_of(ty).ok()?;
506508
ImmTy::from_immediate(pointer, layout).into()

0 commit comments

Comments
 (0)