Thread overview
[Issue 22854] static foreach byCodepoint segfault (2.099-rc.1)
Mar 07, 2022
Dennis
Mar 07, 2022
FeepingCreature
Dec 20, 2022
Iain Buclaw
Dec 20, 2022
Iain Buclaw
Dec 20, 2022
Dlang Bot
Dec 21, 2022
Dlang Bot
Jan 01, 2023
Dlang Bot
March 07, 2022
https://issues.dlang.org/show_bug.cgi?id=22854

Dennis <dkorpel@live.nl> changed:

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

--- Comment #1 from Dennis <dkorpel@live.nl> ---
digger bisect:

```
digger: 8be5c8b0111b68ca23e23c36a6fd1cc746a9c0cc is the first bad commit
commit 8be5c8b0111b68ca23e23c36a6fd1cc746a9c0cc
Author: Iain Buclaw <ibuclaw@gdcproject.org>
Date:   Wed Jun 9 12:34:57 2021 +0200

    dmd: static foreach: Delay running ctfeInterpret until the lowering stage
```

--
March 07, 2022
https://issues.dlang.org/show_bug.cgi?id=22854

--- Comment #2 from FeepingCreature <default_357-line@yahoo.de> ---
Further reduced:

void main() {
    static foreach (ch; SomeContainer().range) { }
}

struct SomeContainer {
    SomeRange range() { return SomeRange(); }
    TypeWithDestructor data;
}

struct TypeWithDestructor { ~this() { } }

struct SomeRange {
    int front() { return 0; }
    bool empty() { return true; }
    void popFront() { }
}

--
December 20, 2022
https://issues.dlang.org/show_bug.cgi?id=22854

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ibuclaw@gdcproject.org

--- Comment #3 from Iain Buclaw <ibuclaw@gdcproject.org> ---
(In reply to FeepingCreature from comment #0)
> From looking at the longer DMD debug mode backtrace on master, there seems to be a stack variable destructor call that reads dead stack memory?
As far as I can tell, possibly an artificial/temporary variable that has not been declared properly.

--
December 20, 2022
https://issues.dlang.org/show_bug.cgi?id=22854

--- Comment #4 from Iain Buclaw <ibuclaw@gdcproject.org> ---
Tweaking the code to use a regular foreach:
---
void main() {
    foreach (ch; SomeContainer().range) { }
}
pragma(msg, main());
---

This is where the temporary gets *pushed* onto the CTFE stack.
---
// If the comma returns a temporary variable, it needs to be an lvalue
// (this is particularly important for struct constructors)
if (e.e1.op == EXP.declaration &&
    e.e2.op == EXP.variable &&
    e.e1.isDeclarationExp().declaration == e.e2.isVarExp().var &&
    e.e2.isVarExp().var.storage_class & STC.ctfe)
{
    VarExp ve = e.e2.isVarExp();
    VarDeclaration v = ve.var.isVarDeclaration();
    ctfeGlobals.stack.push(v);  // <--- here
---

However for `static foreach`, this is never done because of the equality condition (declaration == var) is not true.

This despite both declarationexp and varexp having the same identifier name
(__slSomeCo3).

Obviously, *something* has been copied that really shouldn't have been.

--
December 20, 2022
https://issues.dlang.org/show_bug.cgi?id=22854

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

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

--- Comment #5 from Dlang Bot <dlang-bot@dlang.rocks> ---
@ibuclaw created dlang/dmd pull request #14724 "fix Issue 22854 - [REG 2.099-rc.1] static foreach byCodepoint segfault" fixing this issue:

- fix Issue 22854 - static foreach byCodepoint segfault (2.099-rc.1)

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

--
December 21, 2022
https://issues.dlang.org/show_bug.cgi?id=22854

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #14724 "fix Issue 22854 - [REG  2.099-rc.1] static foreach byCodepoint segfault" was merged into stable:

- 650a3a730abd2d4fa65b126ef4c25f2519470db4 by Iain Buclaw:
  fix Issue 22854 - static foreach byCodepoint segfault (2.099-rc.1)

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

--
January 01, 2023
https://issues.dlang.org/show_bug.cgi?id=22854

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/dmd pull request #14765 "merge stable" was merged into master:

- 77935bf4944c257736a3d0d76be3fdaef54cfb60 by Iain Buclaw:
  fix Issue 22854 - static foreach byCodepoint segfault (2.099-rc.1)

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

--