diff --git a/src/librustc/front/map/mod.rs b/src/librustc/front/map/mod.rs index 2d84f6fc2be..68ed0cda5f1 100644 --- a/src/librustc/front/map/mod.rs +++ b/src/librustc/front/map/mod.rs @@ -348,6 +348,27 @@ pub fn get_parent_node(&self, id: NodeId) -> NodeId { 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), diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index e1f8aaead88..fb3a6b0f420 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -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); diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index 555f864befb..186a8b1390e 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -1463,11 +1463,10 @@ pub fn descriptive_string(&self, tcx: &ty::ctxt) -> String { "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) => { diff --git a/src/test/compile-fail/borrowck-argument.rs b/src/test/compile-fail/borrowck-argument.rs new file mode 100644 index 00000000000..3230689e53c --- /dev/null +++ b/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 or the MIT license +// , 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 +} diff --git a/src/test/compile-fail/borrowck-closures-unique.rs b/src/test/compile-fail/borrowck-closures-unique.rs index 9410181659c..3646a68f06f 100644 --- a/src/test/compile-fail/borrowck-closures-unique.rs +++ b/src/test/compile-fail/borrowck-closures-unique.rs @@ -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() { diff --git a/src/test/compile-fail/borrowck-unboxed-closures.rs b/src/test/compile-fail/borrowck-unboxed-closures.rs index 3eca850e493..1c12ca9c1de 100644 --- a/src/test/compile-fail/borrowck-unboxed-closures.rs +++ b/src/test/compile-fail/borrowck-unboxed-closures.rs @@ -17,7 +17,7 @@ fn a isize>(mut f: F) { } fn b isize>(f: F) { - f(1, 2); //~ ERROR cannot borrow immutable local variable + f(1, 2); //~ ERROR cannot borrow immutable argument } fn c isize>(f: F) {