sun::borrow_checker::BorrowState
class · Source (opens in a new tab)
class sun::borrow_checker::BorrowStateTracks the borrow state of all variables during analysis Enforces Rust-style borrow rules:
- At any given time, either ONE mutable borrow OR any number of shared borrows
- Cannot mutate a variable while it has active borrows (unless through the borrow)
- Borrows are invalidated when they go out of scope
Public Functions
- BorrowState
- addBorrow
- canMutateDirectly
- canMutateThroughRef
- canRead
- clear
- clearLifetime
- exitScope
- getActiveLoans
- getAllActiveLoans
- getLifetime
- getRefTarget
- isActiveRef
- setLifetime
BorrowState
public · function · Source (opens in a new tab)
sun::borrow_checker::BorrowState::BorrowState()=defaultCreates an instance with its default state.
addBorrow
public · function · Source (opens in a new tab)
BorrowCheckResult sun::borrow_checker::BorrowState::addBorrow(const std::string &borrowedVar, const std::string &refName, BorrowKind kind, size_t scopeDepth, const sun::support::Position &loc)Attempt to create a new borrow Returns error if this would violate borrow rules.
Related: BorrowCheckResult, BorrowKind, sun::support::Position
canMutateDirectly
public · function · Source (opens in a new tab)
BorrowCheckResult sun::borrow_checker::BorrowState::canMutateDirectly(const std::string &var) constCheck if we can mutate a variable directly (not through a reference) Mutation is blocked if there are any active borrows.
Related: BorrowCheckResult
canMutateThroughRef
public · function · Source (opens in a new tab)
BorrowCheckResult sun::borrow_checker::BorrowState::canMutateThroughRef(const std::string &refName) constCheck if we can mutate through a specific reference Only allowed if this is the only active mutable borrow.
Related: BorrowCheckResult
canRead
public · function · Source (opens in a new tab)
BorrowCheckResult sun::borrow_checker::BorrowState::canRead(const std::string &var, const std::string &throughRef="") constCheck if we can read a variable Reading is always allowed unless there's an active mutable borrow by another ref.
Related: BorrowCheckResult
clear
public · function · Source (opens in a new tab)
void sun::borrow_checker::BorrowState::clear()Clear all state (for testing).
clearLifetime
public · function · Source (opens in a new tab)
void sun::borrow_checker::BorrowState::clearLifetime(const std::string &name)Forget the lifetime recorded for a name.
exitScope
public · function · Source (opens in a new tab)
void sun::borrow_checker::BorrowState::exitScope(size_t scopeDepth)Called when exiting a scope - invalidates borrows at or deeper than scopeDepth.
getActiveLoans
public · function · Source (opens in a new tab)
std::vector< Loan > sun::borrow_checker::BorrowState::getActiveLoans(const std::string &var) constGet all active loans for a variable (for error reporting).
Related: Loan
getAllActiveLoans
public · function · Source (opens in a new tab)
std::vector< Loan > sun::borrow_checker::BorrowState::getAllActiveLoans() constGet all active loans (for debugging).
Related: Loan
getLifetime
public · function · Source (opens in a new tab)
std::optional< Lifetime > sun::borrow_checker::BorrowState::getLifetime(const std::string &name) constGet the lifetime for a variable or reference (if known).
Related: Lifetime
getRefTarget
public · function · Source (opens in a new tab)
const std::string * sun::borrow_checker::BorrowState::getRefTarget(const std::string &refName) constGet the variable that a reference points to (if it's an active ref).
isActiveRef
public · function · Source (opens in a new tab)
bool sun::borrow_checker::BorrowState::isActiveRef(const std::string &name) constCheck if a name refers to an active reference.
setLifetime
public · function · Source (opens in a new tab)
void sun::borrow_checker::BorrowState::setLifetime(const std::string &name, const Lifetime <)Set the lifetime for a variable or reference.
Related: Lifetime
Private Fields
lifetimes_
private · variable · Source (opens in a new tab)
std::unordered_map<std::string, Lifetime> sun::borrow_checker::BorrowState::lifetimes_Maps variable/reference name -> its lifetime.
Related: Lifetime
loans_
private · variable · Source (opens in a new tab)
std::unordered_map<std::string, std::vector<Loan> > sun::borrow_checker::BorrowState::loans_Maps variable name -> list of active loans on that variable.
Related: Loan
refToTarget_
private · variable · Source (opens in a new tab)
std::unordered_map<std::string, std::string> sun::borrow_checker::BorrowState::refToTarget_Maps reference name -> target variable (for quick lookup).