Jump to page: 1 2
Thread overview
[Issue 3922] New: Wrong error message with return in void function
Aug 21, 2010
Iain Buclaw
Jun 08, 2011
kennytm@gmail.com
Jun 08, 2011
kennytm@gmail.com
[Issue 3922] Refuse returning nonvoid from void functions
Jan 05, 2012
Trass3r
Jan 06, 2012
timon.gehr@gmx.ch
Jan 06, 2012
Trass3r
Jan 06, 2012
timon.gehr@gmx.ch
Jan 19, 2012
yebblies
March 10, 2010
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: -------
March 28, 2010
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: -------
August 21, 2010
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: -------
August 21, 2010
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: -------
June 08, 2011
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: -------
June 08, 2011
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: -------
June 13, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3922


bearophile_hugs@eml.cc changed:

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


--- Comment #6 from bearophile_hugs@eml.cc 2011-06-13 03:21:06 PDT ---
*** This issue has been marked as a duplicate of issue 3746 ***

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


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|DUPLICATE                   |
           Severity|normal                      |enhancement


--- Comment #7 from bearophile_hugs@eml.cc 2011-06-14 05:18:56 PDT ---
Reopened as enhancement request after discussions in bug 3746.

This is from the DMD 2.053 specs: http://www.digitalmars.com/d/2.0/statement.html#ReturnStatement

  Expression is allowed even if the function specifies a void return type. The
Expression will be evaluated, but nothing will be returned. If the Expression
has no side effects, and the return type is void, then it is illegal.


So according to the D specs this code is illegal:

pure int sqr(int x) { return x * x; }
void main() {
    return sqr(10);
}


While this code is correct:

int sqr(int x) { return x * x; }
void main() {
    return sqr(10);
}

But I can't see this as correct, it's bug prone. I think it's better to turn into an error returning any nonvoid from a void function, regardless of side effects.

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


Trass3r <mrmocool@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid
                 CC|                            |mrmocool@gmx.de
           Severity|enhancement                 |normal


--- Comment #8 from Trass3r <mrmocool@gmx.de> 2012-01-05 14:29:46 PST ---
I totally agree, just like others:
http://d.puremagic.com/issues/show_bug.cgi?id=3746#c12
This has to happen. Let's consider this a bug and not an enhancements.

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



--- Comment #9 from bearophile_hugs@eml.cc 2012-01-05 15:36:21 PST ---
The main real problem (returning a non void from a void function) is now fixed: https://github.com/D-Programming-Language/dmd/commit/c942d51c8b1103d5ce4c3dfc03ae77c07c687cd6

-----------------

DMD 2.058head accepts code like this:


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


This code is formally correct, because no real value is ignored.

I don't like such code a lot because that code looks like the intent of the programmer is to return something, while this is not true. Maybe allowing this is useful for generic code, I don't know. I'd like to see examples of this usefulness in real code.

(But this discussion looks a bit different from the main issue that is now fixed, so maybe this should be moved to a new different Bugzilla issue).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2