Fixing spurious "statement is not reachable" in template code

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 10:35:19 PDT 2015


On 10/27/15 9:23 AM, Timon Gehr wrote:
> On 10/27/2015 01:35 PM, Steven Schveighoffer wrote:
>>>
>>> Easy to fix:
>>>
>>> void reachIf(bool x)()
>>> {
>>>       static if(!x)
>>>           return;
>>>       else
>>>           writeln("reached");
>>> }
>>>
>>> The warning is correct, and incredibly annoying.
>>>
>>
>> Easy to fix, but the warning is incorrect, the statement is reachable if
>> you use reachIf!true.
>>
>> A statement is not a compiled piece of code.
>>
>
> Versions of the same statement in different instantiations are
> independent. Templates are just a restricted form of hygienic macros.
>

I understand how the compiler treats it. But it still doesn't make the 
warning true. In some cases, the statement is reachable, the compiler is 
unhelpfully pointing out cases where it was unreachable.

It would be if the compiler had a function like this:

int foo(bool x)
{
    if(x == 5) return 1;
    return 0;
}

And you compile this function:

void main()
{
    foo(5);
}

with -inline, the compiler complained. It's unhelpful that you are 
telling me that you are not generating code for my statement *in this 
instance*.

If you want to tell me that you would *never* generate code for my 
statement in *any* instance, that is helpful.

-Steve


More information about the Digitalmars-d mailing list