On Thursday, 23 August 2018 at 08:48:15 UTC, Walter Bright wrote:
Being a template doesn't make any difference, except that it
will helpfully infer these things.
Including the `return` attribute.
Also, since 'this' is passed by 'ref' to struct member
functions, it cannot escape anyway with dip1000:
struct S {
int x;
@safe ref int foo() { return x; }
}
dmd test -dip1000
test.d(4): Error: returning this.x escapes a reference to
parameter this, perhaps annotate with return
If we add the `return` attribute, we can escape a pointer to
`this`. And since attributes are often inferred, it might come as
a surprise to the programmer.
However, as far as I understand DIP 1000, it should make sure
that the returned pointer won't outlive the struct instance. And
I guess that's why it's ok to infer the `return` attribute?
That depends on DMD not over-estimating the lifetimes of things,
though. Unfortunately, it does just that. When in doubt, DMD goes
with infinite (e.g., `malloc`). I think that's a problem.
But I've already demonstrated my ignorance regarding DIP 1000 in
a long, not very fruitful discussion with Atila on Bugzilla [1],
so maybe I just don't get it.
[1] https://issues.dlang.org/show_bug.cgi?id=19183