D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4554 - Cyclic constructor calls cause stack overflow
Summary: Cyclic constructor calls cause stack overflow
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2010-08-01 13:45 UTC by Andrej Mitrovic
Modified: 2012-01-31 01:22 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2010-08-01 13:45:41 UTC
On this page:

http://www.digitalmars.com/d/2.0/class.html

It states "It is illegal for constructors to mutually call each other". This will compile and cause a stack overflow:

import std.stdio;

class C {
    this() { this(1); }
    this(int i) { this(); }	// illegal, cyclic constructor calls
}

void main() {
    C c = new C;
}

object.Error: Stack Overflow

Isn't it possible to make this a compiler error? Or maybe this is a form of a halting-problem and can't be fixed? :)
Comment 1 Don 2010-08-13 22:51:00 UTC
(In reply to comment #0)
> On this page:
> 
> http://www.digitalmars.com/d/2.0/class.html
> 
> It states "It is illegal for constructors to mutually call each other". This
> will compile and cause a stack overflow:
> 
> import std.stdio;
> 
> class C {
>     this() { this(1); }
>     this(int i) { this(); }    // illegal, cyclic constructor calls
> }
> 
> void main() {
>     C c = new C;
> }
> 
> object.Error: Stack Overflow
> 
> Isn't it possible to make this a compiler error? Or maybe this is a form of a
> halting-problem and can't be fixed? :)

I guess the compiler could set up a dependency tree of constructor calls, and check for cycles. Quite a bit of work.
Comment 2 Andrej Mitrovic 2012-01-29 11:20:47 UTC
Page now reads:
"It is illegal for constructors to mutually call each other, although the compiler is not required to detect it."
Comment 3 yebblies 2012-01-30 19:21:19 UTC
I think this is worth asking for.
Comment 4 Walter Bright 2012-01-31 01:22:00 UTC
It's technically not a solvable problem in the general case. I think it's the halting problem.