DIP1000 scope inference

Steven Schveighoffer schveiguy at gmail.com
Tue Oct 25 02:38:02 UTC 2022


On 10/24/22 10:09 PM, Paul Backus wrote:
> On Tuesday, 25 October 2022 at 01:35:28 UTC, Steven Schveighoffer wrote:
>> Does the inferred `scope` make it so that the compiler is *allowed* to 
>> allocate the `[4, 5, 6]` literal on the stack? Keep in mind that I 
>> never put `scope` here, this is something the compiler did on its own.
> 
> No, it does not. This capability was added only for array literals, and 
> only for variable initialization:
> 
> DMD PR: https://github.com/dlang/dmd/pull/14562
> Spec PR (pending): https://github.com/dlang/dlang.org/pull/3442

OK, what about this?

```d
int[] mkarr() @trusted {
     int[3] arr = [1, 2, 3];
     int[] other = [4, 5, 6];

     auto foo = other;
     other = arr[];
     return foo;
}
```

`other` is inferred as `scope` (along with `foo`), because it touches 
`arr[]` later (but after it was pointing at what should have been heap 
memory). So does that count as possible for stack allocation, or is it 
still heap allocated?

-Steve


More information about the Digitalmars-d mailing list