www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2214] New: No error if void function returns a value

reply d-bugmail puremagic.com writes:
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?


-- 
Jul 10 2008
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<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
Jul 10 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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.


-- 
Jul 10 2008
prev sibling parent d-bugmail puremagic.com writes:
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."


-- 
Jul 10 2008