Fixing spurious "statement is not reachable" in template code
Timon Gehr via Digitalmars-d
digitalmars-d at puremagic.com
Tue Oct 27 15:03:33 PDT 2015
On 10/27/2015 10:33 PM, deadalnix wrote:
> On Saturday, 24 October 2015 at 17:25:23 UTC, tsbockman wrote:
>> While improving the DMD front-end's constant folding:
>> https://github.com/D-Programming-Language/dmd/pull/5229
>> I found out about DMD issue 14835:
>> https://issues.dlang.org/show_bug.cgi?id=14835
>>
>> Briefly:
>> ///////////////////////////////
>> module main;
>>
>> import std.stdio;
>>
>> void reachIf(bool x)()
>> {
>> if(!x)
>> return;
>> writeln("reached"); // Warning: statement is not reachable
>> }
>>
>> void main(string[] args) {
>> reachIf!true(); // prints "reached"
>> reachIf!false(); // triggers warning
>> }
>> ///////////////////////////////
>>
>> This is, I think, a big problem.
>
> It is. This is the optimizer leaking into semantic. As far as language
> definition is concerned, this is a runtime if and changing it to a
> compile time one is an optimization.
>
> If this was a static if, then generating a error is reasonable, but with
> a regular if, it isn't.
>
if statements with constant conditions perhaps shouldn't be eliminated
in the frontend, at least for reachability analysis within templated
functions.
More information about the Digitalmars-d
mailing list