Comment #0 by daniel.keep+d.puremagic.com — 2007-01-17T02:32:30Z
Description:
If you attempt to compile code where there is a static constructor which creates an anonymous delegate which instantiates the class itself, all of which is in a unit test, with the "-unittest" switch, DMD crashes with an ICE.
Steps to reproduce:
1) Attempt to compile code below with the command "dmd -unittest ice.d".
Actual result:
The following message: "Internal error: toir.c 170"
Expected result:
Either a normal compilation error, or compilation success.
Compiler: Digital Mars D Compiler v1.0, WinXP SP2.
Code to reproduce: (ice.d)
unittest
{
class Foo
{
static this()
{
auto i = {return new Foo;};
}
this() {}
}
}
void main() {}
Comment #1 by thomas-dloop — 2007-02-23T16:36:20Z
The code isn't legal:
1 # void test(){
2 # class Foo{
3 # static this(){
4 # auto i = {
5 # return new Foo;
6 # };
7 # }
8 # }
9 # }
Line 5 requires access to test's context pointer, thus it should fail because
line 3 can't provide one.
Change "class Foo" to "static class Foo" to sidestep the problem.
Added to DStress as
http://dstress.kuehne.cn/nocompile/b/bug_toir_170_A.dhttp://dstress.kuehne.cn/nocompile/b/bug_toir_170_B.d
Comment #2 by smjg — 2007-09-19T06:56:33Z
Indeed, it compiles if the class is made static. But why? Where has it got a context pointer from?
Comment #3 by clugdbug — 2009-05-27T04:32:51Z
Changed name from ICE(toir.c) in dmd with anon. delegate in static class ctor in unittest, since it has nothing to do with delegates, static class constructors, or unittest!
This reduced test case shows that it's caused by use of new of local class, inside a non-static function nested inside static local function.
---
void bar() {
class Foo { }
static void foo() {
void pug() {
Foo z = new Foo;
}
}
}
---
If you move the 'static' from foo to pug, you get the error:
void bar() {
class Foo { }
void foo() {
static void pug() {
Foo z = new Foo;
}
}
}
ice.d(8): Error: function ice.bar is a nested function and cannot be accessed fr
om pug
But this seems to be a rejects-valid to me -- I can't see any reason why a static nested function couldn't access a local class.
Comment #4 by tomas — 2009-05-27T04:40:48Z
It's because the local class is not marked static, and thus needs a reference to bar's stack frame, which is unreachable from pug.
Comment #5 by tomas — 2009-05-27T04:46:15Z
Both test cases work correctly in LDC by giving an error:
$ ldc foo.d
foo.d(5): Error: outer function context of foo.bar is needed to 'new' nested class foo.bar.Foo
$ ldc foo2.d
foo.d(5): Error: outer function context of foo2.bar is needed to 'new' nested class foo2.bar.Foo
This is most likely because we moved the error checking out of codegen and into the frontend (where it belongs).
Created attachment 383
Patch against DMD2.030.
This is simply copied from LDC. It works without modification on DMD2 and DMD1.
(Gives a nicer error message, too).