Comapring two structs having arrays throws InvalidProgramException (“Invalid IL code”) at runtime
dahlia opened this issue · comments
If a struct having array properties comparing two values of that type throws InvalidProgramException
at runtime. The minimum reproducible code is here:
using System;
[Equals]
public struct FooBar {
public FooBar(int[] foo, int[] bar) {
Foo = foo;
Bar = bar;
}
public int[] Foo { get; set; }
public int[] Bar { get; set; }
}
public class Program {
static void Main(string[] args) {
var a = new FooBar(new[] { 1, 2 }, new[] { 3, 4 });
var b = new FooBar(new[] { 1, 2 }, new[] { 3, 4 });
if (a.Equals(b))
{
Console.WriteLine("They equal.");
}
}
}
This program crashes with an unhandled exception:
Unhandled Exception:
System.InvalidProgramException: Invalid IL code in FooBar:EqualsInternal (FooBar,FooBar): IL_0002: bne.un.s IL_0010
at FooBar.Equals (System.Object obj) [0x0003a] in <9c04883983e745dcaee8439101d7e69a>:0
at Program.Main (System.String[] args) [0x00047] in <9c04883983e745dcaee8439101d7e69a>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidProgramException: Invalid IL code in FooBar:EqualsInternal (FooBar,FooBar): IL_0002: bne.un.s IL_0010
at FooBar.Equals (System.Object obj) [0x0003a] in <9c04883983e745dcaee8439101d7e69a>:0
at Program.Main (System.String[] args) [0x00047] in <9c04883983e745dcaee8439101d7e69a>:0
On the other hand, this runtime exception does not occur if it's not a struct
but a class
.
I ran the above program on Mono 5.20.1.19 & macOS 10.14.2.
can u try 1.9.6
Thanks! It now works well.
The version 1.9.6 became to depend on Fody 5.0.0, which dropped MSBuild 15 support. So this version of Fody.Equals cannot be used with .NET Core 2.2, which sticks with MSBuild 15. On the other hand, Fody.Equals 1.9.5 works well with .NET Core 2.2. Is depending on Fody 5.0.0 intended?
So this version of Fody.Equals cannot be used with .NET Core 2.2, which sticks with MSBuild 15
i dont understand this statement? .NET Core 2.2 works fine with MSBuild 16
Indeed, .NET Core 2.2.20x versions bundle MSBuild 16. I hadn't been aware that. However, there are apparently no .NET Core 2.2 version bundling MSBuild 16 and working with Visual Studio 2017. (See also The Visual Studio / .NET Core SDK support matrix
on this issue.) Note that there is no .NET Core version higher than 2.2.106 (this also bundles MSBuild 15) for macOS either.
Or is there any way to make dotnet
command to use other MSBuild than the version it bundles?
I tested on VS for mac. You should be able to target netcore 2.1 in your project. Is this not the case?
I tried the dotnet build
command on macOS, and it gave me the following result:
$ dotnet build
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restoring packages for ~/sample/SampleApp.csproj...
Generating MSBuild file ~/sample/obj/SampleApp.csproj.nuget.g.props.
Restore completed in 198.91 ms for ~/sample/SampleApp.csproj.
~/.nuget/packages/fody/5.0.0/build/Fody.targets(37,5): error : Fody is only supported on MSBuild 16 and above. Current version: 15. [~/sample/uno-repro/SampleApp.csproj]
Build FAILED.
~/.nuget/packages/fody/5.0.0/build/Fody.targets(37,5): error : Fody is only supported on MSBuild 16 and above. Current version: 15. [~/sample/SampleApp.csproj]
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:01.93
The tested code is here. My .NET Core installation is like the following:
$ dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.2.106
Commit: aa79b139a8
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.14
OS Platform: Darwin
RID: osx.10.14-x64
Base Path: /usr/local/share/dotnet/sdk/2.2.106/
Host (useful for support):
Version: 2.2.4
Commit: f95848e524
.NET Core SDKs installed:
2.2.106 [/usr/local/share/dotnet/sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.2.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
Can u try 5.0.1 beta2 of fody
Fody 5.0.1-beta.2 with Equals.Fody 1.9.6 works well with MSBuild 15! Thanks!
@dahlia i have deployed 5.0.1 stable, sorry for the hassle
Thanks for your immediate answer! 🙏🏼