yanghuan / CSharp.lua

The C# to Lua compiler
Other
1.23k stars 202 forks source link

KeyValuePair does not implement GetHashCode (error about "user-defined struct") #476

Closed joelverhagen closed 9 months ago

joelverhagen commented 10 months ago

I think this is confusing because from the user's perspective (the user of CSharp.Lua) a KVP is not user-defined.

Repro:

internal class Program
{
    private static void Main(string[] args)
    {
        var dictA = new Dictionary<KeyValuePair<int, bool>, string>();
        dictA.Add(KeyValuePair.Create(1, true), "foo");
        var dictB = new Dictionary<KeyValuePair<int, bool>, string>();
        dictB.Add(KeyValuePair.Create(1, true), "baz");
        Console.WriteLine(dictA.Except(dictB).Count());
    }
}

Error:

C:\Users\jver\Downloads\lua-5.2.4_Win64_bin\lua52.exe: System.NotSupportedException: System.Collections.Generic.KeyValuePair`2[System.Collections.Generic.KeyValuePair`2[System.Int32,System.Boolean],System.String] User-defined struct not support GetHashCode
stack traceback:
        .\out.lua:10330: in function 'addToSet'
        .\out.lua:10444: in function 'init'
        .\out.lua:9783: in function 'MoveNext'
        .\out.lua:10839: in function 'Count'
        .\out.lua:19606: in function 'Main'
        .\out.lua:19621: in main chunk
        [C]: in function 'require'
        ...arp.lua\CSharp.lua.Launcher\bin\Debug\net7.0\DT\main.lua:1: in main chunk
        [C]: in ?
joelverhagen commented 10 months ago

I started working on this and got struck on behavior I don't understand. Tuple returns a string representation of its content as the hash code. And some base types return non-integers as their hash code (https://github.com/yanghuan/CSharp.lua/issues/477).