Skip to content

rustc: Implement typechecking for automatically-derived enums #3885

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 1 commit into from
Oct 29, 2012
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
6 changes: 6 additions & 0 deletions src/rustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ type ctxt =
deriving_struct_methods: HashMap<ast::def_id,
@~[typeck::method_origin]>,

// The outer vector here describes each enum variant, while the inner
// nested vector describes each enum variant argument.
deriving_enum_methods: HashMap<ast::def_id,
@~[@~[typeck::method_origin]]>,

// A mapping from the def ID of a method that was automatically derived
// to information about it.
automatically_derived_methods: HashMap<ast::def_id, DerivedMethodInfo>,
Expand Down Expand Up @@ -959,6 +964,7 @@ fn mk_ctxt(s: session::Session,
provided_method_sources: HashMap(),
supertraits: HashMap(),
deriving_struct_methods: HashMap(),
deriving_enum_methods: HashMap(),
automatically_derived_methods: HashMap(),
automatically_derived_methods_for_impl: HashMap()}
}
Expand Down
51 changes: 49 additions & 2 deletions src/rustc/middle/typeck/deriving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ impl DerivingChecker {
tcx.deriving_struct_methods.insert(local_def(impl_id), field_info);
}

fn check_deriving_for_enum(enum_def_id: def_id,
enum_substs: &substs,
trait_ref: @trait_ref,
impl_id: node_id,
impl_span: span) {
let tcx = self.crate_context.tcx;
let enum_methods = dvec::DVec();
let variants = ty::substd_enum_variants(
tcx, enum_def_id, enum_substs);
for variants.each |enum_variant_info| {
let variant_methods = dvec::DVec();
for enum_variant_info.args.eachi |i, variant_arg_type| {
match self.check_deriving_for_substructure_type(
*variant_arg_type, trait_ref, impl_span) {
Some(method_target_def_id) => {
variant_methods.push(method_static(
method_target_def_id));
}
None => {
let trait_str = pprust::path_to_str(
trait_ref.path, tcx.sess.parse_sess.interner);
tcx.sess.span_err(impl_span,
fmt!("cannot automatically derive \
an implementation for `%s`: \
argument %u of variant `%s` \
does not implement the trait \
`%s`",
trait_str,
i + 1,
tcx.sess.str_of(
enum_variant_info.name),
trait_str));
}
}
}
enum_methods.push(@dvec::unwrap(move variant_methods));
}

let enum_methods = @dvec::unwrap(move enum_methods);
tcx.deriving_enum_methods.insert(local_def(impl_id), enum_methods);
}

fn check_deriving(crate: @crate) {
let tcx = self.crate_context.tcx;
visit_crate(*crate, (), mk_simple_visitor(@{
Expand All @@ -123,8 +165,13 @@ impl DerivingChecker {
let superty = ty::lookup_item_type(
tcx, local_def(item.id)).ty;
match ty::get(superty).sty {
ty_enum(_def_id, _substs) => {
// XXX: Handle enums.
ty_enum(def_id, ref substs) => {
self.check_deriving_for_enum(
def_id,
substs,
trait_ref,
item.id,
item.span);
}
ty_class(def_id, ref substs) => {
self.check_deriving_for_struct(
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/enum-deriving-incomplete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
trait MyEq {
pure fn eq(other: &self) -> bool;
}

struct A {
x: int
}

enum B {
C(A),
D(A),
E(A)
}

impl B : MyEq;
//~^ ERROR cannot automatically derive
//~^^ ERROR cannot automatically derive
//~^^^ ERROR cannot automatically derive

fn main() {
let c = C(A { x: 15 });
assert c.eq(&c);
}