Bug 8857 – [CTFE] does not evaluate to a boolean, only with -inline
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-20T07:27:00Z
Last change time
2015-06-09T05:11:59Z
Keywords
CTFE
Assigned to
nobody
Creator
youxkei
Comments
Comment #0 by youxkei — 2012-10-20T07:27:56Z
Code:
struct Result{ bool match; string next; }
void func1()(){
auto r = func2();
if(r.match){ // line 5
auto next = r.next;
}
}
Result func2(){
Result result;
result.match = true;
return result;
}
static assert({
func1();
return true;
}());
void main(){}
Compilation Output:
bug.d(5): Error: Result(true,null).next does not evaluate to a boolean.
The above code doesn't compiled by dmd 2.060 with -inline.
At the line 5, I surely wrote "r.match", but dmd seems to have recognized "r.match" as "r.next".
This means that dmd is referring to a wrong member of the struct "Result".
Comment #1 by clugdbug — 2012-11-06T11:46:53Z
Original title:
[CTFE] Referring to a wrong member of a struct in CTFE with -inline
Actually it isn't using a wrong struct member. What happens is that the inliner changes
if(r.match){ // line 5
auto next = r.next;
}
into:
r.match && (string next = r.next);
The CTFE engine cannot currently cope with this expression, and gives an error. It's the wrong line number, though this hardly matters since it's really an internal compiler error.
It's quite an obscure bug, in that it is only triggered with -inline (the code after inlining is not valid D code) + a true condition + an assignment where the value is null.
Comment #2 by github-bugzilla — 2012-11-10T12:14:56Z