[Issue 16528] @safe inference does not work for mutually recursive functions

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Sep 23 10:21:01 PDT 2016


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

John Colvin <john.loughran.colvin at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |john.loughran.colvin at gmail.
                   |                            |com

--- Comment #1 from John Colvin <john.loughran.colvin at gmail.com> ---
Ok so this is absolutely hideous and there is definitely something weird going
on with the compiler in this region, but I *think* the following works as
expected. There is probably an adaption of this that can be made simpler, but I
don't have time to find it right now.

template fun1Wrap(T)
{
    enum foo = q{
    void impl(T value) %s
    {
        if (value) fun2%s(value - 1);
    }
    };
    import std.format : format;
    static if(__traits(compiles, { mixin(format(foo, `@safe`, `Safe`)); }))
        mixin(format(foo, `@safe`, `Safe`));
    else
        mixin(foo);
}

void fun1Safe(T)(T t) @safe
{
    fun1Wrap!T.impl(t);
}

void fun1(T)(T t)
{
    static if (__traits(compiles, fun1Safe(t) ))
        fun1Safe(t);
    else
        fun1Wrap!T.impl(t);
}

template fun2Wrap(T)
{
    enum foo = q{%s void impl(T value)
{
    if (value) fun1%s(value - 1);
}
};
    import std.format : format;
    static if(__traits(compiles, { mixin(format(foo, `@safe`, `Safe`)); }))
        mixin(format(foo, `@safe`, `Safe`));
    else
        mixin(foo);
}

void fun2Safe(T)(T t) @safe
{
    fun2Wrap!T.impl(t);
}

void fun2(T)(T t)
{
    static if (__traits(compiles, fun1Safe(t) ))
        fun2Safe(t);
    else
        fun2Wrap!T.impl(t);
}

@safe void main()
{
    fun1(4);
}

--


More information about the Digitalmars-d-bugs mailing list