Possible bug when instantiating template function with nested struct

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Sun Nov 7 12:02:49 UTC 2021


On Thursday, 4 November 2021 at 17:28:14 UTC, Teodor Dutu wrote:
> Hi,
>
> As part of my project for SAoC 2021, I am trying to rewrite the 
> hook to `_d_arrayctor` to a template function. This work was 
> started by [Dan Printzell](https://github.com/Vild/) 2 years 
> ago. Dan managed to implement a template version of 
> `_d_arrayctor` in druntime and took some steps to switch to 
> this version in dmd as well:
> - https://github.com/dlang/druntime/pull/2655.
> - https://github.com/dlang/dmd/pull/10102
>
> In essence, Dan performs the following lowering:
> ```d
> T[2] a;
> T[2] b = a;
> // is lowered to:
> T[2] a;
> T[2] b;
> _darrayctor(b[], a[]);
> ```
>
> After picking up Dan's work, I brought [some 
> fixes](https://github.com/dlang/dmd/pull/13116) to his changes 
> to dmd, so that the code now passes the tests in dmd and 
> druntime. However, a few warnings issued by some tests in 
> phobos revealed a flaw in `_d_arrayctor`'s signature. Some time 
> ago, I wrote [a forum 
> post](https://forum.dlang.org/post/simesvkancmscrtsciwq@forum.dlang.org) about this issue.

I've always wondered, whats the problem with representing an 
array through a simple struct?

I've seen multiple comments in the forum where people mentioned 
that an array is basically a struct with one pointer and length. 
So why not have entire array lower to smth like this:

```d
struct Array(T) {
   private T* store;
   private size_t length;

   this(inout ref Array!T array);
   // ... And rest of methods implementing array behavior
}
```

Then compiler would just lower down all T[] expressions to 
Array!T, and then let druntime implement semantics of the array.

This would be much more flexible since you won't need to go 
inside compiler itself to fix bugs related to array behavior, 
unless you need to change the struct interface that the compiler 
potentially will use.

Best regards,
Alexandru.


More information about the Digitalmars-d mailing list