Bug 22837 – [dip1000] checkConstructorEscape quits after first non-pointer
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-03-02T16:55:03Z
Last change time
2022-03-04T14:39:40Z
Keywords
pull, safe
Assigned to
No Owner
Creator
Dennis
Comments
Comment #0 by dkorpel — 2022-03-02T16:55:03Z
dmd checks escaping pointers of a constructor by fake assigning arguments to `this`, but it stops at the first non pointer argument.
```
bool checkConstructorEscape() {
// ...
/* Attempt to assign each `return` arg to the `this` reference
*/
foreach (const i; 0 .. n)
{
Expression arg = (*ce.arguments)[i];
if (!arg.type.hasPointers())
return false; // < EARLY EXIT
```
This means you can escape scope pointers like this:
```
@safe:
struct S
{
int* p;
this(int dummy, return scope int* p)
{
this.p = p;
}
}
int* escape()
{
int x;
auto s = S(0, &x);
return s.p;
}
```
Comment #1 by dlang-bot — 2022-03-02T16:59:01Z
@dkorpel created dlang/dmd pull request #13744 "Fix issue 22837 - checkConstructorEscape quits after first non-pointer" fixing this issue:
- Fix issue 22837 - checkConstructorEscape quits after first non-pointer
https://github.com/dlang/dmd/pull/13744
Comment #2 by dlang-bot — 2022-03-04T14:39:40Z
dlang/dmd pull request #13744 "Fix issue 22837 - checkConstructorEscape quits after first non-pointer" was merged into master:
- 54ecdf46f47e41999ff4720be5fa9ea9b0c43898 by Dennis Korpel:
Fix issue 22837 - checkConstructorEscape quits after first non-pointer
https://github.com/dlang/dmd/pull/13744