Bug 10980 – static initialization of immutable structs with disabled postblit fails
Status
RESOLVED
Resolution
FIXED
Severity
blocker
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-09-06T08:08:00Z
Last change time
2013-09-09T14:11:27Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2013-09-06T08:08:48Z
Let S be a struct a disabled potsblit, and we want to create a static instance of an immutable S:
//----
alias immutable(S) IS;
static IS i = IS.init;
//----
2.060: OK!
2.061: Error: variablei of type struct immutable(S) uses this(this), which is not allowed in static initialization
2.062: Error: variable hello.i of type struct immutable(S) uses this(this), which is not allowed in static initialization
2.063: OK!
2.063.2: OK!
So... it broke and it was fixed (apparently).
BUT!
Let's try that in a template now:
//----
template foo(T)
{
static T i = T.init;
alias i foo;
}
struct S
{
@disable this(this);
}
alias immutable(S) IS;
//IS i = IS.init; //Uncoment me
void main()
{
auto p = &foo!IS;
}
//----
2.060: OK!
2.061: Error: variable main.i of type struct immutable(S) uses this(this), which is not allowed in static initialization
2.062: Error: variable main.i of type struct immutable(S) uses this(this), which is not allowed in static initialization
2.063: Error: template instance main.foo!(immutable(S)) error instantiating
2.063.2: Error: template instance main.foo!(immutable(S)) error instantiating
Now, we simply have an "unclear" error that the template failed, but with no diagnostics (at least, I am unable to provide any).
Funny story: if we uncomment the global static, it works again. Another workaround is to declare the static inside a function, and have the function return the static by reference.
This issue is also "semi-blocker" in emplace, which needs to create a static instance of T, no matter T's type.
Comment #1 by k.hara.pg — 2013-09-08T23:34:36Z
(In reply to comment #0)
> Let S be a struct a disabled potsblit, and we want to create a static instance
> of an immutable S:
>
> //----
> alias immutable(S) IS;
> static IS i = IS.init;
> //----
>
> 2.060: OK!
> 2.061: Error: variablei of type struct immutable(S) uses this(this), which is
> not allowed in static initialization
> 2.062: Error: variable hello.i of type struct immutable(S) uses this(this),
> which is not allowed in static initialization
> 2.063: OK!
> 2.063.2: OK!
>
> So... it broke and it was fixed (apparently).
The issue had not been fixed, instead it just be hidden by the regression bug 10998, from 2.063.
> BUT!
> Let's try that in a template now:
>
> //----
> template foo(T)
> {
> static T i = T.init;
> alias i foo;
> }
>
> struct S
> {
> @disable this(this);
> }
> alias immutable(S) IS;
>
> //IS i = IS.init; //Uncoment me
>
> void main()
> {
> auto p = &foo!IS;
> }
> //----
> 2.060: OK!
> 2.061: Error: variable main.i of type struct immutable(S) uses this(this),
> which is not allowed in static initialization
> 2.062: Error: variable main.i of type struct immutable(S) uses this(this),
> which is not allowed in static initialization
> 2.063: Error: template instance main.foo!(immutable(S)) error instantiating
> 2.063.2: Error: template instance main.foo!(immutable(S)) error instantiating
>
> Now, we simply have an "unclear" error that the template failed, but with no
> diagnostics (at least, I am unable to provide any).
>
> Funny story: if we uncomment the global static, it works again. Another
> workaround is to declare the static inside a function, and have the function
> return the static by reference.
>
> This issue is also "semi-blocker" in emplace, which needs to create a static
> instance of T, no matter T's type.
Because the current compile-time postblit call check is not enough strict. I opened a compiler fix to improve check mechanism.
https://github.com/D-Programming-Language/dmd/pull/2541
Comment #2 by github-bugzilla — 2013-09-09T14:08:31Z