Skip to content

Commit

Permalink
Remove RestrictionSet
Browse files Browse the repository at this point in the history
Now that RestrictionSet is no longer being used for anything meaningful,
it can be removed, along with any other associated functions and
RestrictionSet fields of other types.
  • Loading branch information
Cameron Zwarich committed Jun 16, 2014
1 parent 9934759 commit 59309e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 98 deletions.
17 changes: 1 addition & 16 deletions src/librustc/middle/borrowck/gather_loans/mod.rs
Expand Up @@ -259,7 +259,7 @@ impl<'a> GatherLoanCtxt<'a> {
// loan is safe.
let restr = restrictions::compute_restrictions(
self.bccx, borrow_span, cause,
cmt.clone(), loan_region, self.restriction_set(req_kind));
cmt.clone(), loan_region);

// Create the loan record (if needed).
let loan = match restr {
Expand Down Expand Up @@ -390,21 +390,6 @@ impl<'a> GatherLoanCtxt<'a> {
}
}

fn restriction_set(&self, req_kind: ty::BorrowKind) -> RestrictionSet {
match req_kind {
// If borrowing data as immutable, no mutation allowed:
ty::ImmBorrow => RESTR_MUTATE,

// If borrowing data as mutable, no mutation nor other
// borrows allowed:
ty::MutBorrow => RESTR_MUTATE | RESTR_FREEZE,

// If borrowing data as unique imm, no mutation nor other
// borrows allowed:
ty::UniqueImmBorrow => RESTR_MUTATE | RESTR_FREEZE,
}
}

pub fn mark_loan_path_as_mutated(&self, loan_path: &LoanPath) {
//! For mutable loans of content whose mutability derives
//! from a local variable, mark the mutability decl as necessary.
Expand Down
45 changes: 15 additions & 30 deletions src/librustc/middle/borrowck/gather_loans/restrictions.rs
Expand Up @@ -30,16 +30,15 @@ pub fn compute_restrictions(bccx: &BorrowckCtxt,
span: Span,
cause: euv::LoanCause,
cmt: mc::cmt,
loan_region: ty::Region,
restr: RestrictionSet) -> RestrictionResult {
loan_region: ty::Region) -> RestrictionResult {
let ctxt = RestrictionsContext {
bccx: bccx,
span: span,
cause: cause,
loan_region: loan_region,
};

ctxt.restrict(cmt, restr)
ctxt.restrict(cmt)
}

///////////////////////////////////////////////////////////////////////////
Expand All @@ -54,11 +53,8 @@ struct RestrictionsContext<'a> {

impl<'a> RestrictionsContext<'a> {
fn restrict(&self,
cmt: mc::cmt,
restrictions: RestrictionSet) -> RestrictionResult {
debug!("restrict(cmt={}, restrictions={})",
cmt.repr(self.bccx.tcx),
restrictions.repr(self.bccx.tcx));
cmt: mc::cmt) -> RestrictionResult {
debug!("restrict(cmt={})", cmt.repr(self.bccx.tcx));

match cmt.cat.clone() {
mc::cat_rvalue(..) => {
Expand All @@ -75,19 +71,14 @@ impl<'a> RestrictionsContext<'a> {
mc::cat_upvar(ty::UpvarId {var_id: local_id, ..}, _) => {
// R-Variable
let lp = Rc::new(LpVar(local_id));
SafeIf(lp.clone(), vec!(Restriction {
loan_path: lp,
set: restrictions
}))
SafeIf(lp.clone(), vec!(Restriction { loan_path: lp }))
}

mc::cat_downcast(cmt_base) => {
// When we borrow the interior of an enum, we have to
// ensure the enum itself is not mutated, because that
// could cause the type of the memory to change.
self.restrict(
cmt_base,
restrictions | RESTR_MUTATE)
self.restrict(cmt_base)
}

mc::cat_interior(cmt_base, i) => {
Expand All @@ -96,8 +87,8 @@ impl<'a> RestrictionsContext<'a> {
// Overwriting the base would not change the type of
// the memory, so no additional restrictions are
// needed.
let result = self.restrict(cmt_base, restrictions);
self.extend(result, cmt.mutbl, LpInterior(i), restrictions)
let result = self.restrict(cmt_base);
self.extend(result, cmt.mutbl, LpInterior(i))
}

mc::cat_deref(cmt_base, _, pk @ mc::OwnedPtr) |
Expand All @@ -112,10 +103,8 @@ impl<'a> RestrictionsContext<'a> {
// same, because this could be the last ref.
// Eventually we should make these non-special and
// just rely on Deref<T> implementation.
let result = self.restrict(
cmt_base,
restrictions | RESTR_MUTATE);
self.extend(result, cmt.mutbl, LpDeref(pk), restrictions)
let result = self.restrict(cmt_base);
self.extend(result, cmt.mutbl, LpDeref(pk))
}

mc::cat_copied_upvar(..) | // FIXME(#2152) allow mutation of upvars
Expand Down Expand Up @@ -152,8 +141,8 @@ impl<'a> RestrictionsContext<'a> {
return Safe;
}

let result = self.restrict(cmt_base, restrictions);
self.extend(result, cmt.mutbl, LpDeref(pk), restrictions)
let result = self.restrict(cmt_base);
self.extend(result, cmt.mutbl, LpDeref(pk))
}

mc::cat_deref(_, _, mc::UnsafePtr(..)) => {
Expand All @@ -162,24 +151,20 @@ impl<'a> RestrictionsContext<'a> {
}

mc::cat_discr(cmt_base, _) => {
self.restrict(cmt_base, restrictions)
self.restrict(cmt_base)
}
}
}

fn extend(&self,
result: RestrictionResult,
mc: mc::MutabilityCategory,
elem: LoanPathElem,
restrictions: RestrictionSet) -> RestrictionResult {
elem: LoanPathElem) -> RestrictionResult {
match result {
Safe => Safe,
SafeIf(base_lp, mut base_vec) => {
let lp = Rc::new(LpExtend(base_lp, mc, elem));
base_vec.push(Restriction {
loan_path: lp.clone(),
set: restrictions
});
base_vec.push(Restriction { loan_path: lp.clone() });
SafeIf(lp, base_vec)
}
}
Expand Down
53 changes: 1 addition & 52 deletions src/librustc/middle/borrowck/mod.rs
Expand Up @@ -21,7 +21,6 @@ use middle::ty;
use util::ppaux::{note_and_explain_region, Repr, UserString};

use std::cell::{Cell};
use std::ops::{BitOr, BitAnd};
use std::rc::Rc;
use std::gc::{Gc, GC};
use std::string::String;
Expand Down Expand Up @@ -250,56 +249,8 @@ pub fn opt_loan_path(cmt: &mc::cmt) -> Option<Rc<LoanPath>> {
}
}

///////////////////////////////////////////////////////////////////////////
// Restrictions
//
// Borrowing an lvalue often results in *restrictions* that limit what
// can be done with this lvalue during the scope of the loan:
//
// - `RESTR_MUTATE`: The lvalue may not be modified or `&mut` borrowed.
// - `RESTR_FREEZE`: `&` borrows of the lvalue are forbidden.
//
// In addition, no value which is restricted may be moved. Therefore,
// restrictions are meaningful even if the RestrictionSet is empty,
// because the restriction against moves is implied.

pub struct Restriction {
loan_path: Rc<LoanPath>,
set: RestrictionSet
}

#[deriving(PartialEq)]
pub struct RestrictionSet {
bits: u32
}

#[allow(dead_code)] // potentially useful
pub static RESTR_EMPTY: RestrictionSet = RestrictionSet {bits: 0b0000};
pub static RESTR_MUTATE: RestrictionSet = RestrictionSet {bits: 0b0001};
pub static RESTR_FREEZE: RestrictionSet = RestrictionSet {bits: 0b0010};

impl RestrictionSet {
pub fn intersects(&self, restr: RestrictionSet) -> bool {
(self.bits & restr.bits) != 0
}
}

impl BitOr<RestrictionSet,RestrictionSet> for RestrictionSet {
fn bitor(&self, rhs: &RestrictionSet) -> RestrictionSet {
RestrictionSet {bits: self.bits | rhs.bits}
}
}

impl BitAnd<RestrictionSet,RestrictionSet> for RestrictionSet {
fn bitand(&self, rhs: &RestrictionSet) -> RestrictionSet {
RestrictionSet {bits: self.bits & rhs.bits}
}
}

impl Repr for RestrictionSet {
fn repr(&self, _tcx: &ty::ctxt) -> String {
format!("RestrictionSet(0x{:x})", self.bits as uint)
}
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -832,9 +783,7 @@ impl Repr for Loan {

impl Repr for Restriction {
fn repr(&self, tcx: &ty::ctxt) -> String {
(format!("Restriction({}, {:x})",
self.loan_path.repr(tcx),
self.set.bits as uint)).to_string()
(format!("Restriction({})", self.loan_path.repr(tcx))).to_string()
}
}

Expand Down

0 comments on commit 59309e0

Please sign in to comment.