[Issue 21884] [betterC] can't compare arrays with -betterC

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Apr 30 20:58:18 UTC 2021


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

--- Comment #3 from Blatnik <blatblatnik at gmail.com> ---
Here's a "fixed" version of core.internal.array.equality.__equals that acts
correctly in betterC. I'm sure it could be done better but at least it works.

bool __equals(T1, T2)(scope T1[] lhs, scope T2[] rhs)
if (!__traits(isScalar, T1) || !__traits(isScalar, T2))
{
    if (lhs.length != rhs.length)
        return false;

    if (lhs.length == 0)
        return true;

    static if (useMemcmp!(T1, T2))
    {
        if (!__ctfe)
        {
            static bool trustedMemcmp(scope T1[] lhs, scope T2[] rhs) @trusted
@nogc nothrow pure
            {
                pragma(inline, true);
                import core.stdc.string : memcmp;
                return memcmp(cast(void*) lhs.ptr, cast(void*) rhs.ptr,
lhs.length * T1.sizeof) == 0;
            }
            return trustedMemcmp(lhs, rhs);
        }
    }

    foreach (const i; 0 .. lhs.length)
    {
        static if (__traits(isStaticArray, at(lhs, 0))) // "Fix" for -betterC
        {
            if (at(lhs, i)[] != at(rhs, i)[]) // T1[N] != T2[N] doesn't compile
with -betterC.
                return false;
        }
        else
        {
            if (at(lhs, i) != at(rhs, i))
                return false;
        }
    }
    return true;
}

--


More information about the Digitalmars-d-bugs mailing list