Bug 4554 – Cyclic constructor calls cause stack overflow

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-08-01T13:45:00Z
Last change time
2012-01-31T01:22:00Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2010-08-01T13:45:41Z
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 by clugdbug — 2010-08-13T22:51:00Z
(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 by andrej.mitrovich — 2012-01-29T11:20:47Z
Page now reads: "It is illegal for constructors to mutually call each other, although the compiler is not required to detect it."
Comment #3 by yebblies — 2012-01-30T19:21:19Z
I think this is worth asking for.
Comment #4 by bugzilla — 2012-01-31T01:22:00Z
It's technically not a solvable problem in the general case. I think it's the halting problem.