1
Fork 0

Represent the raw pointer for a array length check as a new kind of fake borrow

This commit is contained in:
Michael Goulet 2025-01-19 22:45:03 +00:00
parent 057313b7a6
commit eeecb56b73
26 changed files with 199 additions and 89 deletions

View file

@ -457,7 +457,7 @@ pub enum Rvalue {
///
/// This is generated by pointer casts like `&v as *const _` or raw address of expressions like
/// `&raw v` or `addr_of!(v)`.
AddressOf(Mutability, Place),
AddressOf(RawPtrKind, Place),
/// Creates an aggregate value, like a tuple or struct.
///
@ -577,7 +577,7 @@ impl Rvalue {
}
Rvalue::AddressOf(mutability, place) => {
let place_ty = place.ty(locals)?;
Ok(Ty::new_ptr(place_ty, *mutability))
Ok(Ty::new_ptr(place_ty, mutability.to_mutable_lossy()))
}
Rvalue::Len(..) => Ok(Ty::usize_ty()),
Rvalue::Cast(.., ty) => Ok(*ty),
@ -903,6 +903,24 @@ impl BorrowKind {
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)]
pub enum RawPtrKind {
Mut,
Const,
FakeForPtrMetadata,
}
impl RawPtrKind {
pub fn to_mutable_lossy(self) -> Mutability {
match self {
RawPtrKind::Mut { .. } => Mutability::Mut,
RawPtrKind::Const => Mutability::Not,
// FIXME: There's no type corresponding to a shallow borrow, so use `&` as an approximation.
RawPtrKind::FakeForPtrMetadata => Mutability::Not,
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)]
pub enum MutBorrowKind {
Default,

View file

@ -6,7 +6,9 @@ use std::{fmt, io, iter};
use fmt::{Display, Formatter};
use super::{AggregateKind, AssertMessage, BinOp, BorrowKind, FakeBorrowKind, TerminatorKind};
use crate::mir::{Operand, Place, Rvalue, StatementKind, UnwindAction, VarDebugInfoContents};
use crate::mir::{
Operand, Place, RawPtrKind, Rvalue, StatementKind, UnwindAction, VarDebugInfoContents,
};
use crate::ty::{AdtKind, IndexedVal, MirConst, Ty, TyConst};
use crate::{Body, CrateDef, Mutability, with};
@ -325,7 +327,7 @@ fn pretty_ty_const(ct: &TyConst) -> String {
fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> {
match rval {
Rvalue::AddressOf(mutability, place) => {
write!(writer, "&raw {} {:?}", pretty_mut(*mutability), place)
write!(writer, "&raw {} {:?}", pretty_raw_ptr_kind(*mutability), place)
}
Rvalue::Aggregate(aggregate_kind, operands) => {
// FIXME: Add pretty_aggregate function that returns a pretty string
@ -437,3 +439,11 @@ fn pretty_mut(mutability: Mutability) -> &'static str {
Mutability::Mut => "mut ",
}
}
fn pretty_raw_ptr_kind(kind: RawPtrKind) -> &'static str {
match kind {
RawPtrKind::Const => "const",
RawPtrKind::Mut => "mut",
RawPtrKind::FakeForPtrMetadata => "const (fake)",
}
}

View file

@ -308,7 +308,7 @@ pub trait MirVisitor {
fn super_rvalue(&mut self, rvalue: &Rvalue, location: Location) {
match rvalue {
Rvalue::AddressOf(mutability, place) => {
let pcx = PlaceContext { is_mut: *mutability == Mutability::Mut };
let pcx = PlaceContext { is_mut: *mutability == RawPtrKind::Mut };
self.visit_place(place, pcx, location);
}
Rvalue::Aggregate(_, operands) => {