Skip to content

Implement intra-crate static methods on anonymous trait implementations (e.g. Foo::new()) #3776

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

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 1 addition & 29 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,6 @@ use std::serialization::{Serializable,
use codemap::{span, filename};
use parse::token;

#[cfg(stage0)]
impl span: Serializable {
/* Note #1972 -- spans are serialized but not deserialized */
fn serialize<S: Serializer>(&self, _s: &S) { }
}

#[cfg(stage0)]
impl span: Deserializable {
static fn deserialize<D: Deserializer>(_d: &D) -> span {
ast_util::dummy_sp()
}
}

#[cfg(stage1)]
#[cfg(stage2)]
impl<S: Serializer> span: Serializable<S> {
/* Note #1972 -- spans are serialized but not deserialized */
fn serialize(&self, _s: &S) { }
}

#[cfg(stage1)]
#[cfg(stage2)]
impl<D: Deserializer> span: Deserializable<D> {
static fn deserialize(_d: &D) -> span {
ast_util::dummy_sp()
}
}

#[auto_serialize]
#[auto_deserialize]
type spanned<T> = {node: T, span: span};
Expand Down Expand Up @@ -168,7 +140,7 @@ type ty_param = {ident: ident, id: node_id, bounds: @~[ty_param_bound]};
enum def {
def_fn(def_id, purity),
def_static_method(/* method */ def_id,
/* trait */ def_id,
/* trait */ Option<def_id>,
purity),
def_self(node_id),
def_mod(def_id),
Expand Down
32 changes: 32 additions & 0 deletions src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use dvec::DVec;
use std::serialization::{Serializable,
Deserializable,
Serializer,
Deserializer};

export filename;
export filemap;
Expand Down Expand Up @@ -178,6 +182,34 @@ impl span : cmp::Eq {
pure fn ne(other: &span) -> bool { !self.eq(other) }
}

#[cfg(stage0)]
impl span: Serializable {
/* Note #1972 -- spans are serialized but not deserialized */
fn serialize<S: Serializer>(&self, _s: &S) { }
}

#[cfg(stage0)]
impl span: Deserializable {
static fn deserialize<D: Deserializer>(_d: &D) -> span {
ast_util::dummy_sp()
}
}

#[cfg(stage1)]
#[cfg(stage2)]
impl<S: Serializer> span: Serializable<S> {
/* Note #1972 -- spans are serialized but not deserialized */
fn serialize(&self, _s: &S) { }
}

#[cfg(stage1)]
#[cfg(stage2)]
impl<D: Deserializer> span: Deserializable<D> {
static fn deserialize(_d: &D) -> span {
ast_util::dummy_sp()
}
}

fn span_to_str_no_adj(sp: span, cm: CodeMap) -> ~str {
let lo = lookup_char_pos(cm, sp.lo);
let hi = lookup_char_pos(cm, sp.hi);
Expand Down
2 changes: 2 additions & 0 deletions src/rustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,7 @@ enum astencode_tag { // Reserves 0x50 -- 0x6f

const tag_item_trait_method_sort: uint = 0x70;

const tag_item_impl_type_basename: uint = 0x71;

type link_meta = {name: ~str, vers: ~str, extras_hash: ~str};

21 changes: 21 additions & 0 deletions src/rustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export get_impls_for_mod;
export get_trait_methods;
export get_provided_trait_methods;
export get_method_names_if_trait;
export get_type_name_if_impl;
export get_static_methods_if_impl;
export get_item_attrs;
export each_path;
export get_type;
Expand All @@ -33,12 +35,19 @@ export get_impl_method;
export get_item_path;
export maybe_get_item_ast, found_ast, found, found_parent, not_found;
export ProvidedTraitMethodInfo;
export StaticMethodInfo;

struct ProvidedTraitMethodInfo {
ty: ty::method,
def_id: ast::def_id
}

struct StaticMethodInfo {
ident: ast::ident,
def_id: ast::def_id,
purity: ast::purity
}

fn get_symbol(cstore: cstore::CStore, def: ast::def_id) -> ~str {
let cdata = cstore::get_crate_data(cstore, def.crate).data;
return decoder::get_symbol(cdata, def.node);
Expand Down Expand Up @@ -120,6 +129,18 @@ fn get_method_names_if_trait(cstore: cstore::CStore, def: ast::def_id)
return decoder::get_method_names_if_trait(cstore.intr, cdata, def.node);
}

fn get_type_name_if_impl(cstore: cstore::CStore, def: ast::def_id) ->
Option<ast::ident> {
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_type_name_if_impl(cstore.intr, cdata, def.node)
}

fn get_static_methods_if_impl(cstore: cstore::CStore, def: ast::def_id) ->
Option<~[StaticMethodInfo]> {
let cdata = cstore::get_crate_data(cstore, def.crate);
decoder::get_static_methods_if_impl(cstore.intr, cdata, def.node)
}

fn get_item_attrs(cstore: cstore::CStore,
def_id: ast::def_id,
f: fn(~[@ast::meta_item])) {
Expand Down
83 changes: 76 additions & 7 deletions src/rustc/metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use syntax::diagnostic::span_handler;
use common::*;
use syntax::parse::token::ident_interner;
use hash::{Hash, HashUtil};
use csearch::ProvidedTraitMethodInfo;
use csearch::{ProvidedTraitMethodInfo, StaticMethodInfo};

export class_dtor;
export get_class_fields;
Expand All @@ -31,6 +31,7 @@ export get_type_param_count;
export get_impl_traits;
export get_class_method;
export get_impl_method;
export get_static_methods_if_impl;
export lookup_def;
export resolve_path;
export get_crate_attributes;
Expand All @@ -43,6 +44,7 @@ export get_impls_for_mod;
export get_trait_methods;
export get_provided_trait_methods;
export get_method_names_if_trait;
export get_type_name_if_impl;
export get_item_attrs;
export get_crate_module_paths;
export def_like;
Expand Down Expand Up @@ -185,6 +187,12 @@ fn item_parent_item(d: ebml::Doc) -> Option<ast::def_id> {
None
}

fn translated_parent_item_opt(cnum: ast::crate_num, d: ebml::Doc) ->
Option<ast::def_id> {
let trait_did_opt = item_parent_item(d);
trait_did_opt.map(|trait_did| {crate: cnum, node: trait_did.node})
}

fn item_reqd_and_translated_parent_item(cnum: ast::crate_num,
d: ebml::Doc) -> ast::def_id {
let trait_did = item_parent_item(d).expect(~"item without parent");
Expand Down Expand Up @@ -321,16 +329,16 @@ fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)
PureFn => dl_def(ast::def_fn(did, ast::pure_fn)),
ForeignFn => dl_def(ast::def_fn(did, ast::extern_fn)),
UnsafeStaticMethod => {
let trait_did = item_reqd_and_translated_parent_item(cnum, item);
dl_def(ast::def_static_method(did, trait_did, ast::unsafe_fn))
let trait_did_opt = translated_parent_item_opt(cnum, item);
dl_def(ast::def_static_method(did, trait_did_opt, ast::unsafe_fn))
}
StaticMethod => {
let trait_did = item_reqd_and_translated_parent_item(cnum, item);
dl_def(ast::def_static_method(did, trait_did, ast::impure_fn))
let trait_did_opt = translated_parent_item_opt(cnum, item);
dl_def(ast::def_static_method(did, trait_did_opt, ast::impure_fn))
}
PureStaticMethod => {
let trait_did = item_reqd_and_translated_parent_item(cnum, item);
dl_def(ast::def_static_method(did, trait_did, ast::pure_fn))
let trait_did_opt = translated_parent_item_opt(cnum, item);
dl_def(ast::def_static_method(did, trait_did_opt, ast::pure_fn))
}
Type | ForeignType => dl_def(ast::def_ty(did)),
Mod => dl_def(ast::def_mod(did)),
Expand Down Expand Up @@ -784,6 +792,67 @@ fn get_method_names_if_trait(intr: @ident_interner, cdata: cmd,
return Some(resulting_methods);
}

fn get_type_name_if_impl(intr: @ident_interner,
cdata: cmd,
node_id: ast::node_id) -> Option<ast::ident> {
let item = lookup_item(node_id, cdata.data);
if item_family(item) != Impl {
return None;
}

for ebml::tagged_docs(item, tag_item_impl_type_basename) |doc| {
return Some(intr.intern(@str::from_bytes(ebml::doc_data(doc))));
}

return None;
}

fn get_static_methods_if_impl(intr: @ident_interner,
cdata: cmd,
node_id: ast::node_id) ->
Option<~[StaticMethodInfo]> {
let item = lookup_item(node_id, cdata.data);
if item_family(item) != Impl {
return None;
}

// If this impl has a trait ref, don't consider it.
for ebml::tagged_docs(item, tag_impl_trait) |_doc| {
return None;
}

let impl_method_ids = DVec();
for ebml::tagged_docs(item, tag_item_impl_method) |impl_method_doc| {
impl_method_ids.push(parse_def_id(ebml::doc_data(impl_method_doc)));
}

let static_impl_methods = DVec();
for impl_method_ids.each |impl_method_id| {
let impl_method_doc = lookup_item(impl_method_id.node, cdata.data);
let family = item_family(impl_method_doc);
match family {
StaticMethod | UnsafeStaticMethod | PureStaticMethod => {
let purity;
match item_family(impl_method_doc) {
StaticMethod => purity = ast::impure_fn,
UnsafeStaticMethod => purity = ast::unsafe_fn,
PureStaticMethod => purity = ast::pure_fn,
_ => fail
}

static_impl_methods.push(StaticMethodInfo {
ident: item_name(intr, impl_method_doc),
def_id: item_def_id(impl_method_doc, cdata),
purity: purity
});
}
_ => {}
}
}

return Some(dvec::unwrap(move static_impl_methods));
}

fn get_item_attrs(cdata: cmd,
node_id: ast::node_id,
f: fn(~[@ast::meta_item])) {
Expand Down
22 changes: 20 additions & 2 deletions src/rustc/metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ fn encode_name(ecx: @encode_ctxt, ebml_w: ebml::Serializer, name: ident) {
ebml_w.wr_tagged_str(tag_paths_data_name, ecx.tcx.sess.str_of(name));
}

fn encode_impl_type_basename(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
name: ident) {
ebml_w.wr_tagged_str(tag_item_impl_type_basename,
ecx.tcx.sess.str_of(name));
}

fn encode_def_id(ebml_w: ebml::Serializer, id: def_id) {
ebml_w.wr_tagged_str(tag_def_id, def_to_str(id));
}
Expand Down Expand Up @@ -484,7 +490,12 @@ fn encode_info_for_method(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
ecx.tcx.sess.str_of(m.ident), all_tps.len());
ebml_w.start_tag(tag_items_data_item);
encode_def_id(ebml_w, local_def(m.id));
encode_family(ebml_w, purity_fn_family(m.purity));
match m.self_ty.node {
ast::sty_static => {
encode_family(ebml_w, purity_static_method_family(m.purity));
}
_ => encode_family(ebml_w, purity_fn_family(m.purity))
}
encode_type_param_bounds(ebml_w, ecx, all_tps);
encode_type(ecx, ebml_w, node_id_to_type(ecx.tcx, m.id));
encode_name(ecx, ebml_w, m.ident);
Expand Down Expand Up @@ -701,7 +712,7 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
encode_index(ebml_w, bkts, write_int);
ebml_w.end_tag();
}
item_impl(tps, opt_trait, _, methods) => {
item_impl(tps, opt_trait, ty, methods) => {
add_to_index();
ebml_w.start_tag(tag_items_data_item);
encode_def_id(ebml_w, local_def(item.id));
Expand All @@ -711,6 +722,13 @@ fn encode_info_for_item(ecx: @encode_ctxt, ebml_w: ebml::Serializer,
encode_type(ecx, ebml_w, node_id_to_type(tcx, item.id));
encode_name(ecx, ebml_w, item.ident);
encode_attributes(ebml_w, item.attrs);
match ty.node {
ast::ty_path(path, _) if path.idents.len() == 1 => {
encode_impl_type_basename(ecx, ebml_w,
ast_util::path_to_ident(path));
}
_ => {}
}
for methods.each |m| {
ebml_w.start_tag(tag_item_impl_method);
ebml_w.writer.write(str::to_bytes(def_to_str(local_def(m.id))));
Expand Down
6 changes: 4 additions & 2 deletions src/rustc/middle/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,10 @@ impl ast::def: tr {
fn tr(xcx: extended_decode_ctxt) -> ast::def {
match self {
ast::def_fn(did, p) => { ast::def_fn(did.tr(xcx), p) }
ast::def_static_method(did, did2, p) => {
ast::def_static_method(did.tr(xcx), did2.tr(xcx), p)
ast::def_static_method(did, did2_opt, p) => {
ast::def_static_method(did.tr(xcx),
did2_opt.map(|did2| did2.tr(xcx)),
p)
}
ast::def_self(nid) => { ast::def_self(xcx.tr_id(nid)) }
ast::def_mod(did) => { ast::def_mod(did.tr(xcx)) }
Expand Down
Loading