ILVerify incorrectly complains about use of initonly field outside of constructor+
#94.090 aperta il 27 ott 2023
Metriche repository
- Star
- (17.886 star)
- Metriche merge PR
- (Merge medio 12g 11h) (661 PR mergiate in 30 g)
Descrizione
Description
ILVerify is rejecting C# code compiled using the normal compiler from the .NET SDK.
Reproduction Steps
Compile an assembly containing a class like this:
internal class ILVerifyDoesNotLikeThis {
private static readonly DateTimeOffset FixedUtcTime = new(1969, 7, 20, 20, 17, 40, TimeSpan.Zero);
public DateTimeOffset UtcNow => ILVerifyDoesNotLikeThis.FixedUtcTime.ToUniversalTime();
}
Run ILVerify on it.
I doubt it matters, but in my case, the compile used MSBuild version 17.7.3+8ec440e68 for .NET, targeting net6.0 in Debug configuration.
Expected behavior
No errors detected by ILVerify.
Actual behavior
ILVerify reports an error:
[IL]: Error [InitOnly]: [\Path\To\Assembly.dll : Some.Namespace.ILVerifyDoesNotLikeThis::get_UtcNow()][offset 0x00000000] Cannot change initonly field outside its .ctor.
The code does not actually change any fields.
IL for the getter:
.method /* 06001BBB */ public hidebysig specialname
instance valuetype [System.Runtime]System.DateTimeOffset get_UtcNow () cil managed
{
// Method begins at RVA 0xdfcb7
// Header size: 1
// Code size: 11 (0xb)
.maxstack 8
// return ILVerifyDoesNotLikeThis.FixedUtcTime.ToUniversalTime();
// sequence point: (line 1597, col 35) to (line 1597, col 89) in C:\Sources\Anubex\natural-services.dotnet\Anubex.NaturalServices.Tests\DummyObjects\DummyProcessor.cs
IL_0000: ldsflda valuetype [System.Runtime]System.DateTimeOffset Anubex.NaturalServices.Tests.DummyObjects.ILVerifyDoesNotLikeThis::FixedUtcTime /* 040011F2 */
IL_0005: call instance valuetype [System.Runtime]System.DateTimeOffset [System.Runtime]System.DateTimeOffset::ToUniversalTime() /* 0A0000D3 */
IL_000a: ret
} // end of method ILVerifyDoesNotLikeThis::get_UtcNow
(would be nice if GitHub supported IL highlighting, btw)
Looking at #57385 it looks like support for ldflda was added but ldsflda was left out.
Regression?
No response
Known Workarounds
No response
Configuration
ILVerify --version reports 7.0.0-rtm.22518.5+d099f075e45d2aa6007a22b71b45a08758559f80, which should be the current version.
Output of dotnet --info:
.NET SDK:
Version: 7.0.402
Commit: 791db8e2d8
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.402\
Host:
Version: 7.0.12
Architecture: x64
Commit: 4a824ef37c
.NET SDKs installed:
6.0.416 [C:\Program Files\dotnet\sdk]
7.0.304 [C:\Program Files\dotnet\sdk]
7.0.402 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.23 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.24 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
Other information
No response