On Borrow Checking
Walter Bright
newshound2 at digitalmars.com
Sat May 3 19:11:48 UTC 2025
On 5/2/2025 12:44 AM, Manu wrote:
> On a slight tangent; why do I need to attribute the function at all for it to
> check `scope` args don't escape?
Because otherwise the compiler has to examine the function implementation to
verify this. Doing such is called attribute inference, and D does quite a bit of
that. Problems are:
1. Function declarations don't have a body, so there's no way to inspect the body.
2. Inspecting all the bodies means compilation gets slowed down quite a bit.
3. Chicken-and-egg problems when inferring attributes when there are loops in
the function call flow graph.
> I do want to add `scope` to pointers and ref arguments, but I don't see any
> reason that I should have to attribute `live`... why isn't `scope` enough to
> enable escape analysis?
The DFA (Data Flow Analysis) required to determine whether a pointer is the
"owner" or not at each point in the function is considerably more complex than
just checking for scope-ness. DFA equations are generated and then solved
iteratively.
The code required to do it is:
https://github.com/dlang/dmd/blob/master/compiler/src/dmd/ob.d
which is very similar to the DFA performed by the optimizer:
https://github.com/dlang/dmd/blob/master/compiler/src/dmd/backend/gflow.d
More information about the Digitalmars-d
mailing list