Rikki's Borrow Checker

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Sat Aug 29 13:17:10 UTC 2026


On 29/08/2026 9:21 PM, Nick Treleaven wrote:
>      Buffer s;
>      int* c = s.get();
>      call(s, c); // error, owner of active borrow may be mutated
>      borrow(c); // same error, but no owner is passed and parameter is 
> scope

These however should not.

>      s = s.init; // no error
>      destroy(s); // no error

They should error.

```
testdfa\startd.d(24): Error: Cannot mutate the owner of an active borrow
     s = s.init; // error
       ^
testdfa\startd.d(19):        For variable `s`
     Buffer s;
            ^
testdfa\startd.d(20):        Borrowed here
     int* c = s.get();
          ^
testdfa\startd.d(25): Error: Cannot pass the owner of an active borrow 
to a function that may mutate it
     destroy(s); // error
            ^
testdfa\startd.d(25):        Parameter `obj` must be const or immutable
     destroy(s); // error
            ^
testdfa\startd.d(20):        Borrowed here
     int* c = s.get();
          ^
```

Thanks for finding these issues.

I have gotten good progress today but now I'm tired so won't be PR'd today.

https://github.com/rikkimax/dmd/tree/fastdfa-fix-borrowcheck

There is still some regressions to deal with (original output does not 
match current):

```
..\test\fail_compilation\fastdfa.d(1418): Error: Cannot pass the owner 
of an active borrow to a function that may mutate it
..\test\fail_compilation\fastdfa.d(1418):        Parameter `p` must be 
const or immutable
..\test\fail_compilation\fastdfa.d(1417):        Borrowed here
..\test\fail_compilation\fastdfa.d(1418): Error: Cannot mutate the owner 
of an active borrow
..\test\fail_compilation\fastdfa.d(1416):        For variable `x`
..\test\fail_compilation\fastdfa.d(1417):        Borrowed here

fail_compilation/fastdfa.d(1425): Error: Cannot pass the owner of an 
active borrow to a function that may mutate it
fail_compilation/fastdfa.d(1425):        Parameter `p` must be const or 
immutable
fail_compilation/fastdfa.d(1424):        Borrowed here

..\test\fail_compilation\fastdfa.d(1441): Error: Cannot mutate the owner 
of an active borrow
..\test\fail_compilation\fastdfa.d(1439):        For variable `s`
..\test\fail_compilation\fastdfa.d(1440):        Borrowed here
..\test\fail_compilation\fastdfa.d(1441): Error: Cannot mutate the owner 
of an active borrow
..\test\fail_compilation\fastdfa.d(1439):        For variable `s`
..\test\fail_compilation\fastdfa.d(1440):        Borrowed here
```



More information about the Digitalmars-d mailing list