[Issue 21286] [DIP1000] Can't return scope reference from a function

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Nov 18 15:07:14 UTC 2023


https://issues.dlang.org/show_bug.cgi?id=21286

--- Comment #5 from Paul Backus <snarwin+bugzilla at gmail.com> ---
Perhaps more to the point, the bigger problem is that from the outside, the
compiler has no way to distinguish between these two implementations of getRef:

---
@safe:
int global;
ref int* getRef1(return ref int* p) => p;
ref int* getRef2(return ref int* p) => global;
---

Allowing a scope pointer to be assigned to the return value of getRef2 would
clearly be a mistake, so it has to be forbidden in general.

You could maybe justify allowing the assignment if getRef were a pure function,
but giving pure functions special exemptions from the normal safety rules has
bitten us in the past, and it's still just a band-aid over the real problem
(lack of explicit lifetimes), so I wouldn't recommend it.

--


More information about the Digitalmars-d-bugs mailing list