Distinguish argument from local variable

This commit is contained in:
Seo Sanghyeon 2015-10-27 00:46:11 +09:00
parent 908979d53d
commit d7944cee12
6 changed files with 71 additions and 8 deletions

View File

@ -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),

View File

@ -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);

View File

@ -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) => {

View File

@ -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
}

View File

@ -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() {

View File

@ -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) {