www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3922] New: Wrong error message with return in void function

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922

           Summary: Wrong error message with return in void function
           Product: D
           Version: 2.041
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-03-10 05:02:48 PST ---
This is a wrong program:

void foo(int x) {
    return x;
}
void main() {}


Currently dmd2 generates a bad error message:
temp.d(2): Error: var has no effect in expression (x)

A much better error message can be:
temp.d(2): Error: return can't be used in a void function.

Bad error messages like this one do waste my programming time.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 10 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922



--- Comment #1 from bearophile_hugs eml.cc 2010-03-28 06:34:11 PDT ---
This program:


void foo() { return 0; }
enum x = foo();
void main() {}



With dmd 2.042 gives a wrong error message:
test.d(2): Error: variable bug3.x voids have no value

A better error message can be:
test.d(1): Error: return statement not allowed in void functions.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 28 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922


Iain Buclaw <ibuclaw ubuntu.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw ubuntu.com


--- Comment #2 from Iain Buclaw <ibuclaw ubuntu.com> 2010-08-21 04:47:59 PDT ---
*** Issue 4701 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922



--- Comment #3 from bearophile_hugs eml.cc 2010-08-21 05:09:39 PDT ---
Those error messages I have suggested are wrong, because using return in a void
function is OK:


void foo() {
    return;
}
void main() {}


It seems that this too is allowed:


void foo() {
    return;
}
void bar() {
    return foo();
}
void main() {}


What's wrong is returning something that is not void from a void function. So a
better error message is needed.


So for this wrong code:

void foo() {
    return 0;
}
void main() {}


A possible message:
temp.d(2): Error: a void function can return void only.

Alternative:
temp.d(2): Error: a void function can't return an int.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922


kennytm gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm gmail.com
           Platform|x86                         |All
            Version|2.041                       |D2
         OS/Version|Windows                     |All


--- Comment #4 from kennytm gmail.com 2011-06-08 11:11:04 PDT ---
The error seems to be that, in a 'void' function, the statement

     return expr;

is rewritten to

     {
        expr;
        return;
     }

so e.g. the following should-be-wrong (?) code

     int a() { return 4; }
     void b() { return a(); }

will compile successfully.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 08 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922



--- Comment #5 from kennytm gmail.com 2011-06-08 11:29:02 PDT ---
... indeed it is.


// ReturnStatement::semantic

    if (exp && tbret->ty == Tvoid && !implicit0)
    {
        /* Replace:
         *      return exp;
         * with:
         *      exp; return;
         */
        Statement *s = new ExpStatement(loc, exp);
        exp = NULL;
        s = s->semantic(sc);
        return new CompoundStatement(loc, s, this);
    }

Perhaps there should be a check in 'exp' here to ensure its type is 'void'.

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