Skip to content

Commit

Permalink
rustc: de-@ mem_categorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Apr 22, 2014
1 parent d55deae commit fa33012
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 175 deletions.
25 changes: 12 additions & 13 deletions src/librustc/middle/borrowck/check_loans.rs
Expand Up @@ -390,10 +390,9 @@ impl<'a> CheckLoanCtxt<'a> {
// Mutable values can be assigned, as long as they obey loans
// and aliasing restrictions:
if cmt.mutbl.is_mutable() {
if check_for_aliasable_mutable_writes(self, expr, cmt) {
if check_for_aliasable_mutable_writes(self, expr, cmt.clone()) {
if check_for_assignment_to_restricted_or_frozen_location(
self, expr, cmt)
{
self, expr, cmt.clone()) {
// Safe, but record for lint pass later:
mark_variable_as_used_mut(self, cmt);
}
Expand All @@ -403,9 +402,9 @@ impl<'a> CheckLoanCtxt<'a> {

// For immutable local variables, assignments are legal
// if they cannot already have been assigned
if self.is_local_variable(cmt) {
if self.is_local_variable(cmt.clone()) {
assert!(cmt.mutbl.is_immutable()); // no "const" locals
let lp = opt_loan_path(cmt).unwrap();
let lp = opt_loan_path(&cmt).unwrap();
self.move_data.each_assignment_of(expr.id, &lp, |assign| {
self.bccx.report_reassigned_immutable_variable(
expr.span,
Expand All @@ -417,21 +416,21 @@ impl<'a> CheckLoanCtxt<'a> {
}

// Otherwise, just a plain error.
match opt_loan_path(cmt) {
match opt_loan_path(&cmt) {
Some(lp) => {
self.bccx.span_err(
expr.span,
format!("cannot assign to {} {} `{}`",
cmt.mutbl.to_user_str(),
self.bccx.cmt_to_str(cmt),
self.bccx.cmt_to_str(&*cmt),
self.bccx.loan_path_to_str(&*lp)));
}
None => {
self.bccx.span_err(
expr.span,
format!("cannot assign to {} {}",
cmt.mutbl.to_user_str(),
self.bccx.cmt_to_str(cmt)));
self.bccx.cmt_to_str(&*cmt)));
}
}
return;
Expand All @@ -448,7 +447,7 @@ impl<'a> CheckLoanCtxt<'a> {
loop {
debug!("mark_writes_through_upvars_as_used_mut(cmt={})",
cmt.repr(this.tcx()));
match cmt.cat {
match cmt.cat.clone() {
mc::cat_local(id) | mc::cat_arg(id) => {
this.tcx().used_mut_nodes.borrow_mut().insert(id);
return;
Expand Down Expand Up @@ -496,10 +495,10 @@ impl<'a> CheckLoanCtxt<'a> {
debug!("check_for_aliasable_mutable_writes(cmt={}, guarantor={})",
cmt.repr(this.tcx()), guarantor.repr(this.tcx()));
match guarantor.cat {
mc::cat_deref(b, _, mc::BorrowedPtr(ty::MutBorrow, _)) => {
mc::cat_deref(ref b, _, mc::BorrowedPtr(ty::MutBorrow, _)) => {
// Statically prohibit writes to `&mut` when aliasable

check_for_aliasability_violation(this, expr, b);
check_for_aliasability_violation(this, expr, b.clone());
}

_ => {}
Expand Down Expand Up @@ -537,7 +536,7 @@ impl<'a> CheckLoanCtxt<'a> {
//! Check for assignments that violate the terms of an
//! outstanding loan.

let loan_path = match opt_loan_path(cmt) {
let loan_path = match opt_loan_path(&cmt) {
Some(lp) => lp,
None => { return true; /* no loan path, can't be any loans */ }
};
Expand Down Expand Up @@ -814,7 +813,7 @@ fn check_loans_in_expr<'a>(this: &mut CheckLoanCtxt<'a>,
if !this.move_data.is_assignee(expr.id) {
let cmt = this.bccx.cat_expr_unadjusted(expr);
debug!("path cmt={}", cmt.repr(this.tcx()));
for lp in opt_loan_path(cmt).iter() {
for lp in opt_loan_path(&cmt).iter() {
this.check_if_path_is_moved(expr.id, expr.span, MovedInUse, lp);
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/librustc/middle/borrowck/gather_loans/gather_moves.rs
Expand Up @@ -23,15 +23,15 @@ use syntax::ast;
use syntax::codemap::Span;
use util::ppaux::Repr;

use std::rc::Rc;

struct GatherMoveInfo {
id: ast::NodeId,
kind: MoveKind,
cmt: mc::cmt,
span_path_opt: Option<MoveSpanAndPath>
}

use std::rc::Rc;

pub fn gather_decl(bccx: &BorrowckCtxt,
move_data: &MoveData,
decl_id: ast::NodeId,
Expand Down Expand Up @@ -107,7 +107,7 @@ fn gather_move(bccx: &BorrowckCtxt,
move_info.id, move_info.cmt.repr(bccx.tcx));

let potentially_illegal_move =
check_and_get_illegal_move_origin(bccx, move_info.cmt);
check_and_get_illegal_move_origin(bccx, &move_info.cmt);
match potentially_illegal_move {
Some(illegal_move_origin) => {
let error = MoveError::with_move_info(illegal_move_origin,
Expand All @@ -118,7 +118,7 @@ fn gather_move(bccx: &BorrowckCtxt,
None => ()
}

match opt_loan_path(move_info.cmt) {
match opt_loan_path(&move_info.cmt) {
Some(loan_path) => {
move_data.add_move(bccx.tcx, loan_path,
move_info.id, move_info.kind);
Expand Down Expand Up @@ -158,14 +158,14 @@ pub fn gather_move_and_assignment(bccx: &BorrowckCtxt,
}

fn check_and_get_illegal_move_origin(bccx: &BorrowckCtxt,
cmt: mc::cmt) -> Option<mc::cmt> {
cmt: &mc::cmt) -> Option<mc::cmt> {
match cmt.cat {
mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
mc::cat_deref(_, _, mc::GcPtr) |
mc::cat_deref(_, _, mc::UnsafePtr(..)) |
mc::cat_upvar(..) | mc::cat_static_item |
mc::cat_copied_upvar(mc::CopiedUpvar { onceness: ast::Many, .. }) => {
Some(cmt)
Some(cmt.clone())
}

// Can move out of captured upvars only if the destination closure
Expand All @@ -181,12 +181,12 @@ fn check_and_get_illegal_move_origin(bccx: &BorrowckCtxt,
None
}

mc::cat_downcast(b) |
mc::cat_interior(b, _) => {
mc::cat_downcast(ref b) |
mc::cat_interior(ref b, _) => {
match ty::get(b.ty).sty {
ty::ty_struct(did, _) | ty::ty_enum(did, _) => {
if ty::has_dtor(bccx.tcx, did) {
Some(cmt)
Some(cmt.clone())
} else {
check_and_get_illegal_move_origin(bccx, b)
}
Expand All @@ -197,8 +197,8 @@ fn check_and_get_illegal_move_origin(bccx: &BorrowckCtxt,
}
}

mc::cat_deref(b, _, mc::OwnedPtr) |
mc::cat_discr(b, _) => {
mc::cat_deref(ref b, _, mc::OwnedPtr) |
mc::cat_discr(ref b, _) => {
check_and_get_illegal_move_origin(bccx, b)
}
}
Expand Down
47 changes: 23 additions & 24 deletions src/librustc/middle/borrowck/gather_loans/lifetime.rs
Expand Up @@ -39,9 +39,9 @@ pub fn guarantee_lifetime(bccx: &BorrowckCtxt,
cause: cause,
loan_region: loan_region,
loan_kind: loan_kind,
cmt_original: cmt,
cmt_original: cmt.clone(),
root_scope_id: root_scope_id};
ctxt.check(cmt, None)
ctxt.check(&cmt, None)
}

///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -69,7 +69,7 @@ impl<'a> GuaranteeLifetimeContext<'a> {
self.bccx.tcx
}

fn check(&self, cmt: mc::cmt, discr_scope: Option<ast::NodeId>) -> R {
fn check(&self, cmt: &mc::cmt, discr_scope: Option<ast::NodeId>) -> R {
//! Main routine. Walks down `cmt` until we find the "guarantor".
debug!("guarantee_lifetime.check(cmt={}, loan_region={})",
cmt.repr(self.bccx.tcx),
Expand All @@ -83,15 +83,14 @@ impl<'a> GuaranteeLifetimeContext<'a> {
mc::cat_upvar(..) |
mc::cat_deref(_, _, mc::BorrowedPtr(..)) | // L-Deref-Borrowed
mc::cat_deref(_, _, mc::UnsafePtr(..)) => {
let scope = self.scope(cmt);
self.check_scope(scope)
self.check_scope(self.scope(cmt))
}

mc::cat_static_item => {
Ok(())
}

mc::cat_deref(base, derefs, mc::GcPtr) => {
mc::cat_deref(ref base, derefs, mc::GcPtr) => {
let base_scope = self.scope(base);

// L-Deref-Managed-Imm-User-Root
Expand All @@ -111,13 +110,13 @@ impl<'a> GuaranteeLifetimeContext<'a> {
}
}

mc::cat_downcast(base) |
mc::cat_deref(base, _, mc::OwnedPtr) | // L-Deref-Send
mc::cat_interior(base, _) => { // L-Field
mc::cat_downcast(ref base) |
mc::cat_deref(ref base, _, mc::OwnedPtr) | // L-Deref-Send
mc::cat_interior(ref base, _) => { // L-Field
self.check(base, discr_scope)
}

mc::cat_discr(base, new_discr_scope) => {
mc::cat_discr(ref base, new_discr_scope) => {
// Subtle: in a match, we must ensure that each binding
// variable remains valid for the duration of the arm in
// which it appears, presuming that this arm is taken.
Expand Down Expand Up @@ -176,7 +175,7 @@ impl<'a> GuaranteeLifetimeContext<'a> {
}

fn is_rvalue_or_immutable(&self,
cmt: mc::cmt) -> bool {
cmt: &mc::cmt) -> bool {
//! We can omit the root on an `@T` value if the location
//! that holds the box is either (1) an rvalue, in which case
//! it is in a non-user-accessible temporary, or (2) an immutable
Expand All @@ -189,8 +188,8 @@ impl<'a> GuaranteeLifetimeContext<'a> {
}

fn check_root(&self,
cmt_deref: mc::cmt,
cmt_base: mc::cmt,
cmt_deref: &mc::cmt,
cmt_base: &mc::cmt,
derefs: uint,
discr_scope: Option<ast::NodeId>) -> R {
debug!("check_root(cmt_deref={}, cmt_base={}, derefs={:?}, \
Expand Down Expand Up @@ -253,7 +252,7 @@ impl<'a> GuaranteeLifetimeContext<'a> {
}
}

fn is_moved(&self, cmt: mc::cmt) -> bool {
fn is_moved(&self, cmt: &mc::cmt) -> bool {
//! True if `cmt` is something that is potentially moved
//! out of the current stack frame.

Expand All @@ -269,17 +268,17 @@ impl<'a> GuaranteeLifetimeContext<'a> {
mc::cat_upvar(..) => {
false
}
r @ mc::cat_downcast(..) |
r @ mc::cat_interior(..) |
r @ mc::cat_discr(..) => {
ref r @ mc::cat_downcast(..) |
ref r @ mc::cat_interior(..) |
ref r @ mc::cat_discr(..) => {
self.tcx().sess.span_bug(
cmt.span,
format!("illegal guarantor category: {:?}", r));
}
}
}

fn scope(&self, cmt: mc::cmt) -> ty::Region {
fn scope(&self, cmt: &mc::cmt) -> ty::Region {
//! Returns the maximal region scope for the which the
//! lvalue `cmt` is guaranteed to be valid without any
//! rooting etc, and presuming `cmt` is not mutated.
Expand Down Expand Up @@ -307,18 +306,18 @@ impl<'a> GuaranteeLifetimeContext<'a> {
mc::cat_deref(_, _, mc::BorrowedPtr(_, r)) => {
r
}
mc::cat_downcast(cmt) |
mc::cat_deref(cmt, _, mc::OwnedPtr) |
mc::cat_deref(cmt, _, mc::GcPtr) |
mc::cat_interior(cmt, _) |
mc::cat_discr(cmt, _) => {
mc::cat_downcast(ref cmt) |
mc::cat_deref(ref cmt, _, mc::OwnedPtr) |
mc::cat_deref(ref cmt, _, mc::GcPtr) |
mc::cat_interior(ref cmt, _) |
mc::cat_discr(ref cmt, _) => {
self.scope(cmt)
}
}
}

fn report_error(&self, code: bckerr_code) {
self.bccx.report(BckError { cmt: self.cmt_original,
self.bccx.report(BckError { cmt: self.cmt_original.clone(),
span: self.span,
cause: self.cause,
code: code });
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/borrowck/gather_loans/mod.rs
Expand Up @@ -230,7 +230,7 @@ fn gather_loans_in_expr(this: &mut GatherLoanCtxt,
let cmt = this.bccx.cat_expr(ex_v);
for arm in arms.iter() {
for pat in arm.pats.iter() {
this.gather_pat(cmt, *pat, Some((arm.body.id, ex.id)));
this.gather_pat(cmt.clone(), *pat, Some((arm.body.id, ex.id)));
}
}
visit::walk_expr(this, ex, ());
Expand Down Expand Up @@ -300,7 +300,7 @@ fn gather_loans_in_expr(this: &mut GatherLoanCtxt,

fn with_assignee_loan_path(bccx: &BorrowckCtxt, expr: &ast::Expr, op: |Rc<LoanPath>|) {
let cmt = bccx.cat_expr(expr);
match opt_loan_path(cmt) {
match opt_loan_path(&cmt) {
Some(lp) => op(lp),
None => {
// This can occur with e.g. `*foo() = 5`. In such
Expand Down Expand Up @@ -552,28 +552,28 @@ impl<'a> GatherLoanCtxt<'a> {
// Check that the lifetime of the borrow does not exceed
// the lifetime of the data being borrowed.
if lifetime::guarantee_lifetime(self.bccx, self.item_ub, root_ub,
borrow_span, cause, cmt, loan_region,
borrow_span, cause, cmt.clone(), loan_region,
req_kind).is_err() {
return; // reported an error, no sense in reporting more.
}

// Check that we don't allow mutable borrows of non-mutable data.
if check_mutability(self.bccx, borrow_span, cause,
cmt, req_kind).is_err() {
cmt.clone(), req_kind).is_err() {
return; // reported an error, no sense in reporting more.
}

// Check that we don't allow mutable borrows of aliasable data.
if check_aliasability(self.bccx, borrow_span, cause,
cmt, req_kind).is_err() {
cmt.clone(), req_kind).is_err() {
return; // reported an error, no sense in reporting more.
}

// Compute the restrictions that are required to enforce the
// loan is safe.
let restr = restrictions::compute_restrictions(
self.bccx, borrow_span, cause,
cmt, loan_region, self.restriction_set(req_kind));
cmt.clone(), loan_region, self.restriction_set(req_kind));

// Create the loan record (if needed).
let loan = match restr {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/borrowck/gather_loans/move_error.rs
Expand Up @@ -79,7 +79,7 @@ pub struct GroupedMoveErrors {
fn report_move_errors(bccx: &BorrowckCtxt, errors: &Vec<MoveError>) {
let grouped_errors = group_errors_with_same_origin(errors);
for error in grouped_errors.iter() {
report_cannot_move_out_of(bccx, error.move_from);
report_cannot_move_out_of(bccx, error.move_from.clone());
let mut is_first_note = true;
for move_to in error.move_to_places.iter() {
note_move_destination(bccx, move_to.span,
Expand Down Expand Up @@ -112,7 +112,7 @@ fn group_errors_with_same_origin(errors: &Vec<MoveError>)
}
}
grouped_errors.push(GroupedMoveErrors {
move_from: error.move_from,
move_from: error.move_from.clone(),
move_to_places: move_to
})
}
Expand All @@ -128,11 +128,11 @@ fn report_cannot_move_out_of(bccx: &BorrowckCtxt, move_from: mc::cmt) {
bccx.span_err(
move_from.span,
format!("cannot move out of {}",
bccx.cmt_to_str(move_from)));
bccx.cmt_to_str(&*move_from)));
}

mc::cat_downcast(b) |
mc::cat_interior(b, _) => {
mc::cat_downcast(ref b) |
mc::cat_interior(ref b, _) => {
match ty::get(b.ty).sty {
ty::ty_struct(did, _)
| ty::ty_enum(did, _) if ty::has_dtor(bccx.tcx, did) => {
Expand Down

0 comments on commit fa33012

Please sign in to comment.