Bug 3905 – Wrong error message with wrong opBinary("in")
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-03-08T16:10:00Z
Last change time
2017-07-07T17:16:19Z
Keywords
diagnostic
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2010-03-08T16:10:09Z
This D2 program is wrong, because it needs a opBinaryRight to work:
struct Group {
int i1, i2;
bool opBinary(string s:"in")(int x) {
return x == this.i1 || x == this.i2;
}
}
auto enum r = 3 in Group(1, 2);
void main() {}
But the compiler gives a bad error message, that doesn't help the programmer much (and there are no associative arrays in this program):
test.d(7): Error: rvalue of in expression must be an associative array, not Group
For example a better error message can be:
test.d(7): Error: Group has no opBinaryRight("in") operator and int has no opBinary("in") operator.
Comment #1 by clugdbug — 2010-09-30T01:37:09Z
PATCH:
expression.c, line 10624, InExp::semantic()
default:
- error("rvalue of in expression must be an associative array, not %s", e2->type->toChars());
+ error("%s has no opBinaryRight(\"in\") operator and %s has no opBinary(\"in\") operator", e1->type->toChars(), e2->type->toChars());
case Terror:
return new ErrorExp();
}
Comment #2 by bearophile_hugs — 2010-09-30T05:10:35Z
Thank you Don :-) I need to be able to write a patch like this by myself.
Comment #3 by bugzilla — 2010-12-04T18:36:08Z
I'm not convinced this new error message is an improvement. All the expression error messages work on the assumption that operator overloads do not exist, as that is the normal case. Trying to write an error message for the normal case in terms of "you screwed up the operator overloads" is not helpful. For example,
int a;
double b;
a in b;
will give a pretty much incomprehensible error message to the user:
test.d(7): Error: double has no opBinaryRight("in") operator and int has no
opBinary("in") operator.
???
I'd prefer to leave the message as is. If someone is doing operator overloading, the message makes it clear the operator overloading failed to find a match.
Comment #4 by schveiguy — 2010-12-04T22:46:00Z
I agree that the proposed patch is not better. But the original message is invalid. This is a bug that needs to be fixed, regardless of whether you like the given proposals.
Point in fact, in *can* work on types besides associative arrays.
I tried this:
struct S {}
void main()
{
S s;
auto t = s + s;
}
And got:
Error: incompatible types for ((s) + (s)): 'S' and 'S'
So extrapolating to in, the error should look like:
Error: incompatible types for ((3) in (Group(1, 2))): 'int' and 'Group'
Although, I'm unsure why there are so many parentheses...
Comment #5 by bearophile_hugs — 2010-12-11T00:20:31Z
Saying that double has no "in" is correct (even if it's a bit noisy and overkill), while saying "rvalue of in expression must be an associative array, not Group" is wrong.
I think this bug report is valid still, because the current error message is confusing. So a better solution will be useful.
Comment #6 by yebblies — 2013-01-16T07:14:10Z
*** Issue 7837 has been marked as a duplicate of this issue. ***
Comment #7 by dlang-bugzilla — 2017-07-07T17:16:19Z
The error message is now:
test.d(7): Error: incompatible types for ((3) in (Group(1, 2))): 'int' and 'Group'
Fixed by https://github.com/dlang/dmd/pull/6140
*** This issue has been marked as a duplicate of issue 16499 ***