On Borrow Checking
Walter Bright
newshound2 at digitalmars.com
Mon May 12 19:28:10 UTC 2025
On 5/12/2025 6:25 AM, Dennis wrote:
> Finally, mechanical scope checking has false positives:
> ```D
> void f(ref scope S ctx)
> {
> const old = ctx;
> g(ctx);
> ctx = old; // Error: scope variable `old` assigned to `ref` variable `ctx`
> with longer lifetime
>
> }
> ```
This comes about because `old`, being declared after `ctx`, has a shorter
lifetime than `ctx`. If `old` has a destructor, that destructor will run before
`ctx`s destructor, and `ctx` will be referring to an invalid instance of `old`.
If S does not have a destructor, does that make the error a false positive or
not? I decided that the existence of a destructor should not change the scoping
rules. The increase in language complexity is not worth it, as such to often
leads to unexpected, quirky side effects.
BTW, thank you for your post. It's informative and excellent.
More information about the Digitalmars-d
mailing list