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; } ```
@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
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