Forbid classes with no fields

Classes with no fields don't really make sense, so forbid them
(just as records with no fields aren't allowed). Closes #2509
This commit is contained in:
Tim Chevalier 2012-06-05 20:46:10 -07:00
parent 167d726183
commit 8fd9986f0f
2 changed files with 15 additions and 0 deletions

View File

@ -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);
}

View File

@ -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];
}