Thread overview
[Issue 3160] New: ICE(cgcod.c 1511) returning string from void main, D1 only
Sep 15, 2009
Don
Sep 15, 2009
Don
[Issue 3160] ICE(cgcod.c 1511-D1) or bad code-D2 returning string from void main
Sep 15, 2009
Don
Oct 01, 2009
Don
Oct 06, 2009
Walter Bright
July 10, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3160

           Summary: ICE(cgcod.c 1511) returning string from void main, D1
                    only
           Product: D
           Version: 1.045
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: ice-on-invalid-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: clugdbug@yahoo.com.au


Courtesy of bearophile.

void main() { return "a"; }

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |regression


--- Comment #1 from Don <clugdbug@yahoo.com.au> 2009-09-14 23:54:41 PDT ---
This is actually a regression.
Did not ICE in 0.175, 1.006. Began to ICE at 1.017 or earlier.
ICE in 2.000->2.022. Was fixed in 2.023.
However, although it didn't ICE on those versions, it's an accepts-invalid.
Seems to be adding an implicitly cast to void. (BTW, the accepts-invalid
applies to all functions, not just main).

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


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

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


--- Comment #2 from Don <clugdbug@yahoo.com.au> 2009-09-15 08:44:16 PDT ---
ROOT CAUSE: The logic for inserting a "return 0" at the end of a void main, and the logic for evaluating the return expression in a void function, interact incorrectly. The case where BOTH these things need to happen was missing. BTW, this is probably wrong in D2 as well.


PATCH: statement.c, last lines of ReturnStatement::semantic (around line 2891):

-    if (exp && tbret->ty == Tvoid && !fd->isMain())
+    if (exp && tbret->ty == Tvoid && !implicit0)
    {
    /* Replace:
     *    return exp;
     * with:
     *    exp; return;
     */
    Statement *s = new ExpStatement(loc, exp);
    loc = 0;
+    if (fd->isMain()) exp = new IntegerExp(0);
+    else
        exp = NULL;
    return new CompoundStatement(loc, s, this);
    }
    return this;
}

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|ice-on-invalid-code         |ice-on-valid-code
            Summary|ICE(cgcod.c 1511) returning |ICE(cgcod.c 1511-D1) or bad
                   |string from void main, D1   |code-D2 returning string
                   |only                        |from void main
           Severity|regression                  |critical


--- Comment #3 from Don <clugdbug@yahoo.com.au> 2009-09-15 11:31:30 PDT ---
In D2, rather than an ICE, it generates wrong code. Can't really claim ICE<->wrong-code is a regression.

Technically, this is valid, but I'm actually not sure if this type of code should be allowed. Should it check for side-effects? IE, void main() { return 2; } --> should this generate a "expression (2) has no effect" error? Seems like a newbie trap.

Here's a more formal patch against DMD2.032:

Index: statement.c ===================================================================
--- statement.c    (revision 54)
+++ statement.c    (revision 55)
@@ -3454,7 +3454,7 @@
     return gs;
     }

-    if (exp && tbret->ty == Tvoid && !fd->isMain())
+    if (exp && tbret->ty == Tvoid && !implicit0)
     {
     /* Replace:
      *    return exp;
@@ -3463,7 +3463,10 @@
      */
     Statement *s = new ExpStatement(loc, exp);
     loc = 0;
-    exp = NULL;
+    if (fd->isMain())
+        exp = new IntegerExp(0);
+    else
+        exp = NULL;
     return new CompoundStatement(loc, s, this);
     }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3160



--- Comment #4 from Don <clugdbug@yahoo.com.au> 2009-10-01 05:47:20 PDT ---
The patch for the related bug 3344 adds a check for side-effects.

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


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2009-10-06 02:17:15 PDT ---
Fixed dmd 1.048 and 2.033

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