Bug 9281 – Enum struct with op overloading doesnt works
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-01-08T01:42:00Z
Last change time
2013-01-10T23:16:33Z
Assigned to
nobody
Creator
kozzi11
Comments
Comment #0 by kozzi11 — 2013-01-08T01:42:33Z
module main;
import std.algorithm;
import std.array;
immutable struct Column {
string opAssign(V)(V tValue) {
return tValue;
}
}
immutable test1 = Column();
enum test2 = Column();
void main(string[] args)
{
string where = test1 = "something"; // works ok
std.stdio.writeln(where);
where = test2 = "something else"; // works 2.060, dont compile on 2.061
std.stdio.writeln(where);
}
Comment #1 by k.hara.pg — 2013-01-10T06:22:48Z
Did this really work with 2.060? I cannot reproduce the "works 2.060".
Column.opAssign is an immutable member function, then we can call it from immutable object test1, but cannot call from mutable object test2.
So, the error in test2 = "something else" is correct, as far as I know.
Comment #2 by kozzi11 — 2013-01-10T07:42:45Z
(In reply to comment #1)
> Did this really work with 2.060? I cannot reproduce the "works 2.060".
>
> Column.opAssign is an immutable member function, then we can call it from
> immutable object test1, but cannot call from mutable object test2.
>
> So, the error in test2 = "something else" is correct, as far as I know.
Yes my fault, I try to simplify too much. Here is more detailed description
Code which works on 2.060 and don`t compile on 2.061
module main;
import std.algorithm;
import std.array;
immutable struct Column {
string opAssign(V)(V tValue) {
return tValue;
}
}
class Ob2 {
enum : Column {
COLUM_A = Column()
}
immutable COLUMNS = [
COLUM_A,
];
}
immutable test1 = Column();
void main(string[] args)
{
string where = test1 = "something"; // works ok
std.stdio.writeln(where);
where = (Ob2.COLUM_A = "something else"); // works 2.060, dont compile on 2.061
std.stdio.writeln(where);
}
However I find out more interesing thing. This code is almost same however it doesn`t work on 2.061 neither 2.060:
module main;
import std.algorithm;
import std.array;
immutable struct Column {
string opAssign(V)(V tValue) {
return tValue;
}
}
class Ob2 {
enum : Column {
COLUM_A = Column()
}
// comment this code make it not compilable in 2.060
/*immutable COLUMNS = [
COLUM_A,
];*/
}
immutable test1 = Column();
void main(string[] args)
{
string where = test1 = "something"; // works ok
std.stdio.writeln(where);
where = (Ob2.COLUM_A = "something else"); // dont compile on 2.060 and 2.061
std.stdio.writeln(where);
}
Comment #3 by k.hara.pg — 2013-01-10T22:52:17Z
(In reply to comment #2)
> Yes my fault, I try to simplify too much. Here is more detailed description
>
> Code which works on 2.060 and don`t compile on 2.061
>
[snip]
The reason why (Ob2.COLUM_A = "something else") doesn't work in 2.061 is same as in comment #1. typeof(Ob2.COLUM_A) is a mutable Column, and cannot call immutable opAssign from that.
> However I find out more interesing thing. This code is almost same however it
> doesn`t work on 2.061 neither 2.060:
>
[snip]
I think this _was_ an accepts-invalid bug in 2.060.
If you define Ob2.COLUMNS in 2.060, Ob2.COLUM_A is _incorrectly_ typed as immutable(Column). This is definitely a bug (But I don't know what change is fixed the bug in 2.060).
So, this is not a regression.
Comment #4 by k.hara.pg — 2013-01-10T23:16:33Z
(In reply to comment #3)
> I think this _was_ an accepts-invalid bug in 2.060.
> If you define Ob2.COLUMNS in 2.060, Ob2.COLUM_A is _incorrectly_ typed as
> immutable(Column). This is definitely a bug (But I don't know what change is
> fixed the bug in 2.060).
I found a commit which the behavior is changed.
https://github.com/D-Programming-Language/dmd/commit/e01eb59f842dfe7a5275d96c420691c4a64f57f4
The root cause of the bug 5779 was an optimizer bug.
If the type of an optimized result is different from the type of a source expression, the source expression type had been accidentally modified.
In this case, the declaration of COLUMNS invokes optimizer on the expression [ COLUM_A, ], and it accidentally modified the type of COLUM_A to immutable(Column).
So the conclusion is: the original code had an accepts-invalid bug, and it was already fixed in 2.061.
I'll mark this as "resolved-invalid" bug.