Fixing spurious "statement is not reachable" in template code

tsbockman via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 13:32:52 PDT 2015


On Tuesday, 27 October 2015 at 17:23:21 UTC, Jonathan M Davis 
wrote:
> On Tuesday, 27 October 2015 at 16:05:31 UTC, tsbockman wrote:
>> On Tuesday, 27 October 2015 at 13:23:51 UTC, Timon Gehr wrote:
>>> Versions of the same statement in different instantiations 
>>> are independent. Templates are just a restricted form of 
>>> hygienic macros.
>>
>> That's how the current implementation works, but that doesn't 
>> mean the warning is actually *helpful*.
>
> Well, arguably that's exactly what templates _are_ regardless 
> of the implementation - i.e. a template is just a template to 
> generate code, not really actual code in and of itself.

That is one (valid) way of thinking about templates.

Another perspective, though, which I picked up from someone 
(Andrei Alexandrescu, I think?) in the D community, is to 
consider template parameters simply as additional function 
arguments, which happen to be evaluated at compile time. In many 
cases, the timing of their evaluation is just an implementation 
detail - a performance optimization (or de-optimization, as the 
case may be).

Thinking about templates in this way leads naturally to Steven 
Schveighoffer's comparison with function inlining, above.

> Now, that being said, I'm not sure that warning about 
> unreachable code in a templated function is particularly 
> helpful - particularly if it forces you to jump through hoops 
> to make the compiler shut up about it. Heck, if anything, I 
> find that warning/error annoying in regular code, because it 
> tends to get in the way of debugging while developing. It 
> wouldn't really hurt my feelings any if we just removed it from 
> the compiler entirely - though I completely agree that you 
> don't really want to have unreachable code left in your 
> production code.
>
> - Jonathan M Davis

I find the warning helpful; it just needs to be limited to cases 
in which the code is actually unreachable. Removing it entirely 
wouldn't be a disaster, but we'd have to think carefully about 
whether the payoff of improvements to VRP and constant folding 
was really worth it.

Do you have an opinion regarding my solution #1 - defer "not 
reachable" warnings until all instantiations of a template have 
been analyzed, and only issue the warning if it applies to every 
one of them?


More information about the Digitalmars-d mailing list