Bug 15354 – unstable operator overloading with mixin expression

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2015-11-18T13:01:14Z
Last change time
2024-12-13T18:45:53Z
Assigned to
No Owner
Creator
Kenji Hara
Moved to GitHub: dmd#17735 →

Comments

Comment #0 by k.hara.pg — 2015-11-18T13:01:14Z
Test case: struct S { int x; int y; void opIndexAssign(int v, int n) { x = v; } ref int opIndex(int n) { return y; } } void main() { S s1; s1[0] = 1; assert(s1.x == 1); S s2; mixin("s2[0]") = 1; assert(s2.y == 1); // shouldn't be s2.x == 1 ? }
Comment #1 by kolos80 — 2016-07-08T16:06:02Z
This is broken with any overloads, not only for operators. Stumbled upon it today, trying to implement a Visitor pattern for my toy language compiler. Here's a simplified version of it: //------------------------- abstract class Expression { void accept(CodeVisitor visitor); } class Identifier: Expression { override void accept(CodeVisitor visitor) { visitor.visit(this); } } class BinaryExpr: Expression { Expression left, right; override void accept(CodeVisitor visitor) { left.accept(visitor); right.accept(visitor); visitor.visit(this); } } class Unit {} abstract class CodeVisitor { mixin template NotImplementedVisit(T: Expression) { void visit(T expr) { assert(0); } } void visit(Unit unit) { assert(0); } mixin NotImplementedVisit!Identifier; mixin NotImplementedVisit!BinaryExpr; } //------------------------- Although this is perfectly fine to me, DMD doesn't agree: src/issue15354.d(7,22): Error: function issue15354.CodeVisitor.visit (Unit unit) is not callable using argument types (Identifier) src/issue15354.d(16,22): Error: function issue15354.CodeVisitor.visit (Unit unit) is not callable using argument types (BinaryExpr) But if I implement each overload in CodeVisitor manually, it works just fine: //------------------------- abstract class CodeVisitor { void visit(Unit unit) { assert(0); } void visit(Identifier expr) { assert(0); } void visit(BinaryExpr expr) { assert(0); } } //------------------------- P.S. for Kenji: I was quite surprised when your test case ran just fine, then I saw your comment and realised that you were actually testing for invalid behaviour. Please don't do that, tests are supposed to test valid behaviour, so you should have wrote it the other way around, so it fails today, but succeeds when the bug is fixed. It would make maintainer's life easier, if he can just copy your test case to compiler's test suite.
Comment #2 by robert.schadek — 2024-12-13T18:45:53Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17735 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB