Skip to content

Commit a4cedd9

Browse files
committed
Disallow multiple constructors or destructors in the same class
Closes #2825
1 parent 253dfc3 commit a4cedd9

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,9 @@ class parser {
225225
fn span_fatal(sp: span, m: ~str) -> ! {
226226
self.sess.span_diagnostic.span_fatal(sp, m)
227227
}
228+
fn span_note(sp: span, m: ~str) {
229+
self.sess.span_diagnostic.span_note(sp, m)
230+
}
228231
fn bug(m: ~str) -> ! {
229232
self.sess.span_diagnostic.span_bug(copy self.span, m)
230233
}
@@ -2529,10 +2532,30 @@ class parser {
25292532
while self.token != token::RBRACE {
25302533
match self.parse_class_item(class_path) {
25312534
ctor_decl(a_fn_decl, attrs, blk, s) => {
2532-
the_ctor = some((a_fn_decl, attrs, blk, s));
2535+
match the_ctor {
2536+
some((_, _, _, s_first)) => {
2537+
self.span_note(s, #fmt("Duplicate constructor \
2538+
declaration for class %s", *class_name));
2539+
self.span_fatal(copy s_first, ~"First constructor \
2540+
declared here");
2541+
}
2542+
none => {
2543+
the_ctor = some((a_fn_decl, attrs, blk, s));
2544+
}
2545+
}
25332546
}
25342547
dtor_decl(blk, attrs, s) => {
2535-
the_dtor = some((blk, attrs, s));
2548+
match the_dtor {
2549+
some((_, _, s_first)) => {
2550+
self.span_note(s, #fmt("Duplicate destructor \
2551+
declaration for class %s", *class_name));
2552+
self.span_fatal(copy s_first, ~"First destructor \
2553+
declared here");
2554+
}
2555+
none => {
2556+
the_dtor = some((blk, attrs, s));
2557+
}
2558+
}
25362559
}
25372560
members(mms) => { ms = vec::append(ms, mms); }
25382561
}
@@ -2557,9 +2580,6 @@ class parser {
25572580
span: ct_s}), actual_dtor),
25582581
none)
25592582
}
2560-
/*
2561-
Is it strange for the parser to check this?
2562-
*/
25632583
none => {
25642584
(class_name,
25652585
item_class(ty_params, traits, ms, none, actual_dtor),

src/test/compile-fail/issue-2825-b.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class example {
2+
let x: int;
3+
new() {
4+
self.x = 1;
5+
}
6+
drop {} //~ ERROR First destructor declared
7+
drop {
8+
debug!("Goodbye, cruel world");
9+
}
10+
}
11+
12+
fn main(_args: ~[~str]) {
13+
let e: example = example();
14+
}

src/test/compile-fail/issue-2825.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class example {
2+
let x: int;
3+
new() { //~ ERROR First constructor declared here
4+
self.x = 1;
5+
}
6+
new(x_: int) {
7+
self.x = x_;
8+
}
9+
}
10+
11+
fn main(_args: ~[~str]) {
12+
let e: example = example();
13+
}

0 commit comments

Comments
 (0)