DIP1000 scope inference proposal

Walter Bright newshound2 at digitalmars.com
Thu Oct 27 00:57:47 UTC 2022


On 10/26/2022 1:03 AM, Walter Bright wrote:
> On 10/24/2022 6:35 PM, Steven Schveighoffer wrote:
>> In a `@trusted` function today, without dip1000, the above is perfectly 
>> reasonable and not invalid. Will dip1000 make it corrupt memory?
> 
> A very good question. Clearly, having code work when it is @safe, but cause 
> memory corruption when it is marked @trusted, is the wrong solution. This should 
> never happen. I'm not sure what the solution should be here.
> 

[Some more thinking about the problem]

The question is when is [1,2,3] allocated on the stack, and when is it allocated 
on the GC heap?

Some points:

1. in C it is allocated on the stack. D's behavior to allocate it on the heap is 
kinda surprising in that light, even though D had such literals before C did

2. allocating on the heap means it is unusable in @nogc code

3. when writing expressions, the only way to get it on the stack is to assign it 
to a scope variable, which is inconvenient and inefficient

4. it runs against the idea that the simpler code should be more efficient than 
the complex code

Therefore, I suggest the following:

     [1,2,3] is always allocated on the stack

     [1,2,3].dup is always allocated on the heap

and thus, its behavior is not dependent on inference.

How we transition to this, we'll have to figure out.


More information about the Digitalmars-d mailing list