Skip to content

Commit 679b5a9

Browse files
committed
Auto merge of #29327 - sanxiyn:argument, r=nrc
Fix #24114.
2 parents eab5ad5 + 4e2189f commit 679b5a9

File tree

9 files changed

+74
-38
lines changed

9 files changed

+74
-38
lines changed

src/librustc/front/map/collector.rs

-17
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,6 @@ impl<'ast> NodeCollector<'ast> {
104104
let entry = MapEntry::from_node(self.parent_node, node);
105105
self.insert_entry(id, entry);
106106
}
107-
108-
fn visit_fn_decl(&mut self, decl: &'ast FnDecl) {
109-
for a in &decl.inputs {
110-
self.insert(a.id, NodeArg(&*a.pat));
111-
}
112-
}
113107
}
114108

115109
impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
@@ -295,20 +289,9 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
295289
fn visit_fn(&mut self, fk: visit::FnKind<'ast>, fd: &'ast FnDecl,
296290
b: &'ast Block, s: Span, id: NodeId) {
297291
assert_eq!(self.parent_node, id);
298-
self.visit_fn_decl(fd);
299292
visit::walk_fn(self, fk, fd, b, s);
300293
}
301294

302-
fn visit_ty(&mut self, ty: &'ast Ty) {
303-
match ty.node {
304-
TyBareFn(ref fd) => {
305-
self.visit_fn_decl(&*fd.decl);
306-
}
307-
_ => {}
308-
}
309-
visit::walk_ty(self, ty);
310-
}
311-
312295
fn visit_block(&mut self, block: &'ast Block) {
313296
self.insert(block.id, NodeBlock(block));
314297
let parent_node = self.parent_node;

src/librustc/front/map/mod.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ pub enum Node<'ast> {
118118
NodeVariant(&'ast Variant),
119119
NodeExpr(&'ast Expr),
120120
NodeStmt(&'ast Stmt),
121-
NodeArg(&'ast Pat),
122121
NodeLocal(&'ast Pat),
123122
NodePat(&'ast Pat),
124123
NodeBlock(&'ast Block),
@@ -145,7 +144,6 @@ pub enum MapEntry<'ast> {
145144
EntryVariant(NodeId, &'ast Variant),
146145
EntryExpr(NodeId, &'ast Expr),
147146
EntryStmt(NodeId, &'ast Stmt),
148-
EntryArg(NodeId, &'ast Pat),
149147
EntryLocal(NodeId, &'ast Pat),
150148
EntryPat(NodeId, &'ast Pat),
151149
EntryBlock(NodeId, &'ast Block),
@@ -180,7 +178,6 @@ impl<'ast> MapEntry<'ast> {
180178
NodeVariant(n) => EntryVariant(p, n),
181179
NodeExpr(n) => EntryExpr(p, n),
182180
NodeStmt(n) => EntryStmt(p, n),
183-
NodeArg(n) => EntryArg(p, n),
184181
NodeLocal(n) => EntryLocal(p, n),
185182
NodePat(n) => EntryPat(p, n),
186183
NodeBlock(n) => EntryBlock(p, n),
@@ -199,7 +196,6 @@ impl<'ast> MapEntry<'ast> {
199196
EntryVariant(id, _) => id,
200197
EntryExpr(id, _) => id,
201198
EntryStmt(id, _) => id,
202-
EntryArg(id, _) => id,
203199
EntryLocal(id, _) => id,
204200
EntryPat(id, _) => id,
205201
EntryBlock(id, _) => id,
@@ -219,7 +215,6 @@ impl<'ast> MapEntry<'ast> {
219215
EntryVariant(_, n) => NodeVariant(n),
220216
EntryExpr(_, n) => NodeExpr(n),
221217
EntryStmt(_, n) => NodeStmt(n),
222-
EntryArg(_, n) => NodeArg(n),
223218
EntryLocal(_, n) => NodeLocal(n),
224219
EntryPat(_, n) => NodePat(n),
225220
EntryBlock(_, n) => NodeBlock(n),
@@ -348,6 +343,27 @@ impl<'ast> Map<'ast> {
348343
self.find_entry(id).and_then(|x| x.parent_node()).unwrap_or(id)
349344
}
350345

346+
/// Check if the node is an argument. An argument is a local variable whose
347+
/// immediate parent is an item or a closure.
348+
pub fn is_argument(&self, id: NodeId) -> bool {
349+
match self.find(id) {
350+
Some(NodeLocal(_)) => (),
351+
_ => return false,
352+
}
353+
match self.find(self.get_parent_node(id)) {
354+
Some(NodeItem(_)) |
355+
Some(NodeTraitItem(_)) |
356+
Some(NodeImplItem(_)) => true,
357+
Some(NodeExpr(e)) => {
358+
match e.node {
359+
ExprClosure(..) => true,
360+
_ => false,
361+
}
362+
}
363+
_ => false,
364+
}
365+
}
366+
351367
/// If there is some error when walking the parents (e.g., a node does not
352368
/// have a parent in the map or a node can't be found), then we return the
353369
/// last good node id we found. Note that reaching the crate root (id == 0),
@@ -628,7 +644,7 @@ impl<'ast> Map<'ast> {
628644
Some(NodeVariant(variant)) => variant.span,
629645
Some(NodeExpr(expr)) => expr.span,
630646
Some(NodeStmt(stmt)) => stmt.span,
631-
Some(NodeArg(pat)) | Some(NodeLocal(pat)) => pat.span,
647+
Some(NodeLocal(pat)) => pat.span,
632648
Some(NodePat(pat)) => pat.span,
633649
Some(NodeBlock(block)) => block.span,
634650
Some(NodeStructCtor(_)) => self.expect_item(self.get_parent(id)).span,
@@ -886,7 +902,6 @@ impl<'a> NodePrinter for pprust::State<'a> {
886902
// ast_map to reconstruct their full structure for pretty
887903
// printing.
888904
NodeLocal(_) => panic!("cannot print isolated Local"),
889-
NodeArg(_) => panic!("cannot print isolated Arg"),
890905
NodeStructCtor(_) => panic!("cannot print isolated StructCtor"),
891906
}
892907
}
@@ -965,9 +980,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
965980
Some(NodeStmt(ref stmt)) => {
966981
format!("stmt {}{}", pprust::stmt_to_string(&**stmt), id_str)
967982
}
968-
Some(NodeArg(ref pat)) => {
969-
format!("arg {}{}", pprust::pat_to_string(&**pat), id_str)
970-
}
971983
Some(NodeLocal(ref pat)) => {
972984
format!("local {}{}", pprust::pat_to_string(&**pat), id_str)
973985
}

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl MutabilityCategory {
305305

306306
fn from_local(tcx: &ty::ctxt, id: ast::NodeId) -> MutabilityCategory {
307307
let ret = match tcx.map.get(id) {
308-
ast_map::NodeLocal(p) | ast_map::NodeArg(p) => match p.node {
308+
ast_map::NodeLocal(p) => match p.node {
309309
hir::PatIdent(bind_mode, _, _) => {
310310
if bind_mode == hir::BindByValue(hir::MutMutable) {
311311
McDeclared
@@ -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) => {

src/librustc_trans/trans/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1976,7 +1976,7 @@ pub fn create_captured_var_metadata<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
19761976
None => {
19771977
cx.sess().span_bug(span, "debuginfo::create_captured_var_metadata: node not found");
19781978
}
1979-
Some(hir_map::NodeLocal(pat)) | Some(hir_map::NodeArg(pat)) => {
1979+
Some(hir_map::NodeLocal(pat)) => {
19801980
match pat.node {
19811981
hir::PatIdent(_, ref path1, _) => {
19821982
path1.node.name

src/librustc_trans/trans/monomorphize.rs

-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
262262
hir_map::NodeTyParam(..) |
263263
hir_map::NodeExpr(..) |
264264
hir_map::NodeStmt(..) |
265-
hir_map::NodeArg(..) |
266265
hir_map::NodeBlock(..) |
267266
hir_map::NodePat(..) |
268267
hir_map::NodeLocal(..) => {
+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)