D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7232 - Warning: statement is not reachable has no line number
Summary: Warning: statement is not reachable has no line number
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other All
: P2 normal
Assignee: No Owner
URL:
Keywords: diagnostic, patch
Depends on:
Blocks:
 
Reported: 2012-01-05 07:52 UTC by Robert Clipsham
Modified: 2014-11-07 18:45 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Robert Clipsham 2012-01-05 07:52:33 UTC
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 Kenji Hara 2012-01-06 08:51:01 UTC
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 Kenji Hara 2012-01-06 09:03:30 UTC
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.
Comment 3 Kenji Hara 2012-01-06 09:21:19 UTC
Posted bug 7240 as an enhancement.
Comment 5 Nick Treleaven 2014-11-07 18:45:14 UTC
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");
}