Bug 7419 – [2.058/CTFE] Constructor of struct is overwritten inside a unittest with -inline
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-02-01T11:48:00Z
Last change time
2012-02-04T00:59:51Z
Keywords
CTFE
Assigned to
clugdbug
Creator
kennytm
Comments
Comment #0 by kennytm — 2012-02-01T11:48:03Z
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 by clugdbug — 2012-02-01T13:29:03Z
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);
}
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 by clugdbug — 2012-02-02T01:12:07Z
(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 by github-bugzilla — 2012-02-03T23:20:50Z