Thread overview
[Issue 24634] Parse error initializing array from expression with StructInitializer
[Issue 24634] CTFE on static arrays with StructInitializers fails to compile
Jun 27
basile-z
Jun 27
basile-z
Jun 27
Tom Kral
Jun 29
Dlang Bot
Jun 29
Dlang Bot
June 27
https://issues.dlang.org/show_bug.cgi?id=24634

basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|CTFE                        |
             Status|NEW                         |RESOLVED
           Hardware|x86                         |All
         Resolution|---                         |INVALID
                 OS|Mac OS X                    |All

--- Comment #1 from basile-z <b2.temp@gmx.com> ---
That's not really a bug. The problem is that StructInitializer is not supported when the variable initializer is an AssignExpression. You can verify that in the specifications: https://dlang.org/spec/declaration.html#initialization:

```
auto A[1] as = [{0}];
```

takes the path of ArrayLiteral. ArrayLiteral supports StructInitializer.

```
auto A[1] as = [{0}].update();
```

takes the path of AssignExpression. AssignExpression dont support StructInitializer.

As a workaround you can decompose

```
auto A[1] as0 = [{0}];
auto A[1] as1 = [{0}].update();
```

--
June 27
https://issues.dlang.org/show_bug.cgi?id=24634

--- Comment #2 from basile-z <b2.temp@gmx.com> ---
Sorry I meant to write

```
auto A[1] as0 = [{0}];
auto A[1] as1 = as0.update();
```

--
June 27
https://issues.dlang.org/show_bug.cgi?id=24634

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |nick@geany.org
         Resolution|INVALID                     |---

--- Comment #3 from Nick Treleaven <nick@geany.org> ---
> AssignExpression dont support StructInitializer

No - AssignExpression can be a PostfixExpression, which can have a PrimaryExpression on the left, which can be an ArrayLiteral, which can have a StructInitializer element.

--
June 27
https://issues.dlang.org/show_bug.cgi?id=24634

--- Comment #4 from Tom Kral <mail@tomaskral.sk> ---
(In reply to basile-z from comment #2)
> Sorry I meant to write
> 
> ```
> auto A[1] as0 = [{0}];
> auto A[1] as1 = as0.update();
> ```

Hmm, I find it rather surprising that StructInitializer would not work in this case. Is there a good reason for that?

Thanks for the workaround by the way, it did the trick.

--
June 29
https://issues.dlang.org/show_bug.cgi?id=24634

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|CTFE on static arrays with  |Parse error initializing
                   |StructInitializers fails to |array from expression with
                   |compile                     |StructInitializer

--
June 29
https://issues.dlang.org/show_bug.cgi?id=24634

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@ntrel created dlang/dlang.org pull request #3864 "[spec] Restore *ArrayInitializer*" fixing this issue:

- [spec] Restore *ArrayInitializer*

  It was merged with ArrayLiteral in #3215 (oops).

  Fixes Bugzilla 24634 - Parse error initializing array from expression with
StructInitializer.

  Add examples.

https://github.com/dlang/dlang.org/pull/3864

--
June 29
https://issues.dlang.org/show_bug.cgi?id=24634

--- Comment #6 from Nick Treleaven <nick@geany.org> ---
basile-z is right that the compiler can't parse a struct initializer as part of an expression. The ArrayLiteral spec was wrong - the pull fixes that.

Tom Kral:
> I find it rather surprising that StructInitializer would not work in this case. Is there a good reason for that?

It is part of a function argument, which in turn is part of an expression initializer (because a function call is an expression). An expression cannot parse all initializers because some initializers (e.g. `{}` for struct) mean something else as an expression (a function literal). So initializers are not accepted inside an expression.

--
June 29
https://issues.dlang.org/show_bug.cgi?id=24634

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dlang.org pull request #3864 "[spec] Restore *ArrayInitializer*" was merged into master:

- 4700925acae3df03c6e5399f81df47927240be6a by Nick Treleaven:
  [spec] Restore *ArrayInitializer*

  It was merged with ArrayLiteral in #3215 (oops).

  Fixes Bugzilla 24634 - Parse error initializing array from expression with
StructInitializer.

  Add examples.

https://github.com/dlang/dlang.org/pull/3864

--