Bug 7779 – D1-style opWhatever method is chosen in preference to opBinary under D2

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-03-25T15:07:57Z
Last change time
2019-07-16T04:01:30Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Stewart Gordon

Comments

Comment #0 by smjg — 2012-03-25T15:07:57Z
DMD 2.058, Win32 http://dlang.org/operatoroverloading.html#Binary `The expression: a op b is rewritten as both: a.opBinary!("op")(b) b.opBinaryRight!("op")(a) and the one with the ‘better’ match is selected. It is an error for both to equally match.` No mention of opAdd, etc. in there. Nonetheless, if such a method is present, it is chosen instead of behaving according to spec: ---------- import std.stdio; class Qwert { Qwert opBinary(string op : "+")(Qwert yuiop) { puts("opBinary"); return yuiop; } Qwert opAdd(Qwert yuiop) { puts("opAdd"); return this; } } void main() { Qwert asdfg = new Qwert; asdfg = asdfg + asdfg; } ---------- C:\Users\Stewart\Documents\Programming\D\Tests>opbinary_opadd opAdd ---------- (DMD 2.058 Win32) The spec gives opBinary/opBinaryRight as the way the AddExpression is resolved; yet the compiler does something else instead. It might be reasonable as as backward compatibility measure for the opAdd to be used as a fallback if there's no matching opBinary or opBinaryRight. But it's undocumented behaviour. When a matching opBinary is present, by allowing an undocumented feature to override a documented one the compiler is going against the spec. Swapping the order opBinary and opAdd in the code doesn't change the behaviour.
Comment #1 by pro.mathias.lang — 2019-07-16T04:01:30Z
This was "fixed" by deprecating D1-style operator overloads: https://github.com/dlang/dmd/pull/10130