https://issues.dlang.org/show_bug.cgi?id=19097
Issue ID: 19097
Summary: Extend Return Scope Semantics
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: dmd
Assignee: [email protected]
Reporter: [email protected]
Extend return semantics so that a 'return' parameter for void or constructor
functions
applies to the first parameter, if it is a ref or out parameter. For a member
function,
the first parameter is 'this'.
Currently, a 'return' parameter transfers its lifetime to the function return
value:
```
int* frank(return scope int* p) { return p; }
int* p;
int i;
int* q;
q = frank(&i); // ok
p = frank(&i); // error
```
However, the following just issues errors:
```
@safe void betty(ref scope int* r, return scope int* p) { r = p; } // (1) error
int* p;
int i;
int* q;
betty(q, &i); // (2) ok
betty(p, &i); // (3) should be error
```
Marking `betty` as `@trusted` resolves (1), but does not detect the difference
between (2) and (3). Hence, the callers of `betty` would have to be marked
`@trusted` as well.
This situation comes up repeatedly with:
1. constructors
2. property setters
3. put(dest, source) functions
Annotating a parameter with `return` has been quite successful at tracking
scope dependencies from the parameter to the function return value. Extending
this in cases where there is no return value to the first parameter resolves
the issue identified above.
The scope check for:
```
void betty(ref scope int* r, return scope int* p);
betty(q, &i);
```
is as if `q = &i;` was encountered.
This is not new syntax, but new semantics for cases that were compile-time
errors before (i.e. annotating parameters with `return` when there was no
return value).
--