Thread overview
[Issue 3574] New: post-condition is not evaluated if there is no return statement
Dec 04, 2009
Koroskin Denis
Jan 27, 2010
Don
[Issue 3574] post-condition in void main() is not evaluated if there is no return statement
Jan 28, 2010
Don
Jan 28, 2010
Don
Jun 06, 2012
Kenji Hara
Jun 11, 2012
Walter Bright
December 04, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3574

           Summary: post-condition is not evaluated if there is no return
                    statement
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: 2korden@gmail.com


--- Comment #0 from Koroskin Denis <2korden@gmail.com> 2009-12-04 05:52:43 PST ---
import std.stdio;

void main()
out
{
    writeln("out");
}
body
{
    //return;
}

"out" is only printed if return is explicit.

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


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug@yahoo.com.au
            Version|unspecified                 |1.00


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2010-01-27 02:47:38 PST ---
Applies to D1 (even ancient versions) as well as D2.

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


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
            Summary|post-condition is not       |post-condition in void
                   |evaluated if there is no    |main() is not evaluated if
                   |return statement            |there is no return
                   |                            |statement


--- Comment #2 from Don <clugdbug@yahoo.com.au> 2010-01-28 00:29:30 PST ---
This is a very obscure bug; it's of interest for educational purposes only.
Only void main() is affected, and it's because the return 0; needs to be added
AFTER the invariant is processed, not before.
This patch (against DMD2, svn 356) just moves the return 0; insertion slightly
later in FuncDeclaration::semantic3().


Index: func.c ===================================================================
--- func.c    (revision 356)
+++ func.c    (working copy)
@@ -1287,16 +1287,8 @@

         int offend = blockexit & BEfallthru;
 #endif
-        if (type->nextOf()->ty == Tvoid)
+        if (type->nextOf()->ty != Tvoid)
         {
-            if (offend && isMain())
-            {    // Add a return 0; statement
-            Statement *s = new ReturnStatement(0, new IntegerExp(0));
-            fbody = new CompoundStatement(0, fbody, s);
-            }
-        }
-        else
-        {
             if (offend)
             {   Expression *e;
 #if DMDV1
@@ -1462,8 +1454,17 @@
             }
             ReturnStatement *s = new ReturnStatement(0, e);
             a->push(s);
-        }
+        }
         }
+#if DMDV2
+        int blockexit = fbody ? fbody->blockExit() : BEfallthru;
+        int offend = blockexit & BEfallthru;
+#endif
+        if (offend && isMain() && type->nextOf()->ty == Tvoid)
+        {   // For void main(), add a 'return 0' statement
+        ReturnStatement *s = new ReturnStatement(0, new IntegerExp(0));
+        a->push(s);
+        }

         fbody = new CompoundStatement(0, a);
 #if DMDV2

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


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|patch                       |


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2010-01-28 14:00:19 PST ---
Oops, there's something wrong with this patch. It can interfere badly with foreach for some reason.

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


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
           Platform|Other                       |All
            Version|1.00                        |D1 & D2


--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2012-06-06 07:21:09 PDT ---
https://github.com/D-Programming-Language/dmd/pull/986

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



--- Comment #5 from github-bugzilla@puremagic.com 2012-06-11 14:33:53 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/2b05220ce3c10602748037189cfd6f3123faafd7
fix Issue 3574 - post-condition in void main() is not evaluated if there is no
return statement

https://github.com/D-Programming-Language/dmd/commit/48d389b430b16b61d5f101485deb004a50b3a700 Merge pull request #986 from 9rnsr/fix3574

Issue 3574 - post-condition in void main() is not evaluated if there is no
return statement

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


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


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