Bug 10755 – 'has no effect in expression' error for return too with comma operator
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-04T10:34:00Z
Last change time
2017-07-05T20:42:04Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2013-08-04T10:34:27Z
With dmd 2.064alpha this code gives:
void main() {
1;
}
test.d(2): Error: long has no effect in expression (1)
- - - - - - - - - - - -
This gives an error, commas are disallowed here:
void main() {
int[10] a;
auto x = a[1, 2];
}
test.d(3): Error: only one index allowed to index int[10u]
- - - - - - - - - - - -
This too gives errors:
void main() {
int[10, 20] a;
}
test.d(2): Error: found ',' when expecting ']'
test.d(2): Error: no identifier for declarator int[10]
test.d(2): Error: semicolon expected, not '20'
test.d(2): Error: found ']' when expecting ';' following statement
- - - - - - - - - - - -
This is disallowed:
void main() {
int x = 1, 2;
}
test.d(2): Error: no identifier for declarator int
test.d(2): Error: semicolon expected, not '2'
- - - - - - - - - - - -
This gives an error:
void main() {
int x;
x = 1, 2;
}
test.d(3): Error: long has no effect in expression (2)
- - - - - - - - - - - -
While this code compiles with no errors nor warnings:
int foo() {
return 1, 2; // line 2
}
int bar() {
return foo(), 3; // line 5
}
void main() {
assert(bar() == 3);
}
Inside bar() there is a call to foo(). foo() is not pure, but inside foo() "1" is a pure expression (a literal), so for this program I suggest to generate an error at line 2, and disallow comma syntax at return points, but probably no warning at line 5:
test.d(2): Error: int has no effect in expression (1)
- - - - - - - - - - - -
This generates an error:
int foo() {
return 1;
}
void main() {
int x;
x = foo(), 2;
}
test.d(6): Error: long has no effect in expression (2)
So maybe it's right to generate an error at line 5 too.
- - - - - - - - - - - -
Comment #1 by yebblies — 2014-02-05T03:43:16Z
There is some dead code in sideeffect.c that implements this:
/* This isn't used yet because the only way an expression has an unused sub-expression
* is with the CommaExp, and that currently generates messages from rewrites into comma
* expressions. Needs more investigation.
*/
void Expression::useValue()
{
#if 0
// Disabled because need to cast(void) the a,b code generation
void *p;
apply(&lambdaUseValue, &p);
#endif
}
#if 0
int lambdaUseValue(Expression *e, void *param)
{
switch (e->op)
{
case TOKcomma:
{ CommaExp *ce = (CommaExp *)e;
discardValue(ce->E1);
break;
}
default:
break;
}
return 0;
}
#endif