Thread overview
[Issue 21884] [betterC] can't compare arrays with -betterC
Apr 30, 2021
Blatnik
Apr 30, 2021
Blatnik
Apr 30, 2021
Blatnik
Jun 04, 2022
Walter Bright
Dec 17, 2022
Iain Buclaw
April 30, 2021
https://issues.dlang.org/show_bug.cgi?id=21884

--- Comment #1 from Blatnik <blatblatnik@gmail.com> ---
It's also important to point out that fixing the first issue (type[N] == type[N]) would also fix the second issue.

The only reason type[N][] == type[N][] doesn't work, is because it internally tries to compare every element in the two slices, and every element just happens to be a type[N].

So if type[N] == type[N] is fixed, both types of notation would work.

--
April 30, 2021
https://issues.dlang.org/show_bug.cgi?id=21884

--- Comment #2 from Blatnik <blatblatnik@gmail.com> ---
It also seems like the fixed size array comparisons (type[N] == type[N]) are
implemented internally in DMD. However slice comparisons (type[N][] ==
type[N][]) are implemented in the runtime.

The compiler converts

float[2][2] a;
bool b = a[] == b[];

into:

float[2][2] a;
bool b = core.internal.array.equality.__equals!(float[2], float[2])(a[], a[]);

And this __equals template is something we can change very easily without digging through the compiler.

The offending lines in this function effectively try to do:

foreach(i; 0 .. a.length)
  if (a[i] != a[i])
    return false;
return true;

But a[i] is a float[2], which can't be compared in -betterC..

If we had a special case for arrays that did this instead

foreach(i; 0 .. a.length)
  if (a[i][] != a[i][])
    return false;
return true;

Then at least type[N][] == type[N][] would work, until a better fix was found for type[N] == type[N].

--
April 30, 2021
https://issues.dlang.org/show_bug.cgi?id=21884

--- Comment #3 from Blatnik <blatblatnik@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;
}

--
June 04, 2022
https://issues.dlang.org/show_bug.cgi?id=21884

Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |betterC
                 CC|                            |bugzilla@digitalmars.com

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

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

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

--- Comment #4 from dlangBugzillaToGithub <robert.schadek@posteo.de> ---
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19915

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB

--