Thread overview
[Issue 2214] New: No error if void function returns a value
Jul 10, 2008
d-bugmail
[Issue 2214] No error if void function returns a non-void value
Jul 10, 2008
d-bugmail
Jul 10, 2008
d-bugmail
July 10, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2214

           Summary: No error if void function returns a value
           Product: D
           Version: unspecified
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: 2korden@gmail.com


void f() {
    return new Exception("stuff");   // no error is generated
}

void main() {
    return f();
}

Result:
Process returned 8990704 (0x892FF0)

Is this by design or by accident?


-- 

July 10, 2008
<d-bugmail@puremagic.com> wrote in message news:bug-2214-3@http.d.puremagic.com/issues/...
> http://d.puremagic.com/issues/show_bug.cgi?id=2214
>
>           Summary: No error if void function returns a value
>           Product: D
>           Version: unspecified
>          Platform: PC
>        OS/Version: Windows
>            Status: NEW
>          Keywords: accepts-invalid
>          Severity: normal
>          Priority: P2
>         Component: DMD
>        AssignedTo: bugzilla@digitalmars.com
>        ReportedBy: 2korden@gmail.com
>
>
> void f() {
>    return new Exception("stuff");   // no error is generated
> }
>
> void main() {
>    return f();
> }
>
> Result:
> Process returned 8990704 (0x892FF0)
>
> Is this by design or by accident?

I think it's by design.  It's come in very handy in a lot of generic code I've written.  I.e. if I write a function which wraps another function, I don't have to do an icky test to see if the return type is void or not, and decide whether or not to "return realFunction();".  I just have a single codepath that works for void functions and non-void functions.

From a completely stupid standpoint, it's a nice shortcut for "foo(); return;" :P


July 10, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2214


2korden@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|No error if void function   |No error if void function
                   |returns a value             |returns a non-void value




------- Comment #1 from 2korden@gmail.com  2008-07-10 14:17 -------
I agree that this code is correct:

void foo() {
}

void bar() {
  return foo();
}

while I think that this isn't:

void foo() {
  return -1;
}

Besides, if it *is* supposed to work this way, it should be mentioned in spec.


-- 

July 10, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2214


matti.niemenmaa+dbugzilla@iki.fi changed:

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




------- Comment #2 from matti.niemenmaa+dbugzilla@iki.fi  2008-07-10 15:20 -------
It is in the spec.

http://www.digitalmars.com/d/1.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."


--