Skip to content

Method macros #15646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ fn encode_info_for_method(ecx: &EncodeContext,
} else {
encode_symbol(ecx, ebml_w, m.def_id.node);
}
encode_method_argument_names(ebml_w, &*ast_method.decl);
encode_method_argument_names(ebml_w, method_fn_decl(&*ast_method));
}

ebml_w.end_tag();
Expand Down Expand Up @@ -1241,7 +1241,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
encode_method_sort(ebml_w, 'p');
encode_inlined_item(ecx, ebml_w,
IIMethodRef(def_id, true, &*m));
encode_method_argument_names(ebml_w, &*m.decl);
encode_method_argument_names(ebml_w, method_fn_decl(m));
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub fn decode_inlined_item(cdata: &cstore::crate_metadata,
let ident = match ii {
ast::IIItem(i) => i.ident,
ast::IIForeign(i) => i.ident,
ast::IIMethod(_, _, m) => m.ident,
ast::IIMethod(_, _, m) => ast_util::method_ident(&*m),
};
debug!("Fn named: {}", token::get_ident(ident));
debug!("< Decoded inlined fn: {}::{}",
Expand Down Expand Up @@ -345,7 +345,9 @@ fn simplify_ast(ii: e::InlinedItemRef) -> ast::InlinedItem {
// HACK we're not dropping items.
e::IIItemRef(i) => ast::IIItem(fold::noop_fold_item(i, &mut fld)
.expect_one("expected one item")),
e::IIMethodRef(d, p, m) => ast::IIMethod(d, p, fold::noop_fold_method(m, &mut fld)),
e::IIMethodRef(d, p, m) => ast::IIMethod(d, p, fold::noop_fold_method(m, &mut fld)
.expect_one(
"noop_fold_method must produce exactly one method")),
e::IIForeignRef(i) => ast::IIForeign(fold::noop_fold_foreign_item(i, &mut fld))
}
}
Expand Down Expand Up @@ -387,7 +389,8 @@ fn renumber_and_map_ast(xcx: &ExtendedDecodeContext,
ast::IIItem(fld.fold_item(i).expect_one("expected one item"))
}
ast::IIMethod(d, is_provided, m) => {
ast::IIMethod(xcx.tr_def_id(d), is_provided, fld.fold_method(m))
ast::IIMethod(xcx.tr_def_id(d), is_provided, fld.fold_method(m)
.expect_one("expected one method"))
}
ast::IIForeign(i) => ast::IIForeign(fld.fold_foreign_item(i))
}
Expand Down
6 changes: 4 additions & 2 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use util::nodemap::NodeSet;
use std::collections::HashSet;
use syntax::ast;
use syntax::ast_map;
use syntax::ast_util;
use syntax::ast_util::{local_def, is_local};
use syntax::attr::AttrMetaMethods;
use syntax::attr;
Expand Down Expand Up @@ -212,7 +213,7 @@ impl<'a> MarkSymbolVisitor<'a> {
visit::walk_trait_method(self, &*trait_method, ctxt);
}
ast_map::NodeMethod(method) => {
visit::walk_block(self, &*method.body, ctxt);
visit::walk_block(self, ast_util::method_body(&*method), ctxt);
}
ast_map::NodeForeignItem(foreign_item) => {
visit::walk_foreign_item(self, &*foreign_item, ctxt);
Expand Down Expand Up @@ -520,7 +521,8 @@ impl<'a> Visitor<()> for DeadVisitor<'a> {
// Overwrite so that we don't warn the trait method itself.
fn visit_trait_method(&mut self, trait_method: &ast::TraitMethod, _: ()) {
match *trait_method {
ast::Provided(ref method) => visit::walk_block(self, &*method.body, ()),
ast::Provided(ref method) => visit::walk_block(self,
ast_util::method_body(&**method), ()),
ast::Required(_) => ()
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use middle::typeck::MethodCall;
use util::ppaux;

use syntax::ast;
use syntax::ast_util;
use syntax::codemap::Span;
use syntax::visit;
use syntax::visit::Visitor;
Expand Down Expand Up @@ -94,7 +95,7 @@ impl<'a> Visitor<()> for EffectCheckVisitor<'a> {
visit::FkItemFn(_, _, fn_style, _) =>
(true, fn_style == ast::UnsafeFn),
visit::FkMethod(_, _, method) =>
(true, method.fn_style == ast::UnsafeFn),
(true, ast_util::method_fn_style(method) == ast::UnsafeFn),
_ => (false, false),
};

Expand Down
19 changes: 10 additions & 9 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use util::nodemap::{NodeMap, NodeSet};

use syntax::ast;
use syntax::ast_map;
use syntax::ast_util;
use syntax::ast_util::{is_local, local_def};
use syntax::attr;
use syntax::codemap::Span;
Expand Down Expand Up @@ -263,10 +264,10 @@ impl<'a> Visitor<()> for EmbargoVisitor<'a> {

if public_ty || public_trait {
for method in methods.iter() {
let meth_public = match method.explicit_self.node {
let meth_public = match ast_util::method_explicit_self(&**method).node {
ast::SelfStatic => public_ty,
_ => true,
} && method.vis == ast::Public;
} && ast_util::method_vis(&**method) == ast::Public;
if meth_public || tr.is_some() {
self.exported_items.insert(method.id);
}
Expand Down Expand Up @@ -456,8 +457,8 @@ impl<'a> PrivacyVisitor<'a> {
let imp = self.tcx.map.get_parent_did(closest_private_id);
match ty::impl_trait_ref(self.tcx, imp) {
Some(..) => return Allowable,
_ if m.vis == ast::Public => return Allowable,
_ => m.vis
_ if ast_util::method_vis(&**m) == ast::Public => return Allowable,
_ => ast_util::method_vis(&**m)
}
}
Some(ast_map::NodeTraitMethod(_)) => {
Expand Down Expand Up @@ -1078,7 +1079,7 @@ impl<'a> SanePrivacyVisitor<'a> {
"visibility qualifiers have no effect on trait \
impls");
for m in methods.iter() {
check_inherited(m.span, m.vis, "");
check_inherited(m.span, ast_util::method_vis(&**m), "");
}
}

Expand Down Expand Up @@ -1110,7 +1111,7 @@ impl<'a> SanePrivacyVisitor<'a> {
for m in methods.iter() {
match *m {
ast::Provided(ref m) => {
check_inherited(m.span, m.vis,
check_inherited(m.span, ast_util::method_vis(&**m),
"unnecessary visibility");
}
ast::Required(ref m) => {
Expand Down Expand Up @@ -1148,7 +1149,7 @@ impl<'a> SanePrivacyVisitor<'a> {
match item.node {
ast::ItemImpl(_, _, _, ref methods) => {
for m in methods.iter() {
check_inherited(tcx, m.span, m.vis);
check_inherited(tcx, m.span, ast_util::method_vis(&**m));
}
}
ast::ItemForeignMod(ref fm) => {
Expand All @@ -1174,7 +1175,7 @@ impl<'a> SanePrivacyVisitor<'a> {
match *m {
ast::Required(..) => {}
ast::Provided(ref m) => check_inherited(tcx, m.span,
m.vis),
ast_util::method_vis(&**m)),
}
}
}
Expand Down Expand Up @@ -1344,7 +1345,7 @@ impl<'a> Visitor<()> for VisiblePrivateTypesVisitor<'a> {
// methods will be visible as `Public::foo`.
let mut found_pub_static = false;
for method in methods.iter() {
if method.explicit_self.node == ast::SelfStatic &&
if ast_util::method_explicit_self(&**method).node == ast::SelfStatic &&
self.exported_items.contains(&method.id) {
found_pub_static = true;
visit::walk_method_helper(self, &**method, ());
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn item_might_be_inlined(item: &ast::Item) -> bool {
fn method_might_be_inlined(tcx: &ty::ctxt, method: &ast::Method,
impl_src: ast::DefId) -> bool {
if attributes_specify_inlining(method.attrs.as_slice()) ||
generics_require_inlining(&method.generics) {
generics_require_inlining(ast_util::method_generics(&*method)) {
return true
}
if is_local(impl_src) {
Expand Down Expand Up @@ -200,7 +200,7 @@ impl<'a> ReachableContext<'a> {
}
}
Some(ast_map::NodeMethod(method)) => {
if generics_require_inlining(&method.generics) ||
if generics_require_inlining(ast_util::method_generics(&*method)) ||
attributes_specify_inlining(method.attrs.as_slice()) {
true
} else {
Expand Down Expand Up @@ -316,14 +316,14 @@ impl<'a> ReachableContext<'a> {
// Keep going, nothing to get exported
}
ast::Provided(ref method) => {
visit::walk_block(self, &*method.body, ())
visit::walk_block(self, ast_util::method_body(&**method), ())
}
}
}
ast_map::NodeMethod(method) => {
let did = self.tcx.map.get_parent_did(search_item);
if method_might_be_inlined(self.tcx, &*method, did) {
visit::walk_block(self, &*method.body, ())
visit::walk_block(self, ast_util::method_body(&*method), ())
}
}
// Nothing to recurse on for these
Expand Down
17 changes: 10 additions & 7 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use util::nodemap::{NodeMap, DefIdSet, FnvHashMap};

use syntax::ast::*;
use syntax::ast;
use syntax::ast_util;
use syntax::ast_util::{local_def};
use syntax::ast_util::{walk_pat, trait_method_to_ty_method};
use syntax::ext::mtwt;
Expand Down Expand Up @@ -1298,20 +1299,20 @@ impl<'a> Resolver<'a> {
// For each method...
for method in methods.iter() {
// Add the method to the module.
let ident = method.ident;
let ident = ast_util::method_ident(&**method);
let method_name_bindings =
self.add_child(ident,
new_parent.clone(),
ForbidDuplicateValues,
method.span);
let def = match method.explicit_self.node {
let def = match ast_util::method_explicit_self(&**method).node {
SelfStatic => {
// Static methods become
// `def_static_method`s.
DefStaticMethod(local_def(method.id),
FromImpl(local_def(
item.id)),
method.fn_style)
ast_util::method_fn_style(&**method))
}
_ => {
// Non-static methods become
Expand All @@ -1320,7 +1321,7 @@ impl<'a> Resolver<'a> {
}
};

let is_public = method.vis == ast::Public;
let is_public = ast_util::method_vis(&**method) == ast::Public;
method_name_bindings.define_value(def,
method.span,
is_public);
Expand Down Expand Up @@ -4003,13 +4004,15 @@ impl<'a> Resolver<'a> {
fn resolve_method(&mut self,
rib_kind: RibKind,
method: &Method) {
let method_generics = &method.generics;
let method_generics = ast_util::method_generics(method);
let type_parameters = HasTypeParameters(method_generics,
FnSpace,
method.id,
rib_kind);

self.resolve_function(rib_kind, Some(method.decl), type_parameters, method.body);
self.resolve_function(rib_kind, Some(ast_util::method_fn_decl(method)),
type_parameters,
ast_util::method_body(method));
}

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

if self.method_map.borrow().find(&(method_name, did)).is_none() {
let path_str = self.path_idents_to_string(&trait_ref.path);
Expand Down
13 changes: 7 additions & 6 deletions src/librustc/middle/save/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl <'l> DxrVisitor<'l> {
},
};

qualname.push_str(get_ident(method.ident).get());
qualname.push_str(get_ident(ast_util::method_ident(&*method)).get());
let qualname = qualname.as_slice();

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

self.process_formals(&method.decl.inputs, qualname, e);
let m_decl = ast_util::method_fn_decl(&*method);
self.process_formals(&m_decl.inputs, qualname, e);

// walk arg and return types
for arg in method.decl.inputs.iter() {
for arg in m_decl.inputs.iter() {
self.visit_ty(&*arg.ty, e);
}
self.visit_ty(&*method.decl.output, e);
self.visit_ty(m_decl.output, e);
// walk the fn body
self.visit_block(&*method.body, DxrVisitorEnv::new_nested(method.id));
self.visit_block(ast_util::method_body(&*method), DxrVisitorEnv::new_nested(method.id));

self.process_generic_params(&method.generics,
self.process_generic_params(ast_util::method_generics(&*method),
method.span,
qualname,
method.id,
Expand Down
16 changes: 8 additions & 8 deletions src/librustc/middle/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,10 @@ pub fn create_function_debug_context(cx: &CrateContext,
}
}
ast_map::NodeMethod(ref method) => {
(method.ident,
method.decl,
&method.generics,
method.body,
(ast_util::method_ident(&**method),
ast_util::method_fn_decl(&**method),
ast_util::method_generics(&**method),
ast_util::method_body(&**method),
method.span,
true)
}
Expand All @@ -1167,10 +1167,10 @@ pub fn create_function_debug_context(cx: &CrateContext,
ast_map::NodeTraitMethod(ref trait_method) => {
match **trait_method {
ast::Provided(ref method) => {
(method.ident,
method.decl,
&method.generics,
method.body,
(ast_util::method_ident(&**method),
ast_util::method_fn_decl(&**method),
ast_util::method_generics(&**method),
ast_util::method_body(&**method),
method.span,
true)
}
Expand Down
5 changes: 3 additions & 2 deletions src/librustc/middle/trans/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ pub fn maybe_instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
let impl_tpt = ty::lookup_item_type(ccx.tcx(), impl_did);
let unparameterized =
impl_tpt.generics.types.is_empty() &&
mth.generics.ty_params.is_empty();
ast_util::method_generics(&*mth).ty_params.is_empty();

if unparameterized {
let llfn = get_item_val(ccx, mth.id);
trans_fn(ccx, &*mth.decl, &*mth.body, llfn,
trans_fn(ccx, ast_util::method_fn_decl(&*mth),
ast_util::method_body(&*mth), llfn,
&param_substs::empty(), mth.id, []);
}
local_def(mth.id)
Expand Down
9 changes: 5 additions & 4 deletions src/librustc/middle/trans/meth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::c_str::ToCStr;
use std::gc::Gc;
use syntax::abi::Rust;
use syntax::parse::token;
use syntax::{ast, ast_map, visit};
use syntax::{ast, ast_map, visit, ast_util};

/**
The main "translation" pass for methods. Generates code
Expand Down Expand Up @@ -66,9 +66,10 @@ pub fn trans_impl(ccx: &CrateContext,
return;
}
for method in methods.iter() {
if method.generics.ty_params.len() == 0u {
if ast_util::method_generics(&**method).ty_params.len() == 0u {
let llfn = get_item_val(ccx, method.id);
trans_fn(ccx, &*method.decl, &*method.body,
trans_fn(ccx, ast_util::method_fn_decl(&**method),
ast_util::method_body(&**method),
llfn, &param_substs::empty(), method.id, []);
} else {
let mut v = TransItemVisitor{ ccx: ccx };
Expand Down Expand Up @@ -160,7 +161,7 @@ pub fn trans_static_method_callee(bcx: &Block,
ast_map::NodeTraitMethod(method) => {
let ident = match *method {
ast::Required(ref m) => m.ident,
ast::Provided(ref m) => m.ident
ast::Provided(ref m) => ast_util::method_ident(&**m)
};
ident.name
}
Expand Down
Loading