Solution to "statement is not reachable" depending on template variables?

ZombineDev via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 1 03:43:55 PDT 2016


On Friday, 1 April 2016 at 01:21:32 UTC, Walter Bright wrote:
> On 3/16/2016 4:18 AM, Johan Engelen wrote:
>>    I've found discussions, but not an actual "recommended" 
>> solution for the
>> problem of "statement is not reachable" warnings in templates 
>> with early
>> returns, e.g.:
>> ```
>> bool nobool(T...)() {
>>      foreach (i, U; T) {
>>          static if (is(U == bool)) {
>>              return false;
>>          }
>>      }
>>      return true;  // emits "Warning: statement is not 
>> reachable"
>> }
>
>
> bool nobool(T...)() {
>      bool result = true;
>      foreach (i, U; T) {
>          static if (is(U == bool)) {
>              result = false;
>              break;
>          }
>          else
>          {
>              ...
>          }
>      }
>      return result;  // emits "Warning: statement is not 
> reachable"
> }
>
> Note that the optimizer will remove the dead loads.

Actually, I would expect every call of this fuction to be 
replaced by a boolean constant, since the result depends only on 
compile-time information.

What happened to "obvious code should be correct"? Can we try to 
find a solution that doesn't look like a hack? I don't think that 
we should ask users of the language to uglify their code just to 
workaround a deficiency in the compiler. Instead, we should 
improve the lowering of the static foreach construct so the 
information that it is syntactically a loop is preserved after 
loop unrolling.


More information about the Digitalmars-d-learn mailing list