Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 01, 2016 Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
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); } |
March 01, 2016 Re: Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tamas | 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. |
March 01, 2016 Re: Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tamas | 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;
|
March 01, 2016 Re: Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tamas | 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);
}
----
|
March 02, 2016 Re: Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | Thanks, fixing this single issue solved the compiler crash too. Thanks also for the tip using hasUDA! Works nicely! |
March 02, 2016 Re: Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tamas | 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)
|
March 02, 2016 Re: Warning: statement is not reachable | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johan Engelen | 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. |
Copyright © 1999-2021 by the D Language Foundation