Disallow multiple constructors or destructors in the same class

Closes #2825
This commit is contained in:
Tim Chevalier 2012-08-06 17:44:07 -07:00
parent 253dfc3387
commit a4cedd9598
3 changed files with 52 additions and 5 deletions

View File

@ -225,6 +225,9 @@ class parser {
fn span_fatal(sp: span, m: ~str) -> ! {
self.sess.span_diagnostic.span_fatal(sp, m)
}
fn span_note(sp: span, m: ~str) {
self.sess.span_diagnostic.span_note(sp, m)
}
fn bug(m: ~str) -> ! {
self.sess.span_diagnostic.span_bug(copy self.span, m)
}
@ -2529,10 +2532,30 @@ class parser {
while self.token != token::RBRACE {
match self.parse_class_item(class_path) {
ctor_decl(a_fn_decl, attrs, blk, s) => {
the_ctor = some((a_fn_decl, attrs, blk, s));
match the_ctor {
some((_, _, _, s_first)) => {
self.span_note(s, #fmt("Duplicate constructor \
declaration for class %s", *class_name));
self.span_fatal(copy s_first, ~"First constructor \
declared here");
}
none => {
the_ctor = some((a_fn_decl, attrs, blk, s));
}
}
}
dtor_decl(blk, attrs, s) => {
the_dtor = some((blk, attrs, s));
match the_dtor {
some((_, _, s_first)) => {
self.span_note(s, #fmt("Duplicate destructor \
declaration for class %s", *class_name));
self.span_fatal(copy s_first, ~"First destructor \
declared here");
}
none => {
the_dtor = some((blk, attrs, s));
}
}
}
members(mms) => { ms = vec::append(ms, mms); }
}
@ -2557,9 +2580,6 @@ class parser {
span: ct_s}), actual_dtor),
none)
}
/*
Is it strange for the parser to check this?
*/
none => {
(class_name,
item_class(ty_params, traits, ms, none, actual_dtor),

View File

@ -0,0 +1,14 @@
class example {
let x: int;
new() {
self.x = 1;
}
drop {} //~ ERROR First destructor declared
drop {
debug!("Goodbye, cruel world");
}
}
fn main(_args: ~[~str]) {
let e: example = example();
}

View File

@ -0,0 +1,13 @@
class example {
let x: int;
new() { //~ ERROR First constructor declared here
self.x = 1;
}
new(x_: int) {
self.x = x_;
}
}
fn main(_args: ~[~str]) {
let e: example = example();
}