www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5399] New: Return the result of a nonvoid function in a void function

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

           Summary: Return the result of a nonvoid function in a void
                    function
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



In D (rightly) you can't return a value different from void inside a void
function. But this code compiles and runs with no errors in DMD 2.051 (bug
found by Daren Scot Wilson):


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

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


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement



This isn't a bug, I was wrong. D2 specs clearly show this is working as
expected:
http://www.digitalmars.com/d/2.0/statement.html

ReturnStatement:
    return;
    return Expression ;

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.


Indeed, this too compiles:

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


But this is a potentially dangerous corner case in the return rules.

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


Walter Bright <bugzilla digitalmars.com> changed:

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



23:33:12 PST ---
It is not a dangerous corner case, it is a deliberate design choice. It is
meant to facilitate writing generic code so that the same code can be generated
for void and non-void return values.

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


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com




 It is not a dangerous corner case, it is a deliberate design choice. It is
 meant to facilitate writing generic code so that the same code can be generated
 for void and non-void return values.
Wow, D is really full of idiocy, isn't it? If you wanted to facilitate that, you'd just allow declaring void variables and all that. I've written generic code where _that_ would have been quite useful. But returning non-void values in void functions? Garbage language feature. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 02 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Iain Buclaw <ibuclaw ubuntu.com> changed:

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




 It is not a dangerous corner case, it is a deliberate design choice. It is
 meant to facilitate writing generic code so that the same code can be generated
 for void and non-void return values.
Just for clarification, so you allow this, but the return value is always ignored? (ie: 0) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 02 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P2                          |P5
             Status|RESOLVED                    |REOPENED
                 CC|                            |andrei metalanguage.com
         Resolution|INVALID                     |
           Severity|enhancement                 |major



06:48:17 PST ---
This is a very problematic misfeature that takes no effort to remove. In
particular I confirm it is of no or negative use to generic programming.
Walter, please let's remove it in the next release. Thank you.

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




I think the issue is with allowing stuff like this:

Result wrapCall(alias call, Result, Args...)(Args args) {
   return call(args);
}

And then making it work even if the result of the call is void:

wrapCall(&something, void, int, int)(1, 2);

That requires that you can return a void value. Returning a void normally
wouldn't make sense, but as you can see it simplifies generic programming.

Somehow it made sense in Walter's head to allow returning _anything_ from a
void function. (It would make sense if void would work like Scala's Unit, but
void doesn't.)

Walter, please explain.

By the way if D were really orthogonal and would follow any logic, you wouldn't
have any problem with this code:

Result wrapCall(alias call, Result, Args...)(Args args) {
   try {
      return call(args);
   } catch {
      writefln("call failed!");
      return Result.init;
   }
}

This works, except when Result is void. Then you have to use static if,
duplicate the core code around the actual call if that is more complicated than
in the given example, and so on. (I had this in real world code.)

Sure makes a lot of sense.

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




09:05:29 PST ---

 I think the issue is with allowing stuff like this:
 
 Result wrapCall(alias call, Result, Args...)(Args args) {
    return call(args);
 }
 
 And then making it work even if the result of the call is void:
 
 wrapCall(&something, void, int, int)(1, 2);
 
 That requires that you can return a void value. Returning a void normally
 wouldn't make sense, but as you can see it simplifies generic programming.
Yes, that's a classic in C++ too. My assessment refers not to forwarding across void function, but to void functions returning non-void expressions. To clarify: forwarding from one void function to another void function is useful. Having a void function return a non-void value should be removed. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 02 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5399


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com



I'll copy what I said in issue 3746:

Without this feature, what should happen with lazy void?

void lazyFunc(lazy void a) { a; }

void main()
{
   int i;
   lazyFunc(i++);
}

lazyFunc(i++) is currently re-written to something like
lazyFunc({return i++;})
This of course is broken if returning a non-void from a void function is
disallowed.

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au




 I'll copy what I said in issue 3746:
 
 Without this feature, what should happen with lazy void?
 
 void lazyFunc(lazy void a) { a; }
 
 void main()
 {
    int i;
    lazyFunc(i++);
 }
 
 lazyFunc(i++) is currently re-written to something like
 lazyFunc({return i++;})
 This of course is broken if returning a non-void from a void function is
 disallowed.
What's wrong with lazyFunc( { i++; return;}) ? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 29 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5399


yebblies <yebblies gmail.com> changed:

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




 What's wrong with
 lazyFunc( { i++; return;})
 ?
For some reason I was thinking this would skip the 'expression has no effect' errors. https://github.com/D-Programming-Language/dmd/pull/174 Fixing this found a bug in phobos. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jun 29 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5399


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED
           Severity|major                       |enhancement



14:54:12 PDT ---
https://github.com/D-Programming-Language/dmd/commit/c942d51c8b1103d5ce4c3dfc03ae77c07c687cd6

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