Skip to content

Commit 797a373

Browse files
committed
auto merge of #9492 : pnkfelix/rust/fsk-syntax-visit-refactor-remainder, r=huonw
r? anyone Part of #7081. Removed many unnecessary context arguments, turning them into visitors. Removed some @allocation. If this lands, then I think the only thing left that is unaddressed are: * the various lint visitors, and * middle/privacy.rs, which has `impl<'self> Visitor<&'self method_map> for PrivacyVisitor`
2 parents df97d23 + 3748164 commit 797a373

File tree

10 files changed

+250
-259
lines changed

10 files changed

+250
-259
lines changed

src/librustc/middle/check_const.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ struct env {
226226
idstack: @mut ~[NodeId]
227227
}
228228

229-
struct CheckItemRecursionVisitor;
229+
struct CheckItemRecursionVisitor {
230+
env: env,
231+
}
230232

231233
// Make sure a const item doesn't recursively refer to itself
232234
// FIXME: Should use the dependency graph when it's available (#1356)
@@ -242,34 +244,34 @@ pub fn check_item_recursion(sess: Session,
242244
idstack: @mut ~[]
243245
};
244246

245-
let mut visitor = CheckItemRecursionVisitor;
246-
visitor.visit_item(it, env);
247+
let mut visitor = CheckItemRecursionVisitor { env: env };
248+
visitor.visit_item(it, ());
247249
}
248250

249-
impl Visitor<env> for CheckItemRecursionVisitor {
250-
fn visit_item(&mut self, it: @item, env: env) {
251-
if env.idstack.iter().any(|x| x == &(it.id)) {
252-
env.sess.span_fatal(env.root_it.span, "recursive constant");
251+
impl Visitor<()> for CheckItemRecursionVisitor {
252+
fn visit_item(&mut self, it: @item, _: ()) {
253+
if self.env.idstack.iter().any(|x| x == &(it.id)) {
254+
self.env.sess.span_fatal(self.env.root_it.span, "recursive constant");
253255
}
254-
env.idstack.push(it.id);
255-
visit::walk_item(self, it, env);
256-
env.idstack.pop();
256+
self.env.idstack.push(it.id);
257+
visit::walk_item(self, it, ());
258+
self.env.idstack.pop();
257259
}
258260

259-
fn visit_expr(&mut self, e: @Expr, env: env) {
261+
fn visit_expr(&mut self, e: @Expr, _: ()) {
260262
match e.node {
261-
ExprPath(*) => match env.def_map.find(&e.id) {
263+
ExprPath(*) => match self.env.def_map.find(&e.id) {
262264
Some(&DefStatic(def_id, _)) if ast_util::is_local(def_id) =>
263-
match env.ast_map.get_copy(&def_id.node) {
265+
match self.env.ast_map.get_copy(&def_id.node) {
264266
ast_map::node_item(it, _) => {
265-
self.visit_item(it, env);
267+
self.visit_item(it, ());
266268
}
267269
_ => fail!("const not bound to an item")
268270
},
269271
_ => ()
270272
},
271273
_ => ()
272274
}
273-
visit::walk_expr(self, e, env);
275+
visit::walk_expr(self, e, ());
274276
}
275277
}

src/librustc/middle/kind.rs

+38-45
Original file line numberDiff line numberDiff line change
@@ -54,43 +54,38 @@ use syntax::visit::Visitor;
5454
pub struct Context {
5555
tcx: ty::ctxt,
5656
method_map: typeck::method_map,
57-
current_item: NodeId
5857
}
5958

60-
struct KindAnalysisVisitor;
59+
impl Visitor<()> for Context {
6160

62-
impl Visitor<Context> for KindAnalysisVisitor {
63-
64-
fn visit_expr(&mut self, ex:@Expr, e:Context) {
65-
check_expr(self, ex, e);
61+
fn visit_expr(&mut self, ex:@Expr, _:()) {
62+
check_expr(self, ex);
6663
}
6764

68-
fn visit_fn(&mut self, fk:&visit::fn_kind, fd:&fn_decl, b:&Block, s:Span, n:NodeId, e:Context) {
69-
check_fn(self, fk, fd, b, s, n, e);
65+
fn visit_fn(&mut self, fk:&visit::fn_kind, fd:&fn_decl, b:&Block, s:Span, n:NodeId, _:()) {
66+
check_fn(self, fk, fd, b, s, n);
7067
}
7168

72-
fn visit_ty(&mut self, t:&Ty, e:Context) {
73-
check_ty(self, t, e);
69+
fn visit_ty(&mut self, t:&Ty, _:()) {
70+
check_ty(self, t);
7471
}
75-
fn visit_item(&mut self, i:@item, e:Context) {
76-
check_item(self, i, e);
72+
fn visit_item(&mut self, i:@item, _:()) {
73+
check_item(self, i);
7774
}
7875
}
7976

8077
pub fn check_crate(tcx: ty::ctxt,
8178
method_map: typeck::method_map,
8279
crate: &Crate) {
83-
let ctx = Context {
80+
let mut ctx = Context {
8481
tcx: tcx,
8582
method_map: method_map,
86-
current_item: -1
8783
};
88-
let mut visit = KindAnalysisVisitor;
89-
visit::walk_crate(&mut visit, crate, ctx);
84+
visit::walk_crate(&mut ctx, crate, ());
9085
tcx.sess.abort_if_errors();
9186
}
9287

93-
fn check_struct_safe_for_destructor(cx: Context,
88+
fn check_struct_safe_for_destructor(cx: &mut Context,
9489
span: Span,
9590
struct_did: DefId) {
9691
let struct_tpt = ty::lookup_item_type(cx.tcx, struct_did);
@@ -120,7 +115,7 @@ fn check_struct_safe_for_destructor(cx: Context,
120115
}
121116
}
122117

123-
fn check_impl_of_trait(cx: Context, it: @item, trait_ref: &trait_ref, self_type: &Ty) {
118+
fn check_impl_of_trait(cx: &mut Context, it: @item, trait_ref: &trait_ref, self_type: &Ty) {
124119
let ast_trait_def = cx.tcx.def_map.find(&trait_ref.ref_id)
125120
.expect("trait ref not in def map!");
126121
let trait_def_id = ast_util::def_id_of_def(*ast_trait_def);
@@ -156,7 +151,7 @@ fn check_impl_of_trait(cx: Context, it: @item, trait_ref: &trait_ref, self_type:
156151
}
157152
}
158153

159-
fn check_item(visitor: &mut KindAnalysisVisitor, item: @item, cx: Context) {
154+
fn check_item(cx: &mut Context, item: @item) {
160155
if !attr::contains_name(item.attrs, "unsafe_destructor") {
161156
match item.node {
162157
item_impl(_, Some(ref trait_ref), ref self_type, _) => {
@@ -166,16 +161,15 @@ fn check_item(visitor: &mut KindAnalysisVisitor, item: @item, cx: Context) {
166161
}
167162
}
168163

169-
let cx = Context { current_item: item.id, ..cx };
170-
visit::walk_item(visitor, item, cx);
164+
visit::walk_item(cx, item, ());
171165
}
172166

173167
// Yields the appropriate function to check the kind of closed over
174168
// variables. `id` is the NodeId for some expression that creates the
175169
// closure.
176-
fn with_appropriate_checker(cx: Context, id: NodeId,
177-
b: &fn(checker: &fn(Context, @freevar_entry))) {
178-
fn check_for_uniq(cx: Context, fv: &freevar_entry, bounds: ty::BuiltinBounds) {
170+
fn with_appropriate_checker(cx: &Context, id: NodeId,
171+
b: &fn(checker: &fn(&Context, @freevar_entry))) {
172+
fn check_for_uniq(cx: &Context, fv: &freevar_entry, bounds: ty::BuiltinBounds) {
179173
// all captured data must be owned, regardless of whether it is
180174
// moved in or copied in.
181175
let id = ast_util::def_id_of_def(fv.def).node;
@@ -187,7 +181,7 @@ fn with_appropriate_checker(cx: Context, id: NodeId,
187181
check_freevar_bounds(cx, fv.span, var_t, bounds, None);
188182
}
189183

190-
fn check_for_box(cx: Context, fv: &freevar_entry, bounds: ty::BuiltinBounds) {
184+
fn check_for_box(cx: &Context, fv: &freevar_entry, bounds: ty::BuiltinBounds) {
191185
// all captured data must be owned
192186
let id = ast_util::def_id_of_def(fv.def).node;
193187
let var_t = ty::node_id_to_type(cx.tcx, id);
@@ -198,7 +192,7 @@ fn with_appropriate_checker(cx: Context, id: NodeId,
198192
check_freevar_bounds(cx, fv.span, var_t, bounds, None);
199193
}
200194

201-
fn check_for_block(cx: Context, fv: &freevar_entry,
195+
fn check_for_block(cx: &Context, fv: &freevar_entry,
202196
bounds: ty::BuiltinBounds, region: ty::Region) {
203197
let id = ast_util::def_id_of_def(fv.def).node;
204198
let var_t = ty::node_id_to_type(cx.tcx, id);
@@ -209,7 +203,7 @@ fn with_appropriate_checker(cx: Context, id: NodeId,
209203
bounds, Some(var_t));
210204
}
211205

212-
fn check_for_bare(cx: Context, fv: @freevar_entry) {
206+
fn check_for_bare(cx: &Context, fv: @freevar_entry) {
213207
cx.tcx.sess.span_err(
214208
fv.span,
215209
"can't capture dynamic environment in a fn item; \
@@ -252,13 +246,12 @@ fn with_appropriate_checker(cx: Context, id: NodeId,
252246
// Check that the free variables used in a shared/sendable closure conform
253247
// to the copy/move kind bounds. Then recursively check the function body.
254248
fn check_fn(
255-
v: &mut KindAnalysisVisitor,
249+
cx: &mut Context,
256250
fk: &visit::fn_kind,
257251
decl: &fn_decl,
258252
body: &Block,
259253
sp: Span,
260-
fn_id: NodeId,
261-
cx: Context) {
254+
fn_id: NodeId) {
262255

263256
// Check kinds on free variables:
264257
do with_appropriate_checker(cx, fn_id) |chk| {
@@ -268,10 +261,10 @@ fn check_fn(
268261
}
269262
}
270263

271-
visit::walk_fn(v, fk, decl, body, sp, fn_id, cx);
264+
visit::walk_fn(cx, fk, decl, body, sp, fn_id, ());
272265
}
273266

274-
pub fn check_expr(v: &mut KindAnalysisVisitor, e: @Expr, cx: Context) {
267+
pub fn check_expr(cx: &mut Context, e: @Expr) {
275268
debug!("kind::check_expr(%s)", expr_to_str(e, cx.tcx.sess.intr()));
276269

277270
// Handle any kind bounds on type parameters
@@ -336,10 +329,10 @@ pub fn check_expr(v: &mut KindAnalysisVisitor, e: @Expr, cx: Context) {
336329
}
337330
_ => {}
338331
}
339-
visit::walk_expr(v, e, cx);
332+
visit::walk_expr(cx, e, ());
340333
}
341334

342-
fn check_ty(v: &mut KindAnalysisVisitor, aty: &Ty, cx: Context) {
335+
fn check_ty(cx: &mut Context, aty: &Ty) {
343336
match aty.node {
344337
ty_path(_, _, id) => {
345338
let r = cx.tcx.node_type_substs.find(&id);
@@ -354,11 +347,11 @@ fn check_ty(v: &mut KindAnalysisVisitor, aty: &Ty, cx: Context) {
354347
}
355348
_ => {}
356349
}
357-
visit::walk_ty(v, aty, cx);
350+
visit::walk_ty(cx, aty, ());
358351
}
359352

360353
// Calls "any_missing" if any bounds were missing.
361-
pub fn check_builtin_bounds(cx: Context, ty: ty::t, bounds: ty::BuiltinBounds,
354+
pub fn check_builtin_bounds(cx: &Context, ty: ty::t, bounds: ty::BuiltinBounds,
362355
any_missing: &fn(ty::BuiltinBounds))
363356
{
364357
let kind = ty::type_contents(cx.tcx, ty);
@@ -373,7 +366,7 @@ pub fn check_builtin_bounds(cx: Context, ty: ty::t, bounds: ty::BuiltinBounds,
373366
}
374367
}
375368

376-
pub fn check_typaram_bounds(cx: Context,
369+
pub fn check_typaram_bounds(cx: &Context,
377370
_type_parameter_id: NodeId,
378371
sp: Span,
379372
ty: ty::t,
@@ -389,7 +382,7 @@ pub fn check_typaram_bounds(cx: Context,
389382
}
390383
}
391384

392-
pub fn check_freevar_bounds(cx: Context, sp: Span, ty: ty::t,
385+
pub fn check_freevar_bounds(cx: &Context, sp: Span, ty: ty::t,
393386
bounds: ty::BuiltinBounds, referenced_ty: Option<ty::t>)
394387
{
395388
do check_builtin_bounds(cx, ty, bounds) |missing| {
@@ -412,7 +405,7 @@ pub fn check_freevar_bounds(cx: Context, sp: Span, ty: ty::t,
412405
}
413406
}
414407

415-
pub fn check_trait_cast_bounds(cx: Context, sp: Span, ty: ty::t,
408+
pub fn check_trait_cast_bounds(cx: &Context, sp: Span, ty: ty::t,
416409
bounds: ty::BuiltinBounds) {
417410
do check_builtin_bounds(cx, ty, bounds) |missing| {
418411
cx.tcx.sess.span_err(sp,
@@ -423,7 +416,7 @@ pub fn check_trait_cast_bounds(cx: Context, sp: Span, ty: ty::t,
423416
}
424417
}
425418

426-
fn is_nullary_variant(cx: Context, ex: @Expr) -> bool {
419+
fn is_nullary_variant(cx: &Context, ex: @Expr) -> bool {
427420
match ex.node {
428421
ExprPath(_) => {
429422
match cx.tcx.def_map.get_copy(&ex.id) {
@@ -437,7 +430,7 @@ fn is_nullary_variant(cx: Context, ex: @Expr) -> bool {
437430
}
438431
}
439432

440-
fn check_imm_free_var(cx: Context, def: Def, sp: Span) {
433+
fn check_imm_free_var(cx: &Context, def: Def, sp: Span) {
441434
match def {
442435
DefLocal(_, is_mutbl) => {
443436
if is_mutbl {
@@ -457,7 +450,7 @@ fn check_imm_free_var(cx: Context, def: Def, sp: Span) {
457450
}
458451
}
459452

460-
fn check_copy(cx: Context, ty: ty::t, sp: Span, reason: &str) {
453+
fn check_copy(cx: &Context, ty: ty::t, sp: Span, reason: &str) {
461454
debug!("type_contents(%s)=%s",
462455
ty_to_str(cx.tcx, ty),
463456
ty::type_contents(cx.tcx, ty).to_str());
@@ -469,7 +462,7 @@ fn check_copy(cx: Context, ty: ty::t, sp: Span, reason: &str) {
469462
}
470463
}
471464

472-
pub fn check_send(cx: Context, ty: ty::t, sp: Span) -> bool {
465+
pub fn check_send(cx: &Context, ty: ty::t, sp: Span) -> bool {
473466
if !ty::type_is_sendable(cx.tcx, ty) {
474467
cx.tcx.sess.span_err(
475468
sp, fmt!("value has non-sendable type `%s`",
@@ -525,7 +518,7 @@ pub fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: Span) -> bool {
525518
///
526519
/// FIXME(#5723)---This code should probably move into regionck.
527520
pub fn check_cast_for_escaping_regions(
528-
cx: Context,
521+
cx: &Context,
529522
source: &Expr,
530523
target: &Expr)
531524
{
@@ -601,7 +594,7 @@ pub fn check_cast_for_escaping_regions(
601594
}
602595
}
603596

604-
fn is_subregion_of(cx: Context, r_sub: ty::Region, r_sup: ty::Region) -> bool {
597+
fn is_subregion_of(cx: &Context, r_sub: ty::Region, r_sup: ty::Region) -> bool {
605598
cx.tcx.region_maps.is_subregion_of(r_sub, r_sup)
606599
}
607600
}

0 commit comments

Comments
 (0)