www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Warning: statement is not reachable

reply Tamas <user dlang.io> writes:
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.

I minimized the code to get the same warning, although the 
compilers do not crash this time, so this might not be the real 
culprit.

Any suggestion how to fix this?
Thanks!

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);
     }
Mar 01 2016
next sibling parent Edwin van Leeuwen <edder tkwsping.nl> writes:
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.
Mar 01 2016
prev sibling next sibling parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Tuesday, 1 March 2016 at 21:30:44 UTC, Tamas wrote:
           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);
     }
Sorry forgot to add the fix to the email. The following should work: bool tag = false; foreach(attr; __traits(getAttributes, S)) { static if (is(attr == Tag)) { tag = true; } } return tag;
Mar 01 2016
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 01.03.2016 22:30, Tamas wrote:
      struct Tag {}

      template isTagged(S) {
        enum bool isTagged =
          delegate() {
            foreach(attr; __traits(getAttributes, S)) {
              static if (is(attr == Tag)) {
                return true;
              }
            }
            return false;
          }();
      }
This is off topic, but you can use std.meta.staticIndexOf to check if something is in an alias seq: ---- template isTagged(S) { import std.meta: staticIndexOf; enum bool isTagged = staticIndexOf!(Tag, __traits(getAttributes, S)) >= 0; } ---- And for UDAs in particular there is std.traits.hasUDA: ---- template isTagged(S) { import std.traits: hasUDA; enum bool isTagged = hasUDA!(S, Tag); } ----
Mar 01 2016
parent reply Tamas <user dlang.io> writes:
Thanks, fixing this single issue solved the compiler crash too.

Thanks also for the tip using hasUDA! Works nicely!
Mar 01 2016
parent reply Johan Engelen <j j.nl> writes:
On Wednesday, 2 March 2016 at 07:42:09 UTC, Tamas wrote:
 Thanks, fixing this single issue solved the compiler crash too.
Did the compiler crash? Or just exit? (a crash would still be a bug)
Mar 02 2016
parent Tamas <user dlang.io> writes:
On Wednesday, 2 March 2016 at 09:37:03 UTC, Johan Engelen wrote:
 On Wednesday, 2 March 2016 at 07:42:09 UTC, Tamas wrote:
 Thanks, fixing this single issue solved the compiler crash too.
Did the compiler crash? Or just exit? (a crash would still be a bug)
Crashed, just like in the case of Edwin's linked github issue: https://github.com/BlackEdder/painlessjson/issues/49 dmd failed with exit code 1. The minimized code in the OP does not crashes the compiler though.
Mar 02 2016