D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8862 - order of declaration of a function and compile time execution
Summary: order of declaration of a function and compile time execution
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-10-21 06:13 UTC by Ryuichi OHORI
Modified: 2015-07-09 04:24 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Ryuichi OHORI 2012-10-21 06:13:56 UTC
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 bearophile_hugs 2012-10-21 06:37:26 UTC
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 Ryuichi OHORI 2012-10-21 06:47:20 UTC
(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 bearophile_hugs 2012-10-21 06:53:27 UTC
(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 Ryuichi OHORI 2012-10-21 15:11:21 UTC
> 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 Don 2012-10-22 07:39:56 UTC
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 Kenji Hara 2015-07-09 04:24:38 UTC
Works with git-head, so I'm sure that it's a duplication of one of already fixed forward reference issue.