Bug 7232 – Warning: statement is not reachable has no line number
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2012-01-05T07:52:00Z
Last change time
2014-11-07T18:45:14Z
Keywords
diagnostic, patch
Assigned to
nobody
Creator
robert
Comments
Comment #0 by robert — 2012-01-05T07:52:33Z
When the following is compiled with -w or -wi, it will give a warning without a line number
----
bool addArticle()
{
scope(failure) return false;
return true;
}
----
Tested on dmd 2.057 on OS X 64 and Ubuntu 32.
$ dmd -w test.d
Warning: statement is not reachable
Comment #1 by k.hara.pg — 2012-01-06T08:51:01Z
https://github.com/D-Programming-Language/dmd/pull/610
In addArticle function, dmd translates the body code like follows:
try {
return true;
}
catch (Throwable __o) {
return false;
throw __o; // #1 rethrow catched exception object
}
The "statement is not reachable" warning is caused by line #1.
After my patch, the translation result would change like follows:
try {
return true;
}
catch (Throwable __o) {
return false; // this statement never fall through next.
// so next unreachable rethrowing is implicitly removed.
}
Finally, original warning would never be generated.
Comment #2 by k.hara.pg — 2012-01-06T09:03:30Z
Technical note:
Maybe, the original issue by Robert Clipsham is "unreachable scope(failure) should warn "statement is not reachable" _with line number_.
But today it is technically enhancement. Because:
1. Current D2 dmd does only check Exception throwing possibilities in flow analysis.
That means Throwable is not the target of the analysis. In above code,
scope(failure) return false;
return true; // (a)
dmd does not consider the statement (a) throws Throwable or not.
2. scope(failure) catches Throwable object and rethrow it. Therefore the scope(failure) statement is always analysed as *may be reachable*.
From the two reasons, current dmd cannot detect that the `scope(failure) return false;` is not reachable.
There's a missing line number with -w here also (recent dmd):
import std.stdio;
void f()
{
throw new Exception("msg");
scope(exit) write("5");
scope(success) write("6");
}