D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7419 - [2.058/CTFE] Constructor of struct is overwritten inside a unittest with -inline
Summary: [2.058/CTFE] Constructor of struct is overwritten inside a unittest with -inline
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 regression
Assignee: Don
URL:
Keywords: CTFE
Depends on:
Blocks:
 
Reported: 2012-02-01 11:48 UTC by kennytm
Modified: 2012-02-04 00:59 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 kennytm 2012-02-01 11:48:03 UTC
Test case:

-----------------
struct X7419 {
    double x;
    this(double x) {
        this.x = x;
    }
}
unittest {
    enum x = {
        auto p = X7419(3);
        return p.x;
    }();
    static assert(x == 3);
}
-----------------

Compile with:

    dmd -unittest -inline -c test7419.d

Gives the unexpected error:

    test7419.d(12): Error: static assert  (nan == 3) is false

The bug was introduced in commit 40160a53a0c72bfbad2e0ad36ec8f1ccbb76ce8d.
Comment 1 Don 2012-02-01 13:29:03 UTC
It doesn't need unittest. The -inline seems to be necessary, I presume it's creating a ref variable.

struct X7419 {
    double x;
    this(double x) {
        this.x = x;
    }
}

void bug7419() {
    enum x = {
        auto p = X7419(3);
        return p.x;
    }();
    static assert(x == 3);
}
Comment 3 Walter Bright 2012-02-01 22:00:23 UTC
Looking at that git commit, if I add back in the following code that was elided:

3450a3451,3459
>
> #if 1
> if (op==TOKconstruct && this->e1->op==TOKvar && this->e2->op != TOKthis
>         && this->e2->op != TOKcomma
>         && ((VarExp*)this->e1)->var->storage_class & STCref)
>         wantRef = true;
> #endif
>
>

then it works.

Note that when -inline is used, the only function that gets inlined is the constructor call.

Note the comment for the elision, saying it is to fix something with foreach, yet foreach is not in this example, I think the problem is in CTFE.
Comment 4 Don 2012-02-02 01:12:07 UTC
(In reply to comment #3)
> Looking at that git commit, if I add back in the following code that was
> elided:
> 
> 3450a3451,3459
> >
> > #if 1
> > if (op==TOKconstruct && this->e1->op==TOKvar && this->e2->op != TOKthis
> >         && this->e2->op != TOKcomma
> >         && ((VarExp*)this->e1)->var->storage_class & STCref)
> >         wantRef = true;
> > #endif
> >
> >
> 
> then it works.
> 
> Note that when -inline is used, the only function that gets inlined is the
> constructor call.
> 
> Note the comment for the elision, saying it is to fix something with foreach,
> yet foreach is not in this example, I think the problem is in CTFE.

That code was introduced as a hack to get ref foreach to work in CTFE, but it isn't correct (the != TOKthis and != TOKcomma is a hack). Later, after fixing some other bugs, ref foreach works without it.

I have made a proper fix, which I will post tonight.
Comment 5 github-bugzilla 2012-02-03 23:20:50 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/127d0110b839e5bdc2997ae0b586b1e93c7c8daa
7419 [2.058/CTFE] Constructor of struct is overwritten with -inline

The logic for ref variables was incorrect. Stripping out _all_ of the funky
foreach code fixes it.
Fixes bug 7419.