Bug 3746 – Misleading error message "OP has no effect in expression XXX), in void function

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-01-27T17:15:00Z
Last change time
2014-02-15T02:47:00Z
Keywords
diagnostic, patch
Assigned to
nobody
Creator
witold.baryluk+d

Comments

Comment #0 by witold.baryluk+d — 2010-01-27T17:15:05Z
On function with void return type, if one will put expression after return: void f() { auto a = 1, b = 3, c = 5; return a/b*c; } will end with compiler error like: aaa.d(4): Error: * has no effect in expression (a / b * c) My function "f" was lot bigger, and took me about 5 minutes to understand what is wrong. Could such bugs can be detected and be something like aaa.d(4): Error: no return statment allowed in void function/method
Comment #1 by b.helyer — 2010-01-27T19:59:24Z
Well, obviously return *is* allowed from void functions (void f() { return; }). But 'error: return statement with a value in a void function' would be a preferable message than talking about values having no effect in expressions.
Comment #2 by witold.baryluk+d — 2010-01-28T12:12:00Z
(In reply to comment #1) > Well, obviously return *is* allowed from void functions (void f() { return; }). > But 'error: return statement with a value in a void function' would be a > preferable message than talking about values having no effect in expressions. Yes you are right. Error messages should be as precise as possible.
Comment #3 by yebblies — 2011-06-12T23:18:47Z
*** Issue 3855 has been marked as a duplicate of this issue. ***
Comment #4 by yebblies — 2011-06-12T23:19:46Z
*** This issue has been marked as a duplicate of issue 3630 ***
Comment #5 by yebblies — 2011-06-12T23:21:28Z
My bad, this is a request for a better error.
Comment #6 by bearophile_hugs — 2011-06-13T03:21:08Z
*** Issue 3922 has been marked as a duplicate of this issue. ***
Comment #7 by bearophile_hugs — 2011-06-13T03:22:41Z
Issue 3922 contains some interesting comments. It seems this enhancement request for a better error message has many dupes!
Comment #8 by yebblies — 2011-06-13T13:34:34Z
https://github.com/D-Programming-Language/dmd/pull/121 This adds the error message: testx.d(4): Error: expression with no side effects used with void return Good enough?
Comment #9 by bearophile_hugs — 2011-06-13T14:31:09Z
(In reply to comment #8) > https://github.com/D-Programming-Language/dmd/pull/121 > > This adds the error message: > testx.d(4): Error: expression with no side effects used with void return > > Good enough? I don't understand. Elsewhere I have a bug report that asks for a new error message if you don't use the result of a pure function. But this bug report just asks for a better error message in the situation of using "return x;" in a void function. So I expected an error message like: foo.d(20): Error: a void function can't return 'int'.
Comment #10 by yebblies — 2011-06-13T22:21:03Z
(In reply to comment #9) > > I don't understand. Elsewhere I have a bug report that asks for a new error > message if you don't use the result of a pure function. > But this bug report just asks for a better error message in the situation of > using "return x;" in a void function. So I expected an error message like: > > foo.d(20): Error: a void function can't return 'int'. Returning an int expression from a void function is perfectly legal, so long as the expression has side effects. This is explicitly allowed by the spec. http://www.digitalmars.com/d/2.0/statement.html#ReturnStatement You're talking about two completely separate issues, both of which are enhancements. This issue is about providing a clearer error message without changing the spec.
Comment #11 by bearophile_hugs — 2011-06-14T05:14:02Z
(In reply to comment #10) > You're talking about two completely separate issues, both of which are > enhancements. This issue is about providing a clearer error message without > changing the spec. Walter has accepted one of the enhancement requests I was talking about (the one about pure functions). I was not aware the second too is an enhancement request, but after re-reading that part of the D specs I see you are right. So technically your error message is correct. But it's hard to keep in memory all the D specs, especially for D newbies. So I suggest a longer and more wordy error message, because this error message is not easy enough to understand. ---------- According to the D specs this code is illegal, is your patch raising an error on it? pure int sqr(int x) { return x * x; } void main() { return sqr(10); } ---------- According to the current D specs 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. So bug 3922 was not a dupe, it was an ehancement request! I have to reopen it.
Comment #12 by clugdbug — 2011-06-14T05:19:10Z
(In reply to comment #10) > (In reply to comment #9) > > > > I don't understand. Elsewhere I have a bug report that asks for a new error > > message if you don't use the result of a pure function. > > But this bug report just asks for a better error message in the situation of > > using "return x;" in a void function. So I expected an error message like: > > > > foo.d(20): Error: a void function can't return 'int'. > > Returning an int expression from a void function is perfectly legal, so long as > the expression has side effects. This is explicitly allowed by the spec. > http://www.digitalmars.com/d/2.0/statement.html#ReturnStatement Yes, but it was agreed on the newsgroup that this is a misfeature. (Andrei was horrified about it). It should be disallowed.
Comment #13 by yebblies — 2011-06-14T08:51:36Z
Without this feature, what should happen with lazy void? void lf(lazy void a) { a; } void main() { int i; lf(i++); }
Comment #14 by yebblies — 2011-10-09T19:22:15Z