Skip to content

Commit

Permalink
Distinguish argument from local variable
Browse files Browse the repository at this point in the history
  • Loading branch information
sanxiyn committed Oct 26, 2015
1 parent 908979d commit d7944ce
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
21 changes: 21 additions & 0 deletions src/librustc/front/map/mod.rs
Expand Up @@ -348,6 +348,27 @@ impl<'ast> Map<'ast> {
self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id)
}

/// Check if the node is an argument. An argument is a local variable whose
/// immediate parent is an item or a closure.
pub fn is_argument(&self, id: NodeId) -> bool {
match self.find(id) {
Some(NodeLocal(_)) => (),
_ => return false,
}
match self.find(self.get_parent_node(id)) {
Some(NodeItem(_)) |
Some(NodeTraitItem(_)) |
Some(NodeImplItem(_)) => true,
Some(NodeExpr(e)) => {
match e.node {
ExprClosure(..) => true,
_ => false,
}
}
_ => false,
}
}

/// If there is some error when walking the parents (e.g., a node does not
/// have a parent in the map or a node can't be found), then we return the
/// last good node id we found. Note that reaching the crate root (id == 0),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -278,7 +278,7 @@ enum PassArgs {
impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
pub fn new(delegate: &'d mut (Delegate<'tcx>),
typer: &'t infer::InferCtxt<'a, 'tcx>)
-> ExprUseVisitor<'d,'t,'a,'tcx> where 'tcx:'a
-> ExprUseVisitor<'d,'t,'a,'tcx> where 'tcx:'a+'d
{
let mc: mc::MemCategorizationContext<'t, 'a, 'tcx> =
mc::MemCategorizationContext::new(typer);
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -1463,11 +1463,10 @@ impl<'tcx> cmt_<'tcx> {
"non-lvalue".to_string()
}
cat_local(vid) => {
match tcx.map.find(vid) {
Some(ast_map::NodeArg(_)) => {
"argument".to_string()
}
_ => "local variable".to_string()
if tcx.map.is_argument(vid) {
"argument".to_string()
} else {
"local variable".to_string()
}
}
cat_deref(_, _, pk) => {
Expand Down
43 changes: 43 additions & 0 deletions src/test/compile-fail/borrowck-argument.rs
@@ -0,0 +1,43 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[derive(Copy, Clone)]
struct S;

impl S {
fn mutate(&mut self) {
}
}

fn func(arg: S) {
arg.mutate(); //~ ERROR: cannot borrow immutable argument
}

impl S {
fn method(&self, arg: S) {
arg.mutate(); //~ ERROR: cannot borrow immutable argument
}
}

trait T {
fn default(&self, arg: S) {
arg.mutate(); //~ ERROR: cannot borrow immutable argument
}
}

impl T for S {}

fn main() {
let s = S;
func(s);
s.method(s);
s.default(s);
(|arg: S| { arg.mutate() })(s); //~ ERROR: cannot borrow immutable argument
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/borrowck-closures-unique.rs
Expand Up @@ -43,7 +43,7 @@ fn d(x: &mut isize) {
}

fn e(x: &mut isize) {
let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable local variable
let c1 = || x = panic!(); //~ ERROR closure cannot assign to immutable argument
}

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/borrowck-unboxed-closures.rs
Expand Up @@ -17,7 +17,7 @@ fn a<F:Fn(isize, isize) -> isize>(mut f: F) {
}

fn b<F:FnMut(isize, isize) -> isize>(f: F) {
f(1, 2); //~ ERROR cannot borrow immutable local variable
f(1, 2); //~ ERROR cannot borrow immutable argument
}

fn c<F:FnOnce(isize, isize) -> isize>(f: F) {
Expand Down

0 comments on commit d7944ce

Please sign in to comment.