Warning: statement is not reachable

Edwin van Leeuwen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Mar 1 13:46:33 PST 2016


On Tuesday, 1 March 2016 at 21:30:44 UTC, Tamas wrote:
> My d code doesn't compile using ldc2 1.0.0-alpha or anything 
> above DMD v2.068.0
>
> Using these compilers I get a lot of "Warning: statement is not 
> reachable". Then the both compiler crashes.
>
> ldc2 -w reach.d
> dmd -w reach.d
>
> reach.d:
>
>     struct Tag {}
>
>     template isTagged(S) {
>       enum bool isTagged =
>         delegate() {
>           foreach(attr; __traits(getAttributes, S)) {
>             static if (is(attr == Tag)) {
>               return true;
>             }
>           }
>           return false;
>         }();
>     }
>
>     void main() {
>       static @Tag struct MyStruct {}
>       static struct VanillaStruct {}
>       static assert(isTagged!MyStruct);
>       static assert(!isTagged!VanillaStruct);
>     }

We had the same problem in painlessjson and you can find some 
more background (with our fix) on it here: 
https://github.com/BlackEdder/painlessjson/issues/49

As I understand it: if attr == Tag the code will look something 
like this:

            foreach(attr; __traits(getAttributes, S)) {
                return true;
            }
            return false;

and the return false is basically unreachable.


More information about the Digitalmars-d-learn mailing list