Bug 6588 – Struct d'tors + immutable elements doesn't work

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-09-01T06:36:00Z
Last change time
2019-12-19T16:20:03Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
David Simcha
Blocks
6587

Comments

Comment #0 by dsimcha — 2011-09-01T06:36:00Z
struct Task(Args...) { Args _args; this(Args args) { _args = args; } ~this() {} // Bug goes away without this. } alias Task!(int function(immutable int), immutable(int)) F; Error: can only initialize const member __args_field_1 inside constructor Error: this is not mutable
Comment #1 by k.hara.pg — 2011-09-03T07:03:19Z
This is built-in opAssign issue. When struct has only dtor, built-in opAssign is implemented automatically by compiler with memberwise assign. struct Task(Args...) { Args _args; this(Args args) { _args = args; } ~this() {} // Bug goes away without this. // Inserted automatically by compiler. // This code does not have file positions, so error message displays // no position. ref typeof(this) opAssign(typeof(this) rhs) { this._args = rhs.args; // immutable field assign outside constructor causes error return this; } }
Comment #2 by lovelydear — 2012-04-27T14:18:26Z
Compiles on 2.059 Win32
Comment #3 by Marco.Leise — 2012-11-10T17:04:42Z
(In reply to comment #1) Thanks I think in my case I can then implement opAssign to skip the assignment of immutable fields. Here is a real short test case without templates: struct Bug6588 { immutable int x; ~this() {} } Also this is related: struct Bug6588 { immutable int x; ref Bug6588 test() { return this; } // this is not mutable ref const(Bug6588) test() { return this; } // ok } The compiler has a hard time with partial const/immutable structures.
Comment #4 by razvan.nitu1305 — 2019-12-19T16:20:03Z
I cannot reproduce this bug. Closing as WORKSFORME