www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22796] New: dual-context recursive functions seem to be broken

https://issues.dlang.org/show_bug.cgi?id=22796

          Issue ID: 22796
           Summary: dual-context recursive functions seem to be broken
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: jephthah_divots aleeas.com

-------------------
struct T {
    void rec (alias fun) (uint n) {
        if (n == 0)
            return;
        fun();
        rec!fun(n - 1);
    }
}

int *b_ptr;

void main () {
    T a;
    int b;
    b_ptr = &b;

    void someDelegate () {
        assert(&b is b_ptr);
    }

    a.rec!someDelegate(4);
}
-------------------
This code compiles with the warning that:
------
weird.d(2): Deprecation: function `weird.main.rec!(someDelegate).rec` function
requires a dual-context, which is deprecated
weird.d(21):        instantiated from here: `rec!(someDelegate)`
------
And when run, it crashes the second time that `T.rec` is called.

I did not experience this behaviour when I moved the .rec method to global
scope. More interestingly, if we had 2 methods calling eachother, the program
doesn't crash as well:
-------------------
struct T {
    // These 2 don't seem to crash
    void rec1 (alias fun) (uint n) {
        if (n == 0)
            return;
        fun();
        rec2!fun(n - 1);
    }

    void rec2 (alias fun) (uint n) {
        if (n == 0)
            return;
        fun();
        rec1!fun(n - 1);
    }
}
-------------------

`dmd --version` output:
------
DMD64 D Compiler v2.096.1

Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
written by Walter Bright
------

--
Feb 20 2022