Bug 11270 – [REG 2.064] Initialization of struct in constructor
Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-15T02:25:00Z
Last change time
2013-11-06T19:42:34Z
Assigned to
nobody
Creator
code
Comments
Comment #0 by code — 2013-10-15T02:25:27Z
The following code used to work dmd 2.064 beta 1 and all versions before that. It just broke on the latest version of the 2.064 branch on git.
repro.d(13): Error: cannot implicitly convert expression (f) of type float to netvar!float
Although I'm not really sure if this is a bug or if the code is invalid and just used to work because of other bugs.
Repro case:
struct netvar(T) {
T value;
alias value this;
void opAssign(T newVal){
value = newVal;
}
}
void main(string[] args)
{
float f = 2.0f;
netvar!float f2 = f;
}
Comment #1 by k.hara.pg — 2013-10-15T03:59:51Z
(In reply to comment #0)
> The following code used to work dmd 2.064 beta 1 and all versions before that.
> It just broke on the latest version of the 2.064 branch on git.
>
> repro.d(13): Error: cannot implicitly convert expression (f) of type float to
> netvar!float
With 2.057-2.063, same error occurs for the code.
> Although I'm not really sure if this is a bug or if the code is invalid and
> just used to work because of other bugs.
As far as I know, the repro case is invalid. On the variable initialization `netvar!float f2 = f;` opAssign never invoked.
Comment #2 by code — 2013-10-15T04:07:22Z
Sorry, I reduced the repro case to much. Here is the actual repro case I had first:
struct netvar(T) {
T value;
alias value this;
void opAssign(T newVal){
value = newVal;
}
}
class Foo
{
netvar!float f;
this(float f)
{
this.f = f;
}
}
void main(string[] args)
{
new Foo(2.0f);
}
Comment #3 by k.hara.pg — 2013-10-15T07:02:24Z
(In reply to comment #2)
> Sorry, I reduced the repro case to much. Here is the actual repro case I had
> first:
>
> struct netvar(T) {
> T value;
> alias value this;
>
> void opAssign(T newVal){
> value = newVal;
> }
> }
>
> class Foo
> {
> netvar!float f;
>
> this(float f)
> {
> this.f = f;
> }
> }
>
> void main(string[] args)
> {
> new Foo(2.0f);
> }
Ah, this is an expected behavior that has been introduced by fixing bug 9665.
With git-head, compiler tries to rewrite `this.f = f;` to the field initialization.
Possible modifications:
1. Rewrite this.f = f; to this.f = netvar!float(f);
It will initialize the Foo.f by the struct literal.
2. Add a constructor this(T); in netbar struct to accept a T value as a valid
initializer.
Comment #4 by code — 2013-10-15T07:04:49Z
Thanks for the suggestions. I knew how to fix this, but I just wanted to confirm that this is indeed intended behavior and not a regression, because it used to work in all earlier compiler versions.
Comment #5 by k.hara.pg — 2013-10-15T07:21:07Z
(In reply to comment #4)
> Thanks for the suggestions. I knew how to fix this, but I just wanted to
> confirm that this is indeed intended behavior and not a regression, because it
> used to work in all earlier compiler versions.
It was the long standing bug. I'm glad to be able to fix it finally.
Mark this issue as resolved invalid.
Comment #6 by k.hara.pg — 2013-11-06T19:42:34Z
*** Issue 11458 has been marked as a duplicate of this issue. ***