Bug 4484 – Warning for unreachable code in scope statements is too confusing
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-07-18T19:53:00Z
Last change time
2012-12-20T14:21:37Z
Keywords
rejects-valid
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2010-07-18T19:53:09Z
Right now, if you compile the following code with -w (it compiles fine without -w)
import std.stdio;
void someFuncWhichCanThrow(int val)
{
throw new Exception("aaaaaaaa");
}
void main(string[] args)
{
foreach(arg; args)
{
scope(failure) continue;
writeln(arg);
}
}
you get the error
Warning: statement is not reachable
This will be highly confusing to many programmers. It _is_ technically correct (there _is_ an unreachable statement), but not clear enough. It doesn't even give a line number! If I understand correctly, what's happening is that the foreach loop becomes this
foreach(arg; args)
{
try
{
writeln(arg);
}
catch(Exception e)
{
continue;
throw e;
}
}
The throw statement is then unreachable (which is a warning and thus an error with -w). However, since it's not in the programmer's code, the error gives no line number. The statement which is unreachable isn't even in their code! A better error is needed for this. Putting a statement in a scope statement which will result in unreachable code should be a warning - if not an error - but it needs to clearly indicate that the programmer is messing up with the scope statement, not give a generic message about there being an unreachable statement.
Actually, thinking about this a bit more, maybe
scope(failure) continue;
shouldn't be disallowed, since you may very well want to eat the exception and continue. However, if that's the case, then -w shouldn't complain about unreachable statements like it currently does with
scope(failure) continue;
So, either the warning needs to be fixed so that it's properly clear, or the code generated by
scope(failure) flow-control-statement-of-some-kind;
needs to be fixed so that it doesn't have the throw in it anymore.
Comment #1 by issues.dlang — 2010-11-15T01:56:17Z
scope(failure) assert(0);
would be another nice thing to be able to do - particularly when trying to ensure that a function is nothrow (but it results in the error about a statement being unreachable). Putting the whole function in a try-catch block to do that is definitely uglier.
In any case, I think that I'm promoting this to a normal bug rather than an enhancement request. The fact that scope statements translate into try-catch blocks is an implementation detail which shouldn't leak into error messages. While it makes good sense to implement scope that way, I'm not sure that there's anything really requiring that it be implemented that way, and the errors about unreachable code make various useful constructs illegal when they really shouldn't be.
Comment #2 by andrej.mitrovich — 2012-12-20T14:16:24Z