Thread overview
[Issue 21868] DIP1000 doesn't catch pointer to struct temporary
Apr 27
ag0aep6g
Jun 03
Dennis
Jun 03
Dennis
Jun 10
Dlang Bot
Jun 10
Dennis
19 hours ago
Dennis
April 27
https://issues.dlang.org/show_bug.cgi?id=21868

ag0aep6g <ag0aep6g@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid, safe
                 CC|                            |ag0aep6g@gmail.com

--
June 03
https://issues.dlang.org/show_bug.cgi?id=21868

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel@live.nl

--- Comment #1 from Dennis <dkorpel@live.nl> ---
Here's it reduced some more:

```
struct S {
    int buf;
    int* ptr; // <= required
}

int* getPtr(ref return scope S this_) {
    return &this_.buf;
}
```

Since getPtr does not return by `ref`, the `return scope` applies to the
members of `S` and not the `ref this_` itself.
The compiler checks whether `&this_.buf;` is escaped by value.
In `visit(AddrExp e)` of escape.d, it reduces this to checking whether
`this_.x` escapes by ref, which reduces to checking whether `this_` escapes by
ref.
Then it looks at the parameter's storage classes, and sees `return`, so it
allows it.
This is wrong, the `return` does not apply to `&this_.x`, only to `this_.buf`;

Without the `buf` member, S has no pointers, so the `return scope` attribute is
meaningless, so it is stripped away at some earlier stage.
This way the compiler can't mistake it for a `ref return`, so it raises an
error.

I'm still trying to grasp the incredibly difficult logic in escape.d so I'm not sure how to fix this yet.

--
June 03
https://issues.dlang.org/show_bug.cgi?id=21868

--- Comment #2 from Dennis <dkorpel@live.nl> ---
> This is wrong, the `return` does not apply to `&this_.x`, only to `this_.buf`;

I should be careful when renaming variables :) Corrections:

* reduces this to checking whether `this_.buf` escapes by ref
* This is wrong, the `return` does not apply to `&this_.buf`, only to
`this_.ptr`;

--
June 10
https://issues.dlang.org/show_bug.cgi?id=21868

Dlang Bot <dlang-bot@dlang.rocks> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull

--- Comment #3 from Dlang Bot <dlang-bot@dlang.rocks> ---
@dkorpel created dlang/dmd pull request #12665 "Fix issue 21868 - conflation of return-ref and return-scope" fixing this issue:

- fix issue 21868 - conflation of return-ref and return-scope

https://github.com/dlang/dmd/pull/12665

--
June 10
https://issues.dlang.org/show_bug.cgi?id=21868

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |per.nordlow@gmail.com

--- Comment #4 from Dennis <dkorpel@live.nl> ---
*** Issue 18792 has been marked as a duplicate of this issue. ***

--
19 hours ago
https://issues.dlang.org/show_bug.cgi?id=21868

Dennis <dkorpel@live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=22040

--