Fixing the issue with "Statement unreachable"
Steven Schveighoffer
schveiguy at gmail.com
Mon Apr 27 03:59:57 UTC 2020
Is there a way we can fix this?
I have a situation with a large set of nested static ifs, coupled with
returns based on runtime code:
-----
static if(condA)
{
static if(condB)
{
static if(condC)
if(funA()) return true;
else static if(condD)
if(funB()) return true;
else
if(funC()) return true;
}
else static if(condD)
if(funD()) return true;
else
if(funE()) return true;
}
return false;
-----
Now, I have a situation where I'm inserting a condition nested way down.
And it always is going to return true:
----
static if(condA)
{
static if(condB)
{
static if(NEW_CONDITION) return true;
else static if(condC)
if(funA()) return true;
else static if(condD)
if(funB()) return true;
else
if(funC()) return true;
}
else static if(condD)
if(funD()) return true;
else
if(funE()) return true;
}
return false;
----
Now, all of a sudden, the return false at the end is no good. If condA,
condB, and NEW_CONDITION are true, the return false becomes a "Statement
not reachable".
I see 2 ways to fix this, and both are horrible.
Way 1: I have a special static if before-hand that combines all the
various conditions that get to that new branch, and then else everything
else. e.g.:
static if(condA && condB && NEW_CONDITION) return true;
else { ... original code ... }
Way 2: I move "return false" in all the other branches. I can refactor
out some of them, but I need a lot, which is super-annoying.
It bugs me that the compiler can't figure this out. Is there any way we
can fix this? The "warning" is not really true -- the statement IS
reachable in some instantiations.
Personally, I'd rather not have the compiler complain about ANY
unreachable statements than complain about ones that are actually
reachable. If it determines it's not reachable, just don't put it in the
binary.
Would there be a way to say "if this part of the code is reachable, then
include the following"?
(NOTE: I know I could switch all the "return true" to return the result
of the functions, but the real code is more complex and not simple
boolean calls)
-Steve
More information about the Digitalmars-d
mailing list