Skip to content

Commit d7944ce

Browse files
committed
Distinguish argument from local variable
1 parent 908979d commit d7944ce

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

src/librustc/front/map/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,27 @@ impl<'ast> Map<'ast> {
348348
self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id)
349349
}
350350

351+
/// Check if the node is an argument. An argument is a local variable whose
352+
/// immediate parent is an item or a closure.
353+
pub fn is_argument(&self, id: NodeId) -> bool {
354+
match self.find(id) {
355+
Some(NodeLocal(_)) => (),
356+
_ => return false,
357+
}
358+
match self.find(self.get_parent_node(id)) {
359+
Some(NodeItem(_)) |
360+
Some(NodeTraitItem(_)) |
361+
Some(NodeImplItem(_)) => true,
362+
Some(NodeExpr(e)) => {
363+
match e.node {
364+
ExprClosure(..) => true,
365+
_ => false,
366+
}
367+
}
368+
_ => false,
369+
}
370+
}
371+
351372
/// If there is some error when walking the parents (e.g., a node does not
352373
/// have a parent in the map or a node can't be found), then we return the
353374
/// last good node id we found. Note that reaching the crate root (id == 0),

src/librustc/middle/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ enum PassArgs {
278278
impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
279279
pub fn new(delegate: &'d mut (Delegate<'tcx>),
280280
typer: &'t infer::InferCtxt<'a, 'tcx>)
281-
-> ExprUseVisitor<'d,'t,'a,'tcx> where 'tcx:'a
281+
-> ExprUseVisitor<'d,'t,'a,'tcx> where 'tcx:'a+'d
282282
{
283283
let mc: mc::MemCategorizationContext<'t, 'a, 'tcx> =
284284
mc::MemCategorizationContext::new(typer);

src/librustc/middle/mem_categorization.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1463,11 +1463,10 @@ impl<'tcx> cmt_<'tcx> {
14631463
"non-lvalue".to_string()
14641464
}
14651465
cat_local(vid) => {
1466-
match tcx.map.find(vid) {
1467-
Some(ast_map::NodeArg(_)) => {
1468-
"argument".to_string()
1469-
}
1470-
_ => "local variable".to_string()
1466+
if tcx.map.is_argument(vid) {
1467+
"argument".to_string()
1468+
} else {
1469+
"local variable".to_string()
14711470
}
14721471
}
14731472
cat_deref(_, _, pk) => {
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[derive(Copy, Clone)]
12+
struct S;
13+
14+
impl S {
15+
fn mutate(&mut self) {
16+
}
17+
}
18+
19+
fn func(arg: S) {
20+
arg.mutate(); //~ ERROR: cannot borrow immutable argument
21+
}
22+
23+
impl S {
24+
fn method(&self, arg: S) {
25+
arg.mutate(); //~ ERROR: cannot borrow immutable argument
26+
}
27+
}
28+
29+
trait T {
30+
fn default(&self, arg: S) {
31+
arg.mutate(); //~ ERROR: cannot borrow immutable argument
32+
}
33+
}
34+
35+
impl T for S {}
36+
37+
fn main() {
38+
let s = S;
39+
func(s);
40+
s.method(s);
41+
s.default(s);
42+
(|arg: S| { arg.mutate() })(s); //~ ERROR: cannot borrow immutable argument
43+
}

src/test/compile-fail/borrowck-closures-unique.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn d(x: &mut isize) {
4343
}
4444

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

4949
fn main() {

src/test/compile-fail/borrowck-unboxed-closures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn a<F:Fn(isize, isize) -> isize>(mut f: F) {
1717
}
1818

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

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

0 commit comments

Comments
 (0)