D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11292 - Cannot re-initialize a const field in postblit
Summary: Cannot re-initialize a const field in postblit
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-10-18 05:54 UTC by Max Samukha
Modified: 2022-04-12 12:09 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Max Samukha 2013-10-18 05:54:29 UTC
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 Kenji Hara 2013-10-21 21:11:33 UTC
(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 Max Samukha 2020-05-26 08:22:47 UTC
Seems to work with the copy constructor. Postblit is still broken.
Comment 3 Mathias LANG 2020-05-26 15:05:35 UTC
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 Dlang Bot 2020-05-27 11:14:12 UTC
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 Dlang Bot 2020-11-11 19:00:51 UTC
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 RazvanN 2022-04-12 12:09:43 UTC
This is an inherent design flaw of the postblit. This is not going to get fixed. Use copy constructor instead.