1
Fork 0

Add a non-shallow fake borrow

This commit is contained in:
Nadrieril 2024-04-07 00:30:28 +02:00
parent 511bd78863
commit 50531806ee
32 changed files with 188 additions and 92 deletions

View file

@ -865,11 +865,9 @@ pub enum BorrowKind {
/// Data must be immutable and is aliasable.
Shared,
/// The immediately borrowed place must be immutable, but projections from
/// it don't need to be. This is used to prevent match guards from replacing
/// the scrutinee. For example, a fake borrow of `a.b` doesn't
/// conflict with a mutable borrow of `a.b.c`.
Fake,
/// An immutable, aliasable borrow that is discarded after borrow-checking. Can behave either
/// like a normal shared borrow or like a special shallow borrow (see [`FakeBorrowKind`]).
Fake(FakeBorrowKind),
/// Data is mutable and not aliasable.
Mut {
@ -884,7 +882,7 @@ impl BorrowKind {
BorrowKind::Mut { .. } => Mutability::Mut,
BorrowKind::Shared => Mutability::Not,
// FIXME: There's no type corresponding to a shallow borrow, so use `&` as an approximation.
BorrowKind::Fake => Mutability::Not,
BorrowKind::Fake(_) => Mutability::Not,
}
}
}
@ -896,6 +894,17 @@ pub enum MutBorrowKind {
ClosureCapture,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum FakeBorrowKind {
/// A shared (deep) borrow. Data must be immutable and is aliasable.
Deep,
/// The immediately borrowed place must be immutable, but projections from
/// it don't need to be. This is used to prevent match guards from replacing
/// the scrutinee. For example, a fake borrow of `a.b` doesn't
/// conflict with a mutable borrow of `a.b.c`.
Shallow,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Mutability {
Not,

View file

@ -8,7 +8,7 @@ use std::{fmt, io, iter};
use super::{AssertMessage, BinOp, TerminatorKind};
use super::BorrowKind;
use super::{BorrowKind, FakeBorrowKind};
impl Display for Ty {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
@ -352,7 +352,8 @@ fn pretty_rvalue<W: Write>(writer: &mut W, rval: &Rvalue) -> io::Result<()> {
Rvalue::Ref(_, borrowkind, place) => {
let kind = match borrowkind {
BorrowKind::Shared => "&",
BorrowKind::Fake => "&fake ",
BorrowKind::Fake(FakeBorrowKind::Deep) => "&fake ",
BorrowKind::Fake(FakeBorrowKind::Shallow) => "&fake shallow ",
BorrowKind::Mut { .. } => "&mut ",
};
write!(writer, "{kind}{:?}", place)