diff --git a/src/rustc/middle/typeck/check.rs b/src/rustc/middle/typeck/check.rs index 70167192afc..b3f7c5a9388 100644 --- a/src/rustc/middle/typeck/check.rs +++ b/src/rustc/middle/typeck/check.rs @@ -387,6 +387,12 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) { }; // typecheck the members for members.each {|m| check_class_member(class_ccx, class_t, m); } + // Check that there's at least one field + let (fields,_) = split_class_items(members); + if fields.len() < 1u { + ccx.tcx.sess.span_err(it.span, "A class must have at least one \ + field"); + } // Check that the class is instantiable check_instantiable(ccx.tcx, it.span, it.id); } diff --git a/src/test/compile-fail/issue-2509-a.rs b/src/test/compile-fail/issue-2509-a.rs new file mode 100644 index 00000000000..a500d249c07 --- /dev/null +++ b/src/test/compile-fail/issue-2509-a.rs @@ -0,0 +1,9 @@ +class c { //! ERROR A class must have at least one field + new() { } +} + +fn main() { + let a = c(); + let x = [a]; + let _y = x[0]; +}