Bug 8862 – order of declaration of a function and compile time execution

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-21T06:13:00Z
Last change time
2015-07-09T04:24:38Z
Assigned to
nobody
Creator
r.97all

Comments

Comment #0 by r.97all — 2012-10-21T06:13:56Z
I think these two codes below are equivalent, but DMD outputs two lines when compiling the first. http://dpaste.dzfl.pl/3bcc6b95 http://dpaste.dzfl.pl/167ef2ac I am not sure this is caused by CTFE, template mixin, something else, or some combination of them.
Comment #1 by bearophile_hugs — 2012-10-21T06:37:26Z
Reduced test case. The the line of initialization of the 'b' variable matters. If it's defined after bar() it compiles, otherwise it doesn't compile: mixin template Foo() { static Spam[1] b = bar(); // Bad. static Spam[1] bar() { Spam[1] result; return result; } // static Spam[1] b = bar(); // Good. } struct Spam { mixin Foo!(); } void main() {} Initializing 'b' before defining bar() gives the errors: test.d(3): Error: function test.Spam.Foo!().bar circular dependency. Functions cannot be interpreted while being compiled test.d(2): called from here: bar() test.d(2): called from here: bar() test.d(10): Error: mixin test.Spam.Foo!() error instantiating
Comment #2 by r.97all — 2012-10-21T06:47:20Z
(In reply to comment #1) > Reduced test case. The line of initialization of the 'b' variable matters. > If it's defined after bar() it compiles, otherwise it doesn't compile: Thanks, but the behavior you reproduced seems different from mine. In my case, both compiles while the first outputs error-like messages.
Comment #3 by bearophile_hugs — 2012-10-21T06:53:27Z
(In reply to comment #2) > (In reply to comment #1) > > Reduced test case. The line of initialization of the 'b' variable matters. > > If it's defined after bar() it compiles, otherwise it doesn't compile: > > Thanks, but the behavior you reproduced seems different from mine. > In my case, both compiles while the first outputs error-like messages. I see. Then try to minimize the code yourself. And then attach/paste it here instead of dpaste, please.
Comment #4 by r.97all — 2012-10-21T15:11:21Z
> I see. Then try to minimize the code yourself. And then attach/paste it here > instead of dpaste, please. Actually I was trying to reduce test case for a bug http://d.puremagic.com/issues/show_bug.cgi?id=8865 and came up with another (this) one, so it was difficult for me. Thanks to your code, I managed to minimize see below: void main() { F!10 x; } struct F(int n) { mixin I!n; } mixin template I(int n) { int value; static F[1] x = get_xs(); // compiles, but outputs: "error.d(14): called from here: get_xs()" static F[1] get_xs() { return [F(0)]; } //static F[1] x = get_xs(); // compiles, without output. }
Comment #5 by clugdbug — 2012-10-22T07:39:56Z
The difference is simply that bearophile's case is using git head. The "circular dependency" message was missing from previous compiler releases. This situation is actually a bit strange. It's using sequential semantics, as if it were in a function body, rather than the out-of-order semantics it would have in a struct. This isn't actually a CTFE bug - it should be possible to reproduce this bug without using CTFE.
Comment #6 by k.hara.pg — 2015-07-09T04:24:38Z
Works with git-head, so I'm sure that it's a duplication of one of already fixed forward reference issue.