Skip to content

Commit

Permalink
Fix all violations of stronger guarantees for mutable borrows
Browse files Browse the repository at this point in the history
Fix all violations in the Rust source tree of the stronger guarantee
of a unique access path for mutable borrows as described in #12624.
  • Loading branch information
Cameron Zwarich committed Jun 14, 2014
1 parent 036833e commit 159e27a
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 113 deletions.
3 changes: 2 additions & 1 deletion src/libarena/lib.rs
Expand Up @@ -406,7 +406,8 @@ impl<T> TypedArenaChunk<T> {
None => {}
Some(mut next) => {
// We assume that the next chunk is completely filled.
next.destroy(next.capacity)
let capacity = next.capacity;
next.destroy(capacity)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libcollections/ringbuf.rs
Expand Up @@ -66,7 +66,8 @@ impl<T> Deque<T> for RingBuf<T> {

/// Return a mutable reference to the last element in the RingBuf
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
if self.nelts > 0 { Some(self.get_mut(self.nelts - 1)) } else { None }
let nelts = self.nelts;
if nelts > 0 { Some(self.get_mut(nelts - 1)) } else { None }
}

/// Remove and return the first element in the RingBuf, or None if it is empty
Expand Down
11 changes: 7 additions & 4 deletions src/libcollections/vec.rs
Expand Up @@ -114,7 +114,8 @@ impl<T> Vec<T> {
unsafe {
let mut xs = Vec::with_capacity(length);
while xs.len < length {
ptr::write(xs.as_mut_slice().unsafe_mut_ref(xs.len), op(xs.len));
let len = xs.len;
ptr::write(xs.as_mut_slice().unsafe_mut_ref(len), op(len));
xs.len += 1;
}
xs
Expand Down Expand Up @@ -210,7 +211,8 @@ impl<T: Clone> Vec<T> {
unsafe {
let mut xs = Vec::with_capacity(length);
while xs.len < length {
ptr::write(xs.as_mut_slice().unsafe_mut_ref(xs.len),
let len = xs.len;
ptr::write(xs.as_mut_slice().unsafe_mut_ref(len),
value.clone());
xs.len += 1;
}
Expand Down Expand Up @@ -321,9 +323,10 @@ impl<T:Clone> Clone for Vec<T> {
let this_slice = self.as_slice();
while vector.len < len {
unsafe {
let len = vector.len;
ptr::write(
vector.as_mut_slice().unsafe_mut_ref(vector.len),
this_slice.unsafe_ref(vector.len).clone());
vector.as_mut_slice().unsafe_mut_ref(len),
this_slice.unsafe_ref(len).clone());
}
vector.len += 1;
}
Expand Down
6 changes: 4 additions & 2 deletions src/libdebug/repr.rs
Expand Up @@ -127,13 +127,15 @@ impl<'a> ReprVisitor<'a> {
#[inline]
pub fn get<T>(&mut self, f: |&mut ReprVisitor, &T| -> bool) -> bool {
unsafe {
f(self, mem::transmute::<*u8,&T>(self.ptr))
let ptr = self.ptr;
f(self, mem::transmute::<*u8,&T>(ptr))
}
}

#[inline]
pub fn visit_inner(&mut self, inner: *TyDesc) -> bool {
self.visit_ptr_inner(self.ptr, inner)
let ptr = self.ptr;
self.visit_ptr_inner(ptr, inner)
}

#[inline]
Expand Down
5 changes: 2 additions & 3 deletions src/libnative/io/net.rs
Expand Up @@ -637,16 +637,15 @@ impl rtio::RtioUdpSocket for UdpSocket {
mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;

let dolock = || self.lock_nonblocking();
let doread = |nb| unsafe {
let n = try!(read(fd, self.read_deadline, dolock, |nb| unsafe {
let flags = if nb {c::MSG_DONTWAIT} else {0};
libc::recvfrom(fd,
buf.as_mut_ptr() as *mut libc::c_void,
buf.len() as msglen_t,
flags,
storagep,
&mut addrlen) as libc::c_int
};
let n = try!(read(fd, self.read_deadline, dolock, doread));
}));
sockaddr_to_addr(&storage, addrlen as uint).and_then(|addr| {
Ok((n as uint, addr))
})
Expand Down
9 changes: 5 additions & 4 deletions src/libregex/parse/mod.rs
Expand Up @@ -345,18 +345,19 @@ impl<'a> Parser<'a> {
}

fn push_literal(&mut self, c: char) -> Result<(), Error> {
let flags = self.flags;
match c {
'.' => {
self.push(Dot(self.flags))
self.push(Dot(flags))
}
'^' => {
self.push(Begin(self.flags))
self.push(Begin(flags))
}
'$' => {
self.push(End(self.flags))
self.push(End(flags))
}
_ => {
self.push(Literal(c, self.flags))
self.push(Literal(c, flags))
}
}
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/dataflow.rs
Expand Up @@ -300,12 +300,13 @@ impl<'a, O:DataFlowOperator+Clone+'static> DataFlowContext<'a, O> {
}

{
let words_per_id = self.words_per_id;
let mut propcx = PropagationContext {
dfcx: &mut *self,
changed: true
};

let mut temp = Vec::from_elem(self.words_per_id, 0u);
let mut temp = Vec::from_elem(words_per_id, 0u);
let mut loop_scopes = Vec::new();

while propcx.changed {
Expand Down
19 changes: 12 additions & 7 deletions src/librustc/middle/liveness.rs
Expand Up @@ -547,11 +547,13 @@ struct Liveness<'a> {

impl<'a> Liveness<'a> {
fn new(ir: &'a mut IrMaps<'a>, specials: Specials) -> Liveness<'a> {
let num_live_nodes = ir.num_live_nodes;
let num_vars = ir.num_vars;
Liveness {
ir: ir,
s: specials,
successors: Vec::from_elem(ir.num_live_nodes, invalid_node()),
users: Vec::from_elem(ir.num_live_nodes * ir.num_vars, invalid_users()),
successors: Vec::from_elem(num_live_nodes, invalid_node()),
users: Vec::from_elem(num_live_nodes * num_vars, invalid_users()),
loop_scope: Vec::new(),
break_ln: NodeMap::new(),
cont_ln: NodeMap::new(),
Expand Down Expand Up @@ -826,8 +828,9 @@ impl<'a> Liveness<'a> {

debug!("compute: using id for block, {}", block_to_str(body));

let exit_ln = self.s.exit_ln;
let entry_ln: LiveNode =
self.with_loop_nodes(body.id, self.s.exit_ln, self.s.exit_ln,
self.with_loop_nodes(body.id, exit_ln, exit_ln,
|this| this.propagate_through_fn_block(decl, body));

// hack to skip the loop unless debug! is enabled:
Expand All @@ -847,12 +850,13 @@ impl<'a> Liveness<'a> {
-> LiveNode {
// the fallthrough exit is only for those cases where we do not
// explicitly return:
self.init_from_succ(self.s.fallthrough_ln, self.s.exit_ln);
let s = self.s;
self.init_from_succ(s.fallthrough_ln, s.exit_ln);
if blk.expr.is_none() {
self.acc(self.s.fallthrough_ln, self.s.no_ret_var, ACC_READ)
self.acc(s.fallthrough_ln, s.no_ret_var, ACC_READ)
}

self.propagate_through_block(blk, self.s.fallthrough_ln)
self.propagate_through_block(blk, s.fallthrough_ln)
}

fn propagate_through_block(&mut self, blk: &Block, succ: LiveNode)
Expand Down Expand Up @@ -1036,7 +1040,8 @@ impl<'a> Liveness<'a> {

ExprRet(o_e) => {
// ignore succ and subst exit_ln:
self.propagate_through_opt_expr(o_e, self.s.exit_ln)
let exit_ln = self.s.exit_ln;
self.propagate_through_opt_expr(o_e, exit_ln)
}

ExprBreak(opt_label) => {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/privacy.rs
Expand Up @@ -1019,9 +1019,10 @@ impl<'a> Visitor<()> for SanePrivacyVisitor<'a> {
self.check_sane_privacy(item);
}

let in_fn = self.in_fn;
let orig_in_fn = replace(&mut self.in_fn, match item.node {
ast::ItemMod(..) => false, // modules turn privacy back on
_ => self.in_fn, // otherwise we inherit
_ => in_fn, // otherwise we inherit
});
visit::walk_item(self, item, ());
self.in_fn = orig_in_fn;
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/format.rs
Expand Up @@ -202,7 +202,8 @@ impl<'a, 'b> Context<'a, 'b> {
}
parse::CountIsNextParam => {
if self.check_positional_ok() {
self.verify_arg_type(Exact(self.next_arg), Unsigned);
let next_arg = self.next_arg;
self.verify_arg_type(Exact(next_arg), Unsigned);
self.next_arg += 1;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/parse/attr.rs
Expand Up @@ -73,7 +73,8 @@ impl<'a> ParserAttr for Parser<'a> {

let style = if self.eat(&token::NOT) {
if !permit_inner {
self.span_err(self.span,
let span = self.span;
self.span_err(span,
"an inner attribute is not permitted in \
this context");
}
Expand Down

0 comments on commit 159e27a

Please sign in to comment.