Skip to content

Commit bc13365

Browse files
committed
Move unchanging portions of context over to the Visitor.
1 parent 9d07277 commit bc13365

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

src/librustc/middle/stack_check.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,72 +28,69 @@ use util::ppaux::Repr;
2828

2929
#[deriving(Clone)]
3030
struct Context {
31-
tcx: ty::ctxt,
3231
safe_stack: bool
3332
}
3433

35-
struct StackCheckVisitor;
34+
struct StackCheckVisitor {
35+
tcx: ty::ctxt,
36+
}
3637

3738
impl Visitor<Context> for StackCheckVisitor {
3839
fn visit_item(&mut self, i:@ast::item, e:Context) {
39-
stack_check_item(*self, i, e);
40+
stack_check_item(self, i, e);
4041
}
4142
fn visit_fn(&mut self, fk:&visit::fn_kind, fd:&ast::fn_decl,
4243
b:&ast::Block, s:Span, n:ast::NodeId, e:Context) {
43-
stack_check_fn(*self, fk, fd, b, s, n, e);
44+
stack_check_fn(self, fk, fd, b, s, n, e);
4445
}
4546
fn visit_expr(&mut self, ex:@ast::Expr, e:Context) {
46-
stack_check_expr(*self, ex, e);
47+
stack_check_expr(self, ex, e);
4748
}
4849
}
4950

5051
pub fn stack_check_crate(tcx: ty::ctxt,
5152
crate: &ast::Crate) {
52-
let new_cx = Context {
53-
tcx: tcx,
54-
safe_stack: false
55-
};
56-
let mut visitor = StackCheckVisitor;
53+
let new_cx = Context { safe_stack: false };
54+
let mut visitor = StackCheckVisitor { tcx: tcx };
5755
visit::walk_crate(&mut visitor, crate, new_cx);
5856
}
5957

60-
fn stack_check_item(v: StackCheckVisitor,
58+
fn stack_check_item(v: &mut StackCheckVisitor,
6159
item: @ast::item,
6260
in_cx: Context) {
63-
let mut v = v;
6461
match item.node {
6562
ast::item_fn(_, ast::extern_fn, _, _, _) => {
6663
// an extern fn is already being called from C code...
67-
let new_cx = Context {safe_stack: true, ..in_cx};
68-
visit::walk_item(&mut v, item, new_cx);
64+
let new_cx = Context {safe_stack: true};
65+
visit::walk_item(v, item, new_cx);
6966
}
7067
ast::item_fn(*) => {
7168
let safe_stack = fixed_stack_segment(item.attrs);
72-
let new_cx = Context {safe_stack: safe_stack, ..in_cx};
73-
visit::walk_item(&mut v, item, new_cx);
69+
let new_cx = Context {safe_stack: safe_stack};
70+
visit::walk_item(v, item, new_cx);
7471
}
7572
ast::item_impl(_, _, _, ref methods) => {
7673
// visit_method() would make this nicer
7774
for &method in methods.iter() {
7875
let safe_stack = fixed_stack_segment(method.attrs);
79-
let new_cx = Context {safe_stack: safe_stack, ..in_cx};
80-
visit::walk_method_helper(&mut v, method, new_cx);
76+
let new_cx = Context {safe_stack: safe_stack};
77+
visit::walk_method_helper(v, method, new_cx);
8178
}
8279
}
8380
ast::item_trait(_, _, ref methods) => {
8481
for method in methods.iter() {
8582
match *method {
8683
ast::provided(@ref method) => {
8784
let safe_stack = fixed_stack_segment(method.attrs);
88-
let new_cx = Context {safe_stack: safe_stack, ..in_cx};
89-
visit::walk_method_helper(&mut v, method, new_cx);
85+
let new_cx = Context {safe_stack: safe_stack};
86+
visit::walk_method_helper(v, method, new_cx);
9087
}
9188
ast::required(*) => ()
9289
}
9390
}
9491
}
9592
_ => {
96-
visit::walk_item(&mut v, item, in_cx);
93+
visit::walk_item(v, item, in_cx);
9794
}
9895
}
9996

@@ -102,7 +99,7 @@ fn stack_check_item(v: StackCheckVisitor,
10299
}
103100
}
104101

105-
fn stack_check_fn<'a>(v: StackCheckVisitor,
102+
fn stack_check_fn<'a>(v: &mut StackCheckVisitor,
106103
fk: &visit::fn_kind,
107104
decl: &ast::fn_decl,
108105
body: &ast::Block,
@@ -114,7 +111,7 @@ fn stack_check_fn<'a>(v: StackCheckVisitor,
114111
in_cx.safe_stack // see stack_check_item above
115112
}
116113
visit::fk_anon(*) | visit::fk_fn_block => {
117-
match ty::get(ty::node_id_to_type(in_cx.tcx, id)).sty {
114+
match ty::get(ty::node_id_to_type(v.tcx, id)).sty {
118115
ty::ty_bare_fn(*) |
119116
ty::ty_closure(ty::ClosureTy {sigil: ast::OwnedSigil, _}) => {
120117
false
@@ -125,26 +122,25 @@ fn stack_check_fn<'a>(v: StackCheckVisitor,
125122
}
126123
}
127124
};
128-
let new_cx = Context {safe_stack: safe_stack, ..in_cx};
125+
let new_cx = Context {safe_stack: safe_stack};
129126
debug!("stack_check_fn(safe_stack=%b, id=%?)", safe_stack, id);
130-
let mut v = v;
131-
visit::walk_fn(&mut v, fk, decl, body, sp, id, new_cx);
127+
visit::walk_fn(v, fk, decl, body, sp, id, new_cx);
132128
}
133129

134-
fn stack_check_expr<'a>(v: StackCheckVisitor,
130+
fn stack_check_expr<'a>(v: &mut StackCheckVisitor,
135131
expr: @ast::Expr,
136132
cx: Context) {
137133
debug!("stack_check_expr(safe_stack=%b, expr=%s)",
138-
cx.safe_stack, expr.repr(cx.tcx));
134+
cx.safe_stack, expr.repr(v.tcx));
139135
if !cx.safe_stack {
140136
match expr.node {
141137
ast::ExprCall(callee, _, _) => {
142-
let callee_ty = ty::expr_ty(cx.tcx, callee);
143-
debug!("callee_ty=%s", callee_ty.repr(cx.tcx));
138+
let callee_ty = ty::expr_ty(v.tcx, callee);
139+
debug!("callee_ty=%s", callee_ty.repr(v.tcx));
144140
match ty::get(callee_ty).sty {
145141
ty::ty_bare_fn(ref fty) => {
146142
if !fty.abis.is_rust() && !fty.abis.is_intrinsic() {
147-
call_to_extern_fn(cx, callee);
143+
call_to_extern_fn(v, callee);
148144
}
149145
}
150146
_ => {}
@@ -153,18 +149,17 @@ fn stack_check_expr<'a>(v: StackCheckVisitor,
153149
_ => {}
154150
}
155151
}
156-
let mut v = v;
157-
visit::walk_expr(&mut v, expr, cx);
152+
visit::walk_expr(v, expr, cx);
158153
}
159154

160-
fn call_to_extern_fn(cx: Context, callee: @ast::Expr) {
155+
fn call_to_extern_fn(v: &mut StackCheckVisitor, callee: @ast::Expr) {
161156
// Permit direct calls to extern fns that are annotated with
162157
// #[rust_stack]. This is naturally a horrible pain to achieve.
163158
match callee.node {
164159
ast::ExprPath(*) => {
165-
match cx.tcx.def_map.find(&callee.id) {
160+
match v.tcx.def_map.find(&callee.id) {
166161
Some(&ast::DefFn(id, _)) if id.crate == ast::LOCAL_CRATE => {
167-
match cx.tcx.items.find(&id.node) {
162+
match v.tcx.items.find(&id.node) {
168163
Some(&ast_map::node_foreign_item(item, _, _, _)) => {
169164
if attr::contains_name(item.attrs, "rust_stack") {
170165
return;
@@ -179,7 +174,7 @@ fn call_to_extern_fn(cx: Context, callee: @ast::Expr) {
179174
_ => {}
180175
}
181176

182-
cx.tcx.sess.add_lint(lint::cstack,
177+
v.tcx.sess.add_lint(lint::cstack,
183178
callee.id,
184179
callee.span,
185180
fmt!("invoking non-Rust fn in fn without \

0 commit comments

Comments
 (0)