Thread overview
[Issue 4484] New: Warning for unreachable code in scope statements is too confusing
Jul 19, 2010
Jonathan M Davis
Nov 15, 2010
Jonathan M Davis
Dec 20, 2012
Andrej Mitrovic
Dec 20, 2012
Jonathan M Davis
July 19, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4484

           Summary: Warning for unreachable code in scope statements is
                    too confusing
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jmdavisProg@gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg@gmail.com> 2010-07-18 19:53:09 PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4484


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
           Severity|enhancement                 |normal


--- Comment #1 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-11-15 01:56:17 PST ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4484


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com


--- Comment #2 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-20 14:16:24 PST ---
I'm not seeing the warning in 2.060. Fixed?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 20, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4484


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------