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 next 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
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Jun 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Jun 14 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Jan 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
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: -------
Jan 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr gmx.ch


--- Comment #10 from timon.gehr gmx.ch 2012-01-05 16:40:47 PST ---
This is required for forwarding and cannot change.

auto foo(T...)(T args){return bar(args);}

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



--- Comment #11 from bearophile_hugs eml.cc 2012-01-05 16:54:55 PST ---
(In reply to comment #10)
 This is required for forwarding and cannot change.
 
 auto foo(T...)(T args){return bar(args);}
Thank you for your answer. If you are right and it can't change, then this issue should be closed. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 05 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922



--- Comment #12 from Trass3r <mrmocool gmx.de> 2012-01-06 04:59:49 PST ---
(In reply to comment #10)
 This is required for forwarding and cannot change.
 
 auto foo(T...)(T args){return bar(args);}
Well couldn't this special case be checked? Accidentally turning the return type into void or forgetting to change the type from void after you added a return x; is much more common than such code imho. Also should a function returning something or not really depend on argument types? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922



--- Comment #13 from timon.gehr gmx.ch 2012-01-06 06:28:39 PST ---
(In reply to comment #12)
 (In reply to comment #10)
 This is required for forwarding and cannot change.
 
 auto foo(T...)(T args){return bar(args);}
Well couldn't this special case be checked? Accidentally turning the return type into void
Who on earth accidentally changes a function's return type? Also, if the function does not actually return void, a compile error results.
 or forgetting to change the type
 from void after you added a return x; is much more common than such code imho.
That will give a compile error already. (unless x is a property function returning void)
 Also should a function returning something or not really depend on argument
 types?
You realize that it is already disallowed to return non-void from a function returning void? The only case that is allowed and should stay allowed is something like this: void bar(){...} void foo(){return bar();} -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 06 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3922


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |FIXED


--- Comment #14 from yebblies <yebblies gmail.com> 2012-01-19 13:44:18 EST ---
As far as I can tell, this bug has been fixed with issue 3746 and issue 5399.
Returning a value from a void function is only allowed when the value evaluates
to void, preventing the value from being lost accidentally.

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