pointer escaping return scope bug?
ag0aep6g
anonymous at example.com
Sat Nov 19 14:52:23 UTC 2022
On 19.11.22 15:07, Nick Treleaven wrote:
> Hi,
> The following seems like a bug to me (reduced code, FILE* changed to int*):
> ```d
> @safe:
>
> struct LockedFile
> {
> private int* fps;
>
> auto fp() return scope => fps;
> }
>
> void main()
> {
> int* p;
> {
> auto lf = LockedFile(new int);
> p = lf.fp;
> }
> assert(p != null); // address escaped
> }
> ```
> There's no error with -dip1000.
> I'll file this unless I overlooked something.
Let me rewrite `fp` as a static method. It's easier (for me) to
understand what's going on that way:
----
static int* fp(return scope ref LockedFile that)
{
return that.fps;
}
----
That's essentially just a function that returns its pointer parameter.
So the program boils down to this:
----
@safe:
int* fp(return scope int* p) { return p; }
void main()
{
int* p;
{
auto lf = new int;
p = fp(lf);
}
assert(p != null); // address escaped
}
----
Which is fine, as far as I can tell. `lf` is not `scope`. And when you
pass it through `fp`, the result is still not `scope`. So escaping it is
allowed.
You do get an error when you make `lf` `scope` (explicitly or
implicitly). So everything seems to be in order.
More information about the Digitalmars-d-learn
mailing list