Skip to content

Commit efbbb51

Browse files
committed
auto merge of #15691 : jbclements/rust/method-field-cleanup, r=alexcrichton
This patch applies the excellent suggestion of @pnkfelix to group the helper methods for method field access into a Trait, making the code much more readable, and much more similar to the way it was before.
2 parents 6c35d51 + ca05828 commit efbbb51

File tree

18 files changed

+180
-134
lines changed

18 files changed

+180
-134
lines changed

Diff for: src/librustc/metadata/encoder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use syntax::ast_map::{PathElem, PathElems};
4343
use syntax::ast_map;
4444
use syntax::ast_util::*;
4545
use syntax::ast_util;
46+
use syntax::ast_util::PostExpansionMethod;
4647
use syntax::attr;
4748
use syntax::attr::AttrMetaMethods;
4849
use syntax::diagnostic::SpanHandler;
@@ -798,7 +799,7 @@ fn encode_info_for_method(ecx: &EncodeContext,
798799
} else {
799800
encode_symbol(ecx, ebml_w, m.def_id.node);
800801
}
801-
encode_method_argument_names(ebml_w, method_fn_decl(&*ast_method));
802+
encode_method_argument_names(ebml_w, &*ast_method.pe_fn_decl());
802803
}
803804

804805
ebml_w.end_tag();
@@ -1240,7 +1241,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12401241
encode_method_sort(ebml_w, 'p');
12411242
encode_inlined_item(ecx, ebml_w,
12421243
IIMethodRef(def_id, true, &*m));
1243-
encode_method_argument_names(ebml_w, method_fn_decl(m));
1244+
encode_method_argument_names(ebml_w, m.pe_fn_decl());
12441245
}
12451246
}
12461247

Diff for: src/librustc/middle/astencode.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use middle::{ty, typeck};
3131
use util::ppaux::ty_to_string;
3232

3333
use syntax::{ast, ast_map, ast_util, codemap, fold};
34+
use syntax::ast_util::PostExpansionMethod;
3435
use syntax::codemap::Span;
3536
use syntax::fold::Folder;
3637
use syntax::parse::token;
@@ -136,7 +137,7 @@ pub fn decode_inlined_item(cdata: &cstore::crate_metadata,
136137
let ident = match ii {
137138
ast::IIItem(i) => i.ident,
138139
ast::IIForeign(i) => i.ident,
139-
ast::IIMethod(_, _, m) => ast_util::method_ident(&*m),
140+
ast::IIMethod(_, _, m) => m.pe_ident(),
140141
};
141142
debug!("Fn named: {}", token::get_ident(ident));
142143
debug!("< Decoded inlined fn: {}::{}",

Diff for: src/librustc/middle/dead.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use util::nodemap::NodeSet;
2222
use std::collections::HashSet;
2323
use syntax::ast;
2424
use syntax::ast_map;
25-
use syntax::ast_util;
26-
use syntax::ast_util::{local_def, is_local};
25+
use syntax::ast_util::{local_def, is_local, PostExpansionMethod};
2726
use syntax::attr::AttrMetaMethods;
2827
use syntax::attr;
2928
use syntax::codemap;
@@ -213,7 +212,7 @@ impl<'a> MarkSymbolVisitor<'a> {
213212
visit::walk_trait_method(self, &*trait_method, ctxt);
214213
}
215214
ast_map::NodeMethod(method) => {
216-
visit::walk_block(self, ast_util::method_body(&*method), ctxt);
215+
visit::walk_block(self, method.pe_body(), ctxt);
217216
}
218217
ast_map::NodeForeignItem(foreign_item) => {
219218
visit::walk_foreign_item(self, &*foreign_item, ctxt);
@@ -521,8 +520,7 @@ impl<'a> Visitor<()> for DeadVisitor<'a> {
521520
// Overwrite so that we don't warn the trait method itself.
522521
fn visit_trait_method(&mut self, trait_method: &ast::TraitMethod, _: ()) {
523522
match *trait_method {
524-
ast::Provided(ref method) => visit::walk_block(self,
525-
ast_util::method_body(&**method), ()),
523+
ast::Provided(ref method) => visit::walk_block(self, method.pe_body(), ()),
526524
ast::Required(_) => ()
527525
}
528526
}

Diff for: src/librustc/middle/effect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use middle::typeck::MethodCall;
1717
use util::ppaux;
1818

1919
use syntax::ast;
20-
use syntax::ast_util;
20+
use syntax::ast_util::PostExpansionMethod;
2121
use syntax::codemap::Span;
2222
use syntax::visit;
2323
use syntax::visit::Visitor;
@@ -95,7 +95,7 @@ impl<'a> Visitor<()> for EffectCheckVisitor<'a> {
9595
visit::FkItemFn(_, _, fn_style, _) =>
9696
(true, fn_style == ast::UnsafeFn),
9797
visit::FkMethod(_, _, method) =>
98-
(true, ast_util::method_fn_style(method) == ast::UnsafeFn),
98+
(true, method.pe_fn_style() == ast::UnsafeFn),
9999
_ => (false, false),
100100
};
101101

Diff for: src/librustc/middle/privacy.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ use util::nodemap::{NodeMap, NodeSet};
2626

2727
use syntax::ast;
2828
use syntax::ast_map;
29-
use syntax::ast_util;
30-
use syntax::ast_util::{is_local, local_def};
29+
use syntax::ast_util::{is_local, local_def, PostExpansionMethod};
3130
use syntax::attr;
3231
use syntax::codemap::Span;
3332
use syntax::parse::token;
@@ -264,10 +263,10 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {
264263

265264
if public_ty || public_trait {
266265
for method in methods.iter() {
267-
let meth_public = match ast_util::method_explicit_self(&**method).node {
266+
let meth_public = match method.pe_explicit_self().node {
268267
ast::SelfStatic => public_ty,
269268
_ => true,
270-
} && ast_util::method_vis(&**method) == ast::Public;
269+
} && method.pe_vis() == ast::Public;
271270
if meth_public || tr.is_some() {
272271
self.exported_items.insert(method.id);
273272
}
@@ -457,8 +456,8 @@ impl<'a> PrivacyVisitor<'a> {
457456
let imp = self.tcx.map.get_parent_did(closest_private_id);
458457
match ty::impl_trait_ref(self.tcx, imp) {
459458
Some(..) => return Allowable,
460-
_ if ast_util::method_vis(&**m) == ast::Public => return Allowable,
461-
_ => ast_util::method_vis(&**m)
459+
_ if m.pe_vis() == ast::Public => return Allowable,
460+
_ => m.pe_vis()
462461
}
463462
}
464463
Some(ast_map::NodeTraitMethod(_)) => {
@@ -1079,7 +1078,7 @@ impl<'a> SanePrivacyVisitor<'a> {
10791078
"visibility qualifiers have no effect on trait \
10801079
impls");
10811080
for m in methods.iter() {
1082-
check_inherited(m.span, ast_util::method_vis(&**m), "");
1081+
check_inherited(m.span, m.pe_vis(), "");
10831082
}
10841083
}
10851084

@@ -1111,7 +1110,7 @@ impl<'a> SanePrivacyVisitor<'a> {
11111110
for m in methods.iter() {
11121111
match *m {
11131112
ast::Provided(ref m) => {
1114-
check_inherited(m.span, ast_util::method_vis(&**m),
1113+
check_inherited(m.span, m.pe_vis(),
11151114
"unnecessary visibility");
11161115
}
11171116
ast::Required(ref m) => {
@@ -1149,7 +1148,7 @@ impl<'a> SanePrivacyVisitor<'a> {
11491148
match item.node {
11501149
ast::ItemImpl(_, _, _, ref methods) => {
11511150
for m in methods.iter() {
1152-
check_inherited(tcx, m.span, ast_util::method_vis(&**m));
1151+
check_inherited(tcx, m.span, m.pe_vis());
11531152
}
11541153
}
11551154
ast::ItemForeignMod(ref fm) => {
@@ -1175,7 +1174,7 @@ impl<'a> SanePrivacyVisitor<'a> {
11751174
match *m {
11761175
ast::Required(..) => {}
11771176
ast::Provided(ref m) => check_inherited(tcx, m.span,
1178-
ast_util::method_vis(&**m)),
1177+
m.pe_vis()),
11791178
}
11801179
}
11811180
}
@@ -1345,7 +1344,7 @@ impl<'a> Visitor<()> for VisiblePrivateTypesVisitor<'a> {
13451344
// methods will be visible as `Public::foo`.
13461345
let mut found_pub_static = false;
13471346
for method in methods.iter() {
1348-
if ast_util::method_explicit_self(&**method).node == ast::SelfStatic &&
1347+
if method.pe_explicit_self().node == ast::SelfStatic &&
13491348
self.exported_items.contains(&method.id) {
13501349
found_pub_static = true;
13511350
visit::walk_method_helper(self, &**method, ());

Diff for: src/librustc/middle/reachable.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::collections::HashSet;
2626
use syntax::abi;
2727
use syntax::ast;
2828
use syntax::ast_map;
29-
use syntax::ast_util::is_local;
29+
use syntax::ast_util::{is_local, PostExpansionMethod};
3030
use syntax::ast_util;
3131
use syntax::attr::{InlineAlways, InlineHint, InlineNever, InlineNone};
3232
use syntax::attr;
@@ -68,7 +68,7 @@ fn item_might_be_inlined(item: &ast::Item) -> bool {
6868
fn method_might_be_inlined(tcx: &ty::ctxt, method: &ast::Method,
6969
impl_src: ast::DefId) -> bool {
7070
if attributes_specify_inlining(method.attrs.as_slice()) ||
71-
generics_require_inlining(ast_util::method_generics(&*method)) {
71+
generics_require_inlining(method.pe_generics()) {
7272
return true
7373
}
7474
if is_local(impl_src) {
@@ -200,7 +200,7 @@ impl<'a> ReachableContext<'a> {
200200
}
201201
}
202202
Some(ast_map::NodeMethod(method)) => {
203-
if generics_require_inlining(ast_util::method_generics(&*method)) ||
203+
if generics_require_inlining(method.pe_generics()) ||
204204
attributes_specify_inlining(method.attrs.as_slice()) {
205205
true
206206
} else {
@@ -316,14 +316,14 @@ impl<'a> ReachableContext<'a> {
316316
// Keep going, nothing to get exported
317317
}
318318
ast::Provided(ref method) => {
319-
visit::walk_block(self, ast_util::method_body(&**method), ())
319+
visit::walk_block(self, method.pe_body(), ())
320320
}
321321
}
322322
}
323323
ast_map::NodeMethod(method) => {
324324
let did = self.tcx.map.get_parent_did(search_item);
325325
if method_might_be_inlined(self.tcx, &*method, did) {
326-
visit::walk_block(self, ast_util::method_body(&*method), ())
326+
visit::walk_block(self, method.pe_body(), ())
327327
}
328328
}
329329
// Nothing to recurse on for these

Diff for: src/librustc/middle/resolve.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ use util::nodemap::{NodeMap, DefIdSet, FnvHashMap};
2222

2323
use syntax::ast::*;
2424
use syntax::ast;
25-
use syntax::ast_util;
26-
use syntax::ast_util::{local_def};
25+
use syntax::ast_util::{local_def, PostExpansionMethod};
2726
use syntax::ast_util::{walk_pat, trait_method_to_ty_method};
2827
use syntax::ext::mtwt;
2928
use syntax::parse::token::special_names;
@@ -1299,20 +1298,20 @@ impl<'a> Resolver<'a> {
12991298
// For each method...
13001299
for method in methods.iter() {
13011300
// Add the method to the module.
1302-
let ident = ast_util::method_ident(&**method);
1301+
let ident = method.pe_ident();
13031302
let method_name_bindings =
13041303
self.add_child(ident,
13051304
new_parent.clone(),
13061305
ForbidDuplicateValues,
13071306
method.span);
1308-
let def = match ast_util::method_explicit_self(&**method).node {
1307+
let def = match method.pe_explicit_self().node {
13091308
SelfStatic => {
13101309
// Static methods become
13111310
// `def_static_method`s.
13121311
DefStaticMethod(local_def(method.id),
13131312
FromImpl(local_def(
13141313
item.id)),
1315-
ast_util::method_fn_style(&**method))
1314+
method.pe_fn_style())
13161315
}
13171316
_ => {
13181317
// Non-static methods become
@@ -1321,7 +1320,7 @@ impl<'a> Resolver<'a> {
13211320
}
13221321
};
13231322

1324-
let is_public = ast_util::method_vis(&**method) == ast::Public;
1323+
let is_public = method.pe_vis() == ast::Public;
13251324
method_name_bindings.define_value(def,
13261325
method.span,
13271326
is_public);
@@ -4004,15 +4003,14 @@ impl<'a> Resolver<'a> {
40044003
fn resolve_method(&mut self,
40054004
rib_kind: RibKind,
40064005
method: &Method) {
4007-
let method_generics = ast_util::method_generics(method);
4006+
let method_generics = method.pe_generics();
40084007
let type_parameters = HasTypeParameters(method_generics,
40094008
FnSpace,
40104009
method.id,
40114010
rib_kind);
40124011

4013-
self.resolve_function(rib_kind, Some(ast_util::method_fn_decl(method)),
4014-
type_parameters,
4015-
ast_util::method_body(method));
4012+
self.resolve_function(rib_kind, Some(method.pe_fn_decl()), type_parameters,
4013+
method.pe_body());
40164014
}
40174015

40184016
fn with_current_self_type<T>(&mut self, self_type: &Ty, f: |&mut Resolver| -> T) -> T {
@@ -4083,7 +4081,7 @@ impl<'a> Resolver<'a> {
40834081
fn check_trait_method(&self, method: &Method) {
40844082
// If there is a TraitRef in scope for an impl, then the method must be in the trait.
40854083
for &(did, ref trait_ref) in self.current_trait_ref.iter() {
4086-
let method_name = ast_util::method_ident(method).name;
4084+
let method_name = method.pe_ident().name;
40874085

40884086
if self.method_map.borrow().find(&(method_name, did)).is_none() {
40894087
let path_str = self.path_idents_to_string(&trait_ref.path);

Diff for: src/librustc/middle/save/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use std::os;
4343

4444
use syntax::ast;
4545
use syntax::ast_util;
46+
use syntax::ast_util::PostExpansionMethod;
4647
use syntax::ast::{NodeId,DefId};
4748
use syntax::ast_map::NodeItem;
4849
use syntax::attr;
@@ -333,7 +334,7 @@ impl <'l> DxrVisitor<'l> {
333334
},
334335
};
335336

336-
qualname.push_str(get_ident(ast_util::method_ident(&*method)).get());
337+
qualname.push_str(get_ident(method.pe_ident()).get());
337338
let qualname = qualname.as_slice();
338339

339340
// record the decl for this def (if it has one)
@@ -349,18 +350,17 @@ impl <'l> DxrVisitor<'l> {
349350
decl_id,
350351
scope_id);
351352

352-
let m_decl = ast_util::method_fn_decl(&*method);
353-
self.process_formals(&m_decl.inputs, qualname, e);
353+
self.process_formals(&method.pe_fn_decl().inputs, qualname, e);
354354

355355
// walk arg and return types
356-
for arg in m_decl.inputs.iter() {
356+
for arg in method.pe_fn_decl().inputs.iter() {
357357
self.visit_ty(&*arg.ty, e);
358358
}
359-
self.visit_ty(m_decl.output, e);
359+
self.visit_ty(method.pe_fn_decl().output, e);
360360
// walk the fn body
361-
self.visit_block(ast_util::method_body(&*method), DxrVisitorEnv::new_nested(method.id));
361+
self.visit_block(method.pe_body(), DxrVisitorEnv::new_nested(method.id));
362362

363-
self.process_generic_params(ast_util::method_generics(&*method),
363+
self.process_generic_params(method.pe_generics(),
364364
method.span,
365365
qualname,
366366
method.id,

Diff for: src/librustc/middle/trans/debuginfo.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ use std::rc::{Rc, Weak};
207207
use syntax::util::interner::Interner;
208208
use syntax::codemap::{Span, Pos};
209209
use syntax::{abi, ast, codemap, ast_util, ast_map};
210+
use syntax::ast_util::PostExpansionMethod;
210211
use syntax::owned_slice::OwnedSlice;
211212
use syntax::parse::token;
212213
use syntax::parse::token::special_idents;
@@ -1138,10 +1139,10 @@ pub fn create_function_debug_context(cx: &CrateContext,
11381139
}
11391140
}
11401141
ast_map::NodeMethod(ref method) => {
1141-
(ast_util::method_ident(&**method),
1142-
ast_util::method_fn_decl(&**method),
1143-
ast_util::method_generics(&**method),
1144-
ast_util::method_body(&**method),
1142+
(method.pe_ident(),
1143+
method.pe_fn_decl(),
1144+
method.pe_generics(),
1145+
method.pe_body(),
11451146
method.span,
11461147
true)
11471148
}
@@ -1167,10 +1168,10 @@ pub fn create_function_debug_context(cx: &CrateContext,
11671168
ast_map::NodeTraitMethod(ref trait_method) => {
11681169
match **trait_method {
11691170
ast::Provided(ref method) => {
1170-
(ast_util::method_ident(&**method),
1171-
ast_util::method_fn_decl(&**method),
1172-
ast_util::method_generics(&**method),
1173-
ast_util::method_body(&**method),
1171+
(method.pe_ident(),
1172+
method.pe_fn_decl(),
1173+
method.pe_generics(),
1174+
method.pe_body(),
11741175
method.span,
11751176
true)
11761177
}

Diff for: src/librustc/middle/trans/inline.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use middle::trans::common::*;
1616
use middle::ty;
1717

1818
use syntax::ast;
19-
use syntax::ast_util::local_def;
19+
use syntax::ast_util::{local_def, PostExpansionMethod};
2020
use syntax::ast_util;
2121

2222
pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
@@ -128,12 +128,11 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
128128
let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
129129
let unparameterized =
130130
impl_tpt.generics.types.is_empty() &&
131-
ast_util::method_generics(&*mth).ty_params.is_empty();
131+
mth.pe_generics().ty_params.is_empty();
132132

133133
if unparameterized {
134134
let llfn = get_item_val(ccx, mth.id);
135-
trans_fn(ccx, ast_util::method_fn_decl(&*mth),
136-
ast_util::method_body(&*mth), llfn,
135+
trans_fn(ccx, &*mth.pe_fn_decl(), &*mth.pe_body(), llfn,
137136
&param_substs::empty(), mth.id, []);
138137
}
139138
local_def(mth.id)

0 commit comments

Comments
 (0)