Bug 11094 – Disuniform error messages with overloaded + and ^ operators

Status
NEW
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2013-09-22T04:41:14Z
Last change time
2024-12-13T18:11:43Z
Keywords
diagnostic
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#18675 →

Comments

Comment #0 by bearophile_hugs — 2013-09-22T04:41:14Z
With DMD 2.064alpha this code gives no errors, it seems correct: struct Foo { Foo opBinary(string op)(in Foo r) inout if (op == "+") { return Foo(); } Foo opBinary(string op)(in Foo r) inout if (op == "^") { return Foo(); } } void main() { const x = Foo(); auto r1 = x + Foo(); auto r2 = x ^ Foo(); } But if I forget the inouts: struct Foo { Foo opBinary(string op)(in Foo r) if (op == "+") { return Foo(); } Foo opBinary(string op)(in Foo r) if (op == "^") { return Foo(); } } void main() { const x = Foo(); auto r1 = x + Foo(); auto r2 = x ^ Foo(); } DMD gives: test.d(11): Error: incompatible types for ((x) + (Foo())): 'const(Foo)' and 'Foo' test.d(12): Error: 'x' is not of integral type, it is a const(Foo) test.d(12): Error: 'Foo()' is not of integral type, it is a Foo I suggest: - To give only one error message for the "^" case; - To give the same error messages for both "+" and "^"; - Perhaps the error message could explain better the problem, or/and perhaps it could suggest the use of 'inout'.
Comment #1 by andrej.mitrovich — 2014-04-27T19:43:07Z
Note that it is unrelated to the overloads, you could have a single opBinary to reproduce: ----- struct Foo { Foo opBinary(string op)(in Foo r) if (op == "+" || op == "^") { return Foo(); } } void main() { const x = Foo(); auto r1 = x + Foo(); auto r2 = x ^ Foo(); } -----
Comment #2 by ben.james.jones — 2022-05-26T20:31:49Z
I took a look at this, and the difference seems to be related to the extra check(s) added at the end of the expressionsem visitors for AddExp vs XorExp: //in xor only if (exp.checkIntegralBin() || exp.checkSharedAccessBin(sc)) return setError(); A couple of questions. This happens earlier in the visitor: Expression e = exp.op_overload(sc); if (e) { result = e; return; } I assume e is null if the op overload doesn't apply (in this case there are constness issues), but putting an error in opover.visitBin seems like maybe the wrong fix? Those extra checks make sense for xor, is it worth trying to add a supplemental error or something that sees if they tried to do op overloading? There's a TON of duplication between the addexp and xorexp visitors in expressionsem, and I assume probably with most binop visitors. Is it worth trying to refactor to reduce the duplication?
Comment #3 by robert.schadek — 2024-12-13T18:11:43Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18675 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB