Skip to content

ILVerify incorrectly complains about use of initonly field outside of constructor+ #94090

Closed

Description

@Zastai

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

Activity

  1. ghost added
    needs-area-label
    on Oct 27, 2023
  2. ghost added
    untriaged
    on Oct 27, 2023
  3. added
    area-Tools-ILVerification
    on Oct 27, 2023
  4. added
    help wanted
    and removed
    needs-area-label
    on Oct 27, 2023
  5. added this to the Future milestone on Jan 13, 2024
  6. ghost removed
    untriaged
    on Jan 13, 2024
  7. added a commit that references this issue on Feb 3, 2025
    56e9f76
  8. added
    in-pr
    on Feb 3, 2025
  9. added a commit that references this issue on Feb 3, 2025
    4771f78
  10. added a commit that references this issue on Oct 18, 2026
    b22bfee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILVerificationhelp wantedin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions