Bug 5399 – Return the result of a nonvoid function in a void function

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P5
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2011-01-01T13:49:00Z
Last change time
2011-10-09T14:54:12Z
Keywords
accepts-invalid, patch
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-01-01T13:49:57Z
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(); }
Comment #1 by bearophile_hugs — 2011-01-01T23:00:02Z
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.
Comment #2 by bugzilla — 2011-01-01T23:33:12Z
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.
Comment #3 by nfxjfg — 2011-01-02T05:45:57Z
(In reply to comment #2) > 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.
Comment #4 by ibuclaw — 2011-01-02T06:24:38Z
(In reply to comment #2) > 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)
Comment #5 by andrei — 2011-01-02T06:48:17Z
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.
Comment #6 by nfxjfg — 2011-01-02T07:46:29Z
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.
Comment #7 by andrei — 2011-01-02T09:05:29Z
(In reply to comment #6) > 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.
Comment #8 by yebblies — 2011-06-29T01:58:29Z
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.
Comment #9 by clugdbug — 2011-06-29T02:29:05Z
(In reply to comment #8) > 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;}) ?
Comment #10 by yebblies — 2011-06-29T10:02:12Z
(In reply to comment #9) > 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.
Comment #11 by bugzilla — 2011-10-09T14:54:12Z