Bug 11292 – Cannot re-initialize a const field in postblit
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-18T05:54:29Z
Last change time
2022-04-12T12:09:43Z
Keywords
rejects-valid
Assigned to
No Owner
Creator
Max Samukha
Comments
Comment #0 by samukha — 2013-10-18T05:54:29Z
struct S
{
immutable int x;
this(int x)
{
this.x = x;
}
this(this) {
x = 1;
}
}
void main() {
S s = S(1);
S s2 = s;
}
Error: can only initialize const member x inside constructor
Postblit is a constructor. Why re-initialization is allowed for the state copied from .init and not for the state copied from the source struct?
Comment #1 by k.hara.pg — 2013-10-21T21:11:33Z
(In reply to comment #0)
> struct S
> {
> immutable int x;
>
> this(int x)
> {
> this.x = x;
> }
>
> this(this) {
> x = 1;
> }
> }
>
> void main() {
> S s = S(1);
> S s2 = s;
> }
>
> Error: can only initialize const member x inside constructor
>
> Postblit is a constructor. Why re-initialization is allowed for the state
> copied from .init and not for the state copied from the source struct?
This is definitely a compiler bug.
Comment #2 by maxsamukha — 2020-05-26T08:22:47Z
Seems to work with the copy constructor. Postblit is still broken.
Comment #3 by pro.mathias.lang — 2020-05-26T15:05:35Z
Postblit will go the way of the dodo (extinct). Once the deprecation is in master, we can close postblit-related bugs as WONTFIX.
Comment #4 by dlang-bot — 2020-05-27T11:14:12Z
dlang/dmd pull request #11190 "Fix Issue 11292 - Cannot re-initialize a const field in postblit" was merged into master:
- 68275da1a9785ad0bf15782da6502948171271bd by Martin Kinkelin:
Fix Issue 11292 - Cannot re-initialize a const field in postblit
https://github.com/dlang/dmd/pull/11190
Comment #5 by dlang-bot — 2020-11-11T19:00:51Z
dlang/dmd pull request #11921 "Revert "Fix Issue 11292 - Cannot re-initialize a const field in postblit"" was merged into master:
- 417649e0e6624b14933387273d13ba0283fe6c08 by Iain Buclaw:
Revert "Fix Issue 11292 - Cannot re-initialize a const field in postblit"
https://github.com/dlang/dmd/pull/11921
Comment #6 by razvan.nitu1305 — 2022-04-12T12:09:43Z
This is an inherent design flaw of the postblit. This is not going to get fixed. Use copy constructor instead.