Skip to content

librustc: Implement deriving with a unit return type #3955

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 2 commits 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
77 changes: 62 additions & 15 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,29 +1299,76 @@ impl Resolver {
let (name_bindings, new_parent) =
self.add_child(ident, parent, ForbidDuplicateTypes, sp);

// If the trait has static methods, then add all the static
// methods within to a new module.
//
// We only need to create the module if the trait has static
// methods, so check that first.
let mut has_static_methods = false;
for methods.each |method| {
let ty_m = trait_method_to_ty_method(*method);
match ty_m.self_ty.node {
sty_static => {
has_static_methods = true;
break;
}
_ => {}
}
}

// Create the module if necessary.
let module_parent_opt;
if has_static_methods {
let parent_link = self.get_parent_link(parent, ident);
name_bindings.define_module(privacy,
parent_link,
Some(local_def(item.id)),
false,
sp);
module_parent_opt = Some(ModuleReducedGraphParent(
name_bindings.get_module()));
} else {
module_parent_opt = None;
}

// Add the names of all the methods to the trait info.
let method_names = @HashMap();
for methods.each |method| {
let ty_m = trait_method_to_ty_method(*method);

let ident = ty_m.ident;
// Add it to the trait info if not static,
// add it as a name in the enclosing module otherwise.
// add it as a name in the trait module otherwise.
match ty_m.self_ty.node {
sty_static => {
// which parent to use??
let (method_name_bindings, _) =
self.add_child(ident, new_parent,
ForbidDuplicateValues, ty_m.span);
let def = def_static_method(local_def(ty_m.id),
Some(local_def(item.id)),
ty_m.purity);
(*method_name_bindings).define_value
(Public, def, ty_m.span);
}
_ => {
(*method_names).insert(ident, ());
}
sty_static => {
let def = def_static_method(
local_def(ty_m.id),
Some(local_def(item.id)),
ty_m.purity);

// For now, add to both the trait module and the
// enclosing module, for backwards compatibility.
let (method_name_bindings, _) =
self.add_child(ident,
new_parent,
ForbidDuplicateValues,
ty_m.span);
method_name_bindings.define_value(Public,
def,
ty_m.span);

let (method_name_bindings, _) =
self.add_child(ident,
module_parent_opt.get(),
ForbidDuplicateValues,
ty_m.span);
method_name_bindings.define_value(Public,
def,
ty_m.span);
}
_ => {
method_names.insert(ident, ());
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,8 @@ fn register_deriving_method(ccx: @crate_ctxt,
}

let path = vec::append(*path, ~[
ast_map::path_mod(
ccx.sess.parse_sess.interner.intern(@fmt!("__derived%d__", id))),
ast_map::path_name(derived_method_info.method_info.ident)
]);
let mty = ty::lookup_item_type(ccx.tcx, local_def(id)).ty;
Expand Down
Loading