digitalmars.D.bugs - [Issue 20183] New: Returning address of return ref parameter with
- d-bugmail puremagic.com (36/36) Aug 30 2019 https://issues.dlang.org/show_bug.cgi?id=20183
https://issues.dlang.org/show_bug.cgi?id=20183 Issue ID: 20183 Summary: Returning address of return ref parameter with temporary struct field Product: D Version: D2 Hardware: x86 OS: Windows Status: NEW Severity: normal Priority: P1 Component: dmd Assignee: nobody puremagic.com Reporter: nick geany.org With dmd v2.088.0-rc.1 (and earlier): safe: int* addr(return ref int b) { return &b; } struct S { int i; } auto s() {return S();} void main(){ //auto p = addr(S().i); // cannot modify constant expression `S(0).i` auto p = addr(s().i); (*p)++; } Here the problem is that p outlives the int passed to addr. I think `return &b` in addr should not be allowed, because the returned pointer can live longer than the argument passed to the `ref int b` parameter. Allowing &b to be returned also makes enhancing D to support safe smart containers harder than it needs to be, requiring flow analysis - any ref can be turned into a scope pointer which lives longer. --
Aug 30 2019