Skip to content

Commit f98b176

Browse files
committed
syntax: gather common fields of impl & trait items into their respective types.
1 parent 9849182 commit f98b176

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1179
-1843
lines changed

src/librustc/lint/context.rs

+16-25
Original file line numberDiff line numberDiff line change
@@ -519,28 +519,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
519519

520520
fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl,
521521
body: &'v ast::Block, span: Span, id: ast::NodeId) {
522-
match fk {
523-
visit::FkMethod(_, _, m) => {
524-
self.with_lint_attrs(&m.attrs, |cx| {
525-
run_lints!(cx, check_fn, fk, decl, body, span, id);
526-
cx.visit_ids(|v| {
527-
v.visit_fn(fk, decl, body, span, id);
528-
});
529-
visit::walk_fn(cx, fk, decl, body, span);
530-
})
531-
},
532-
_ => {
533-
run_lints!(self, check_fn, fk, decl, body, span, id);
534-
visit::walk_fn(self, fk, decl, body, span);
535-
}
536-
}
537-
}
538-
539-
fn visit_ty_method(&mut self, t: &ast::TypeMethod) {
540-
self.with_lint_attrs(&t.attrs, |cx| {
541-
run_lints!(cx, check_ty_method, t);
542-
visit::walk_ty_method(cx, t);
543-
})
522+
run_lints!(self, check_fn, fk, decl, body, span, id);
523+
visit::walk_fn(self, fk, decl, body, span);
544524
}
545525

546526
fn visit_struct_def(&mut self,
@@ -611,9 +591,20 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
611591
visit::walk_generics(self, g);
612592
}
613593

614-
fn visit_trait_item(&mut self, m: &ast::TraitItem) {
615-
run_lints!(self, check_trait_item, m);
616-
visit::walk_trait_item(self, m);
594+
fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
595+
self.with_lint_attrs(&trait_item.attrs, |cx| {
596+
run_lints!(cx, check_trait_item, trait_item);
597+
cx.visit_ids(|v| v.visit_trait_item(trait_item));
598+
visit::walk_trait_item(cx, trait_item);
599+
});
600+
}
601+
602+
fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
603+
self.with_lint_attrs(&impl_item.attrs, |cx| {
604+
run_lints!(cx, check_impl_item, impl_item);
605+
cx.visit_ids(|v| v.visit_impl_item(impl_item));
606+
visit::walk_impl_item(cx, impl_item);
607+
});
617608
}
618609

619610
fn visit_opt_lifetime_ref(&mut self, sp: Span, lt: &Option<ast::Lifetime>) {

src/librustc/lint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ pub trait LintPass {
143143
fn check_generics(&mut self, _: &Context, _: &ast::Generics) { }
144144
fn check_fn(&mut self, _: &Context,
145145
_: FnKind, _: &ast::FnDecl, _: &ast::Block, _: Span, _: ast::NodeId) { }
146-
fn check_ty_method(&mut self, _: &Context, _: &ast::TypeMethod) { }
147146
fn check_trait_item(&mut self, _: &Context, _: &ast::TraitItem) { }
147+
fn check_impl_item(&mut self, _: &Context, _: &ast::ImplItem) { }
148148
fn check_struct_def(&mut self, _: &Context,
149149
_: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
150150
fn check_struct_def_post(&mut self, _: &Context,

src/librustc/metadata/encoder.rs

+17-42
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
808808
impl_path: PathElems,
809809
is_default_impl: bool,
810810
parent_id: NodeId,
811-
ast_item_opt: Option<&ast::ImplItem>) {
811+
impl_item_opt: Option<&ast::ImplItem>) {
812812

813813
debug!("encode_info_for_method: {:?} {:?}", m.def_id,
814814
token::get_name(m.name));
@@ -826,21 +826,20 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
826826

827827
let elem = ast_map::PathName(m.name);
828828
encode_path(rbml_w, impl_path.chain(Some(elem).into_iter()));
829-
match ast_item_opt {
830-
Some(&ast::MethodImplItem(ref ast_method)) => {
831-
encode_attributes(rbml_w, &ast_method.attrs);
829+
if let Some(impl_item) = impl_item_opt {
830+
if let ast::MethodImplItem(ref ast_method) = impl_item.node {
831+
encode_attributes(rbml_w, &impl_item.attrs);
832832
let scheme = ty::lookup_item_type(ecx.tcx, m.def_id);
833833
let any_types = !scheme.generics.types.is_empty();
834-
if any_types || is_default_impl || attr::requests_inline(&ast_method.attrs) {
834+
if any_types || is_default_impl || attr::requests_inline(&impl_item.attrs) {
835835
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
836-
ast_item_opt.unwrap()));
836+
impl_item));
837837
}
838838
if !any_types {
839839
encode_symbol(ecx, rbml_w, m.def_id.node);
840840
}
841841
encode_method_argument_names(rbml_w, ast_method.pe_fn_decl());
842842
}
843-
Some(_) | None => {}
844843
}
845844

846845
rbml_w.end_tag();
@@ -851,7 +850,7 @@ fn encode_info_for_associated_type(ecx: &EncodeContext,
851850
associated_type: &ty::AssociatedType,
852851
impl_path: PathElems,
853852
parent_id: NodeId,
854-
typedef_opt: Option<&ast::Typedef>) {
853+
impl_item_opt: Option<&ast::ImplItem>) {
855854
debug!("encode_info_for_associated_type({:?},{:?})",
856855
associated_type.def_id,
857856
token::get_name(associated_type.name));
@@ -873,9 +872,9 @@ fn encode_info_for_associated_type(ecx: &EncodeContext,
873872
let elem = ast_map::PathName(associated_type.name);
874873
encode_path(rbml_w, impl_path.chain(Some(elem).into_iter()));
875874

876-
if let Some(typedef) = typedef_opt {
877-
encode_attributes(rbml_w, &typedef.attrs);
878-
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, typedef.id));
875+
if let Some(ii) = impl_item_opt {
876+
encode_attributes(rbml_w, &ii.attrs);
877+
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, ii.id));
879878
}
880879

881880
rbml_w.end_tag();
@@ -1232,11 +1231,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
12321231
pos: rbml_w.mark_stable_position(),
12331232
});
12341233

1235-
let trait_item_type =
1236-
ty::impl_or_trait_item(tcx, trait_item_def_id.def_id());
1237-
match (trait_item_type, ast_item) {
1238-
(ty::MethodTraitItem(ref method_type),
1239-
Some(&ast::MethodImplItem(_))) => {
1234+
match ty::impl_or_trait_item(tcx, trait_item_def_id.def_id()) {
1235+
ty::MethodTraitItem(ref method_type) => {
12401236
encode_info_for_method(ecx,
12411237
rbml_w,
12421238
&**method_type,
@@ -1245,31 +1241,13 @@ fn encode_info_for_item(ecx: &EncodeContext,
12451241
item.id,
12461242
ast_item)
12471243
}
1248-
(ty::MethodTraitItem(ref method_type), _) => {
1249-
encode_info_for_method(ecx,
1250-
rbml_w,
1251-
&**method_type,
1252-
path.clone(),
1253-
false,
1254-
item.id,
1255-
None)
1256-
}
1257-
(ty::TypeTraitItem(ref associated_type),
1258-
Some(&ast::TypeImplItem(ref typedef))) => {
1259-
encode_info_for_associated_type(ecx,
1260-
rbml_w,
1261-
&**associated_type,
1262-
path.clone(),
1263-
item.id,
1264-
Some(typedef))
1265-
}
1266-
(ty::TypeTraitItem(ref associated_type), _) => {
1244+
ty::TypeTraitItem(ref associated_type) => {
12671245
encode_info_for_associated_type(ecx,
12681246
rbml_w,
12691247
&**associated_type,
12701248
path.clone(),
12711249
item.id,
1272-
None)
1250+
ast_item)
12731251
}
12741252
}
12751253
}
@@ -1393,25 +1371,22 @@ fn encode_info_for_item(ecx: &EncodeContext,
13931371
encode_bounds_and_type_for_item(rbml_w, ecx, item_def_id.def_id().local_id());
13941372
}
13951373
};
1396-
match *trait_item {
1374+
encode_attributes(rbml_w, &trait_item.attrs);
1375+
match trait_item.node {
13971376
ast::RequiredMethod(ref m) => {
1398-
encode_attributes(rbml_w, &m.attrs);
13991377
encode_trait_item(rbml_w);
14001378
encode_item_sort(rbml_w, 'r');
14011379
encode_method_argument_names(rbml_w, &*m.decl);
14021380
}
14031381

14041382
ast::ProvidedMethod(ref m) => {
1405-
encode_attributes(rbml_w, &m.attrs);
14061383
encode_trait_item(rbml_w);
14071384
encode_item_sort(rbml_w, 'p');
14081385
encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item));
14091386
encode_method_argument_names(rbml_w, &*m.pe_fn_decl());
14101387
}
14111388

1412-
ast::TypeTraitItem(ref associated_type) => {
1413-
encode_attributes(rbml_w,
1414-
&associated_type.attrs);
1389+
ast::TypeTraitItem(..) => {
14151390
encode_item_sort(rbml_w, 't');
14161391
}
14171392
}

src/librustc/middle/astencode.rs

+13-50
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use middle::ty::{self, Ty, MethodCall, MethodCallee, MethodOrigin};
3232
use util::ppaux::ty_to_string;
3333

3434
use syntax::{ast, ast_map, ast_util, codemap, fold};
35-
use syntax::ast_util::PostExpansionMethod;
3635
use syntax::codemap::Span;
3736
use syntax::fold::Folder;
3837
use syntax::parse::token;
@@ -81,11 +80,8 @@ pub fn encode_inlined_item(ecx: &e::EncodeContext,
8180
let id = match ii {
8281
e::IIItemRef(i) => i.id,
8382
e::IIForeignRef(i) => i.id,
84-
e::IITraitItemRef(_, &ast::ProvidedMethod(ref m)) => m.id,
85-
e::IITraitItemRef(_, &ast::RequiredMethod(ref m)) => m.id,
86-
e::IITraitItemRef(_, &ast::TypeTraitItem(ref ti)) => ti.ty_param.id,
87-
e::IIImplItemRef(_, &ast::MethodImplItem(ref m)) => m.id,
88-
e::IIImplItemRef(_, &ast::TypeImplItem(ref ti)) => ti.id,
83+
e::IITraitItemRef(_, ti) => ti.id,
84+
e::IIImplItemRef(_, ii) => ii.id,
8985
};
9086
debug!("> Encoding inlined item: {} ({:?})",
9187
ecx.tcx.map.path_to_string(id),
@@ -157,19 +153,8 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
157153
let ident = match *ii {
158154
ast::IIItem(ref i) => i.ident,
159155
ast::IIForeign(ref i) => i.ident,
160-
ast::IITraitItem(_, ref ti) => {
161-
match *ti {
162-
ast::ProvidedMethod(ref m) => m.pe_ident(),
163-
ast::RequiredMethod(ref ty_m) => ty_m.ident,
164-
ast::TypeTraitItem(ref ti) => ti.ty_param.ident,
165-
}
166-
},
167-
ast::IIImplItem(_, ref m) => {
168-
match *m {
169-
ast::MethodImplItem(ref m) => m.pe_ident(),
170-
ast::TypeImplItem(ref ti) => ti.ident,
171-
}
172-
}
156+
ast::IITraitItem(_, ref ti) => ti.ident,
157+
ast::IIImplItem(_, ref ii) => ii.ident
173158
};
174159
debug!("Fn named: {}", token::get_ident(ident));
175160
debug!("< Decoded inlined fn: {}::{}",
@@ -412,38 +397,16 @@ fn simplify_ast(ii: e::InlinedItemRef) -> ast::InlinedItem {
412397
.expect_one("expected one item"))
413398
}
414399
e::IITraitItemRef(d, ti) => {
415-
ast::IITraitItem(d, match *ti {
416-
ast::ProvidedMethod(ref m) => {
417-
ast::ProvidedMethod(
418-
fold::noop_fold_method(m.clone(), &mut fld)
419-
.expect_one("noop_fold_method must produce \
420-
exactly one method"))
421-
}
422-
ast::RequiredMethod(ref ty_m) => {
423-
ast::RequiredMethod(
424-
fold::noop_fold_type_method(ty_m.clone(), &mut fld))
425-
}
426-
ast::TypeTraitItem(ref associated_type) => {
427-
ast::TypeTraitItem(
428-
fold::noop_fold_associated_type(
429-
(*associated_type).clone(),
430-
&mut fld))
431-
}
432-
})
400+
ast::IITraitItem(d,
401+
fold::noop_fold_trait_item(P(ti.clone()), &mut fld)
402+
.expect_one("noop_fold_trait_item must produce \
403+
exactly one trait item"))
433404
}
434-
e::IIImplItemRef(d, m) => {
435-
ast::IIImplItem(d, match *m {
436-
ast::MethodImplItem(ref m) => {
437-
ast::MethodImplItem(
438-
fold::noop_fold_method(m.clone(), &mut fld)
439-
.expect_one("noop_fold_method must produce \
440-
exactly one method"))
441-
}
442-
ast::TypeImplItem(ref td) => {
443-
ast::TypeImplItem(
444-
fold::noop_fold_typedef((*td).clone(), &mut fld))
445-
}
446-
})
405+
e::IIImplItemRef(d, ii) => {
406+
ast::IIImplItem(d,
407+
fold::noop_fold_impl_item(P(ii.clone()), &mut fld)
408+
.expect_one("noop_fold_impl_item must produce \
409+
exactly one impl item"))
447410
}
448411
e::IIForeignRef(i) => {
449412
ast::IIForeign(fold::noop_fold_foreign_item(P(i.clone()), &mut fld))

src/librustc/middle/dead.rs

+26-31
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,11 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
228228
_ => ()
229229
}
230230
}
231-
ast_map::NodeTraitItem(trait_method) => {
232-
visit::walk_trait_item(self, trait_method);
231+
ast_map::NodeTraitItem(trait_item) => {
232+
visit::walk_trait_item(self, trait_item);
233233
}
234234
ast_map::NodeImplItem(impl_item) => {
235-
match *impl_item {
236-
ast::MethodImplItem(ref method) => {
237-
visit::walk_method_helper(self, method);
238-
}
239-
ast::TypeImplItem(_) => {}
240-
}
235+
visit::walk_impl_item(self, impl_item);
241236
}
242237
ast_map::NodeForeignItem(foreign_item) => {
243238
visit::walk_foreign_item(self, &*foreign_item);
@@ -355,11 +350,26 @@ impl<'v> Visitor<'v> for LifeSeeder {
355350
ast::ItemEnum(ref enum_def, _) if allow_dead_code => {
356351
self.worklist.extend(enum_def.variants.iter().map(|variant| variant.node.id));
357352
}
358-
ast::ItemImpl(_, _, _, Some(ref _trait_ref), _, ref impl_items) => {
353+
ast::ItemTrait(_, _, _, ref trait_items) => {
354+
for trait_item in trait_items {
355+
match trait_item.node {
356+
ast::ProvidedMethod(_) => {
357+
if has_allow_dead_code_or_lang_attr(&trait_item.attrs) {
358+
self.worklist.push(trait_item.id);
359+
}
360+
}
361+
_ => {}
362+
}
363+
}
364+
}
365+
ast::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
359366
for impl_item in impl_items {
360-
match **impl_item {
361-
ast::MethodImplItem(ref method) => {
362-
self.worklist.push(method.id);
367+
match impl_item.node {
368+
ast::MethodImplItem(_) => {
369+
if opt_trait.is_some() ||
370+
has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
371+
self.worklist.push(impl_item.id);
372+
}
363373
}
364374
ast::TypeImplItem(_) => {}
365375
}
@@ -369,21 +379,6 @@ impl<'v> Visitor<'v> for LifeSeeder {
369379
}
370380
visit::walk_item(self, item);
371381
}
372-
373-
fn visit_fn(&mut self, fk: visit::FnKind<'v>,
374-
_: &'v ast::FnDecl, block: &'v ast::Block,
375-
_: codemap::Span, id: ast::NodeId) {
376-
// Check for method here because methods are not ast::Item
377-
match fk {
378-
visit::FkMethod(_, _, method) => {
379-
if has_allow_dead_code_or_lang_attr(&method.attrs) {
380-
self.worklist.push(id);
381-
}
382-
}
383-
_ => ()
384-
}
385-
visit::walk_block(self, block);
386-
}
387382
}
388383

389384
fn create_and_seed_worklist(tcx: &ty::ctxt,
@@ -561,7 +556,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
561556
span: codemap::Span, id: ast::NodeId) {
562557
// Have to warn method here because methods are not ast::Item
563558
match fk {
564-
visit::FkMethod(name, _, _) => {
559+
visit::FkMethod(name, _) => {
565560
if !self.symbol_is_live(id, None) {
566561
self.warn_dead_code(id, span, name, "method");
567562
}
@@ -582,12 +577,12 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> {
582577

583578
// Overwrite so that we don't warn the trait method itself.
584579
fn visit_trait_item(&mut self, trait_method: &ast::TraitItem) {
585-
match *trait_method {
580+
match trait_method.node {
586581
ast::ProvidedMethod(ref method) => {
587-
visit::walk_block(self, &*method.pe_body())
582+
visit::walk_block(self, method.pe_body())
588583
}
589584
ast::RequiredMethod(_) |
590-
ast::TypeTraitItem(_) => {}
585+
ast::TypeTraitItem(..) => {}
591586
}
592587
}
593588
}

src/librustc/middle/effect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
9090
let (is_item_fn, is_unsafe_fn) = match fn_kind {
9191
visit::FkItemFn(_, _, fn_style, _) =>
9292
(true, fn_style == ast::Unsafety::Unsafe),
93-
visit::FkMethod(_, _, method) =>
93+
visit::FkMethod(_, method) =>
9494
(true, method.pe_unsafety() == ast::Unsafety::Unsafe),
9595
_ => (false, false),
9696
};

0 commit comments

Comments
 (0)