Skip to content

Commit

Permalink
syntax: gather common fields of impl & trait items into their respect…
Browse files Browse the repository at this point in the history
…ive types.
  • Loading branch information
eddyb committed Mar 11, 2015
1 parent 9849182 commit f98b176
Show file tree
Hide file tree
Showing 56 changed files with 1,179 additions and 1,843 deletions.
41 changes: 16 additions & 25 deletions src/librustc/lint/context.rs
Expand Up @@ -519,28 +519,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {

fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl,
body: &'v ast::Block, span: Span, id: ast::NodeId) {
match fk {
visit::FkMethod(_, _, m) => {
self.with_lint_attrs(&m.attrs, |cx| {
run_lints!(cx, check_fn, fk, decl, body, span, id);
cx.visit_ids(|v| {
v.visit_fn(fk, decl, body, span, id);
});
visit::walk_fn(cx, fk, decl, body, span);
})
},
_ => {
run_lints!(self, check_fn, fk, decl, body, span, id);
visit::walk_fn(self, fk, decl, body, span);
}
}
}

fn visit_ty_method(&mut self, t: &ast::TypeMethod) {
self.with_lint_attrs(&t.attrs, |cx| {
run_lints!(cx, check_ty_method, t);
visit::walk_ty_method(cx, t);
})
run_lints!(self, check_fn, fk, decl, body, span, id);
visit::walk_fn(self, fk, decl, body, span);
}

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

fn visit_trait_item(&mut self, m: &ast::TraitItem) {
run_lints!(self, check_trait_item, m);
visit::walk_trait_item(self, m);
fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
self.with_lint_attrs(&trait_item.attrs, |cx| {
run_lints!(cx, check_trait_item, trait_item);
cx.visit_ids(|v| v.visit_trait_item(trait_item));
visit::walk_trait_item(cx, trait_item);
});
}

fn visit_impl_item(&mut self, impl_item: &ast::ImplItem) {
self.with_lint_attrs(&impl_item.attrs, |cx| {
run_lints!(cx, check_impl_item, impl_item);
cx.visit_ids(|v| v.visit_impl_item(impl_item));
visit::walk_impl_item(cx, impl_item);
});
}

fn visit_opt_lifetime_ref(&mut self, sp: Span, lt: &Option<ast::Lifetime>) {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/mod.rs
Expand Up @@ -143,8 +143,8 @@ pub trait LintPass {
fn check_generics(&mut self, _: &Context, _: &ast::Generics) { }
fn check_fn(&mut self, _: &Context,
_: FnKind, _: &ast::FnDecl, _: &ast::Block, _: Span, _: ast::NodeId) { }
fn check_ty_method(&mut self, _: &Context, _: &ast::TypeMethod) { }
fn check_trait_item(&mut self, _: &Context, _: &ast::TraitItem) { }
fn check_impl_item(&mut self, _: &Context, _: &ast::ImplItem) { }
fn check_struct_def(&mut self, _: &Context,
_: &ast::StructDef, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
fn check_struct_def_post(&mut self, _: &Context,
Expand Down
59 changes: 17 additions & 42 deletions src/librustc/metadata/encoder.rs
Expand Up @@ -808,7 +808,7 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>,
impl_path: PathElems,
is_default_impl: bool,
parent_id: NodeId,
ast_item_opt: Option<&ast::ImplItem>) {
impl_item_opt: Option<&ast::ImplItem>) {

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

let elem = ast_map::PathName(m.name);
encode_path(rbml_w, impl_path.chain(Some(elem).into_iter()));
match ast_item_opt {
Some(&ast::MethodImplItem(ref ast_method)) => {
encode_attributes(rbml_w, &ast_method.attrs);
if let Some(impl_item) = impl_item_opt {
if let ast::MethodImplItem(ref ast_method) = impl_item.node {
encode_attributes(rbml_w, &impl_item.attrs);
let scheme = ty::lookup_item_type(ecx.tcx, m.def_id);
let any_types = !scheme.generics.types.is_empty();
if any_types || is_default_impl || attr::requests_inline(&ast_method.attrs) {
if any_types || is_default_impl || attr::requests_inline(&impl_item.attrs) {
encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id),
ast_item_opt.unwrap()));
impl_item));
}
if !any_types {
encode_symbol(ecx, rbml_w, m.def_id.node);
}
encode_method_argument_names(rbml_w, ast_method.pe_fn_decl());
}
Some(_) | None => {}
}

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

if let Some(typedef) = typedef_opt {
encode_attributes(rbml_w, &typedef.attrs);
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, typedef.id));
if let Some(ii) = impl_item_opt {
encode_attributes(rbml_w, &ii.attrs);
encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, ii.id));
}

rbml_w.end_tag();
Expand Down Expand Up @@ -1232,11 +1231,8 @@ fn encode_info_for_item(ecx: &EncodeContext,
pos: rbml_w.mark_stable_position(),
});

let trait_item_type =
ty::impl_or_trait_item(tcx, trait_item_def_id.def_id());
match (trait_item_type, ast_item) {
(ty::MethodTraitItem(ref method_type),
Some(&ast::MethodImplItem(_))) => {
match ty::impl_or_trait_item(tcx, trait_item_def_id.def_id()) {
ty::MethodTraitItem(ref method_type) => {
encode_info_for_method(ecx,
rbml_w,
&**method_type,
Expand All @@ -1245,31 +1241,13 @@ fn encode_info_for_item(ecx: &EncodeContext,
item.id,
ast_item)
}
(ty::MethodTraitItem(ref method_type), _) => {
encode_info_for_method(ecx,
rbml_w,
&**method_type,
path.clone(),
false,
item.id,
None)
}
(ty::TypeTraitItem(ref associated_type),
Some(&ast::TypeImplItem(ref typedef))) => {
encode_info_for_associated_type(ecx,
rbml_w,
&**associated_type,
path.clone(),
item.id,
Some(typedef))
}
(ty::TypeTraitItem(ref associated_type), _) => {
ty::TypeTraitItem(ref associated_type) => {
encode_info_for_associated_type(ecx,
rbml_w,
&**associated_type,
path.clone(),
item.id,
None)
ast_item)
}
}
}
Expand Down Expand Up @@ -1393,25 +1371,22 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_bounds_and_type_for_item(rbml_w, ecx, item_def_id.def_id().local_id());
}
};
match *trait_item {
encode_attributes(rbml_w, &trait_item.attrs);
match trait_item.node {
ast::RequiredMethod(ref m) => {
encode_attributes(rbml_w, &m.attrs);
encode_trait_item(rbml_w);
encode_item_sort(rbml_w, 'r');
encode_method_argument_names(rbml_w, &*m.decl);
}

ast::ProvidedMethod(ref m) => {
encode_attributes(rbml_w, &m.attrs);
encode_trait_item(rbml_w);
encode_item_sort(rbml_w, 'p');
encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item));
encode_method_argument_names(rbml_w, &*m.pe_fn_decl());
}

ast::TypeTraitItem(ref associated_type) => {
encode_attributes(rbml_w,
&associated_type.attrs);
ast::TypeTraitItem(..) => {
encode_item_sort(rbml_w, 't');
}
}
Expand Down
63 changes: 13 additions & 50 deletions src/librustc/middle/astencode.rs
Expand Up @@ -32,7 +32,6 @@ use middle::ty::{self, Ty, MethodCall, MethodCallee, MethodOrigin};
use util::ppaux::ty_to_string;

use syntax::{ast, ast_map, ast_util, codemap, fold};
use syntax::ast_util::PostExpansionMethod;
use syntax::codemap::Span;
use syntax::fold::Folder;
use syntax::parse::token;
Expand Down Expand Up @@ -81,11 +80,8 @@ pub fn encode_inlined_item(ecx: &e::EncodeContext,
let id = match ii {
e::IIItemRef(i) => i.id,
e::IIForeignRef(i) => i.id,
e::IITraitItemRef(_, &ast::ProvidedMethod(ref m)) => m.id,
e::IITraitItemRef(_, &ast::RequiredMethod(ref m)) => m.id,
e::IITraitItemRef(_, &ast::TypeTraitItem(ref ti)) => ti.ty_param.id,
e::IIImplItemRef(_, &ast::MethodImplItem(ref m)) => m.id,
e::IIImplItemRef(_, &ast::TypeImplItem(ref ti)) => ti.id,
e::IITraitItemRef(_, ti) => ti.id,
e::IIImplItemRef(_, ii) => ii.id,
};
debug!("> Encoding inlined item: {} ({:?})",
ecx.tcx.map.path_to_string(id),
Expand Down Expand Up @@ -157,19 +153,8 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
let ident = match *ii {
ast::IIItem(ref i) => i.ident,
ast::IIForeign(ref i) => i.ident,
ast::IITraitItem(_, ref ti) => {
match *ti {
ast::ProvidedMethod(ref m) => m.pe_ident(),
ast::RequiredMethod(ref ty_m) => ty_m.ident,
ast::TypeTraitItem(ref ti) => ti.ty_param.ident,
}
},
ast::IIImplItem(_, ref m) => {
match *m {
ast::MethodImplItem(ref m) => m.pe_ident(),
ast::TypeImplItem(ref ti) => ti.ident,
}
}
ast::IITraitItem(_, ref ti) => ti.ident,
ast::IIImplItem(_, ref ii) => ii.ident
};
debug!("Fn named: {}", token::get_ident(ident));
debug!("< Decoded inlined fn: {}::{}",
Expand Down Expand Up @@ -412,38 +397,16 @@ fn simplify_ast(ii: e::InlinedItemRef) -> ast::InlinedItem {
.expect_one("expected one item"))
}
e::IITraitItemRef(d, ti) => {
ast::IITraitItem(d, match *ti {
ast::ProvidedMethod(ref m) => {
ast::ProvidedMethod(
fold::noop_fold_method(m.clone(), &mut fld)
.expect_one("noop_fold_method must produce \
exactly one method"))
}
ast::RequiredMethod(ref ty_m) => {
ast::RequiredMethod(
fold::noop_fold_type_method(ty_m.clone(), &mut fld))
}
ast::TypeTraitItem(ref associated_type) => {
ast::TypeTraitItem(
fold::noop_fold_associated_type(
(*associated_type).clone(),
&mut fld))
}
})
ast::IITraitItem(d,
fold::noop_fold_trait_item(P(ti.clone()), &mut fld)
.expect_one("noop_fold_trait_item must produce \
exactly one trait item"))
}
e::IIImplItemRef(d, m) => {
ast::IIImplItem(d, match *m {
ast::MethodImplItem(ref m) => {
ast::MethodImplItem(
fold::noop_fold_method(m.clone(), &mut fld)
.expect_one("noop_fold_method must produce \
exactly one method"))
}
ast::TypeImplItem(ref td) => {
ast::TypeImplItem(
fold::noop_fold_typedef((*td).clone(), &mut fld))
}
})
e::IIImplItemRef(d, ii) => {
ast::IIImplItem(d,
fold::noop_fold_impl_item(P(ii.clone()), &mut fld)
.expect_one("noop_fold_impl_item must produce \
exactly one impl item"))
}
e::IIForeignRef(i) => {
ast::IIForeign(fold::noop_fold_foreign_item(P(i.clone()), &mut fld))
Expand Down
57 changes: 26 additions & 31 deletions src/librustc/middle/dead.rs
Expand Up @@ -228,16 +228,11 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
_ => ()
}
}
ast_map::NodeTraitItem(trait_method) => {
visit::walk_trait_item(self, trait_method);
ast_map::NodeTraitItem(trait_item) => {
visit::walk_trait_item(self, trait_item);
}
ast_map::NodeImplItem(impl_item) => {
match *impl_item {
ast::MethodImplItem(ref method) => {
visit::walk_method_helper(self, method);
}
ast::TypeImplItem(_) => {}
}
visit::walk_impl_item(self, impl_item);
}
ast_map::NodeForeignItem(foreign_item) => {
visit::walk_foreign_item(self, &*foreign_item);
Expand Down Expand Up @@ -355,11 +350,26 @@ impl<'v> Visitor<'v> for LifeSeeder {
ast::ItemEnum(ref enum_def, _) if allow_dead_code => {
self.worklist.extend(enum_def.variants.iter().map(|variant| variant.node.id));
}
ast::ItemImpl(_, _, _, Some(ref _trait_ref), _, ref impl_items) => {
ast::ItemTrait(_, _, _, ref trait_items) => {
for trait_item in trait_items {
match trait_item.node {
ast::ProvidedMethod(_) => {
if has_allow_dead_code_or_lang_attr(&trait_item.attrs) {
self.worklist.push(trait_item.id);
}
}
_ => {}
}
}
}
ast::ItemImpl(_, _, _, ref opt_trait, _, ref impl_items) => {
for impl_item in impl_items {
match **impl_item {
ast::MethodImplItem(ref method) => {
self.worklist.push(method.id);
match impl_item.node {
ast::MethodImplItem(_) => {
if opt_trait.is_some() ||
has_allow_dead_code_or_lang_attr(&impl_item.attrs) {
self.worklist.push(impl_item.id);
}
}
ast::TypeImplItem(_) => {}
}
Expand All @@ -369,21 +379,6 @@ impl<'v> Visitor<'v> for LifeSeeder {
}
visit::walk_item(self, item);
}

fn visit_fn(&mut self, fk: visit::FnKind<'v>,
_: &'v ast::FnDecl, block: &'v ast::Block,
_: codemap::Span, id: ast::NodeId) {
// Check for method here because methods are not ast::Item
match fk {
visit::FkMethod(_, _, method) => {
if has_allow_dead_code_or_lang_attr(&method.attrs) {
self.worklist.push(id);
}
}
_ => ()
}
visit::walk_block(self, block);
}
}

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

// Overwrite so that we don't warn the trait method itself.
fn visit_trait_item(&mut self, trait_method: &ast::TraitItem) {
match *trait_method {
match trait_method.node {
ast::ProvidedMethod(ref method) => {
visit::walk_block(self, &*method.pe_body())
visit::walk_block(self, method.pe_body())
}
ast::RequiredMethod(_) |
ast::TypeTraitItem(_) => {}
ast::TypeTraitItem(..) => {}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/effect.rs
Expand Up @@ -90,7 +90,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
let (is_item_fn, is_unsafe_fn) = match fn_kind {
visit::FkItemFn(_, _, fn_style, _) =>
(true, fn_style == ast::Unsafety::Unsafe),
visit::FkMethod(_, _, method) =>
visit::FkMethod(_, method) =>
(true, method.pe_unsafety() == ast::Unsafety::Unsafe),
_ => (false, false),
};
Expand Down

0 comments on commit f98b176

Please sign in to comment.