[Issue 20183] New: Returning address of return ref parameter with temporary struct field
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Aug 30 15:47:27 UTC 2019
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 at puremagic.com
Reporter: nick at 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.
--
More information about the Digitalmars-d-bugs
mailing list