digitalmars.D.learn - Warning: statement is not reachable
- Tamas (30/30) Mar 01 2016 My d code doesn't compile using ldc2 1.0.0-alpha or anything
- Edwin van Leeuwen (11/36) Mar 01 2016 We had the same problem in painlessjson and you can find some
- Edwin van Leeuwen (10/24) Mar 01 2016 Sorry forgot to add the fix to the email. The following should
- ag0aep6g (17/29) Mar 01 2016 This is off topic, but you can use std.meta.staticIndexOf to check if
- Tamas (2/2) Mar 01 2016 Thanks, fixing this single issue solved the compiler crash too.
- Johan Engelen (3/4) Mar 02 2016 Did the compiler crash? Or just exit?
- Tamas (5/9) Mar 02 2016 Crashed, just like in the case of Edwin's linked github issue:
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
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
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
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
Thanks, fixing this single issue solved the compiler crash too. Thanks also for the tip using hasUDA! Works nicely!
Mar 01 2016
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
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: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.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