Skip to content

Commit 8fd9986

Browse files
committed
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
1 parent 167d726 commit 8fd9986

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/rustc/middle/typeck/check.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ fn check_item(ccx: @crate_ctxt, it: @ast::item) {
387387
};
388388
// typecheck the members
389389
for members.each {|m| check_class_member(class_ccx, class_t, m); }
390+
// Check that there's at least one field
391+
let (fields,_) = split_class_items(members);
392+
if fields.len() < 1u {
393+
ccx.tcx.sess.span_err(it.span, "A class must have at least one \
394+
field");
395+
}
390396
// Check that the class is instantiable
391397
check_instantiable(ccx.tcx, it.span, it.id);
392398
}

src/test/compile-fail/issue-2509-a.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class c { //! ERROR A class must have at least one field
2+
new() { }
3+
}
4+
5+
fn main() {
6+
let a = c();
7+
let x = [a];
8+
let _y = x[0];
9+
}

0 commit comments

Comments
 (0)