D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5609 - struct opEquals doesn't conform to language specifications.
Summary: struct opEquals doesn't conform to language specifications.
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL: http://www.digitalmars.com/d/2.0/oper...
Keywords: bootcamp, rejects-valid
: 5979 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-02-18 04:09 UTC by eatingstaples
Modified: 2022-08-15 14:18 UTC (History)
5 users (show)

See Also:


Attachments
Code demonstrating the issue. (237 bytes, application/octet-stream)
2011-02-18 04:09 UTC, eatingstaples
Details

Note You need to log in before you can comment on or make changes to this issue.
Description eatingstaples 2011-02-18 04:09:28 UTC
Created attachment 916 [details]
Code demonstrating the issue.

The language specification states (with respect to opEquals in structs) "...the
expressions a.opEquals(b) and b.opEquals(a) are tried. If both resolve to the
same opEquals function, then the expression is rewritten to be a.opEquals(b).
If one is a better match then the other, or one compiles and the other does
not, the one is selected.
Otherwise, an error results."

Structs, however, don't properly follow the "if one compiles and the other does
not" rule, at least not for the form a.opEquals(rvalue). Example attached.
Comment 1 kennytm 2011-05-11 10:40:22 UTC
*** Issue 5979 has been marked as a duplicate of this issue. ***
Comment 2 SomeDude 2012-04-22 16:23:34 UTC
Code in the attachment:

struct A
{
	int x;
	
	A foo()
	{
		return A(x);
	}
	
	const bool opEquals(ref const A other)
	{
		return (x == other.x);
	}
}

void main()
{
	auto a = A(5);
	assert(a.foo == a); // OK
	assert(a == a.foo); // Error
}
Comment 3 RazvanN 2022-08-15 14:18:38 UTC
This bug report highlights a misunderstanding of the spec [1]. It is clearly stated: "Otherwise the expressions a.opEquals(b) and b.opEquals(a) are tried. If both resolve to the same opEquals function, then the expression is rewritten to be a.opEquals(b).". So, in the provided test case a.opEquals(b) and b.opEquals(a) resolve to the same function therefore `a.opEquals(b)` is selected. Only if opEquals did not resolve to the same function the compiler would have tried to semantically analyze the 2 constructions.

So this bug report is invalid.

[1] https://dlang.org/spec/operatoroverloading.html#equals