Skip to content

MethodAccessException on equality comparison of a record with private fields #17447

Closed

Description

@iminashi

Equality comparison of a record type with private fields no longer works in SDK 8.0.400 for a type that is in a different assembly.

Repro steps

Assembly A:

Record type with private fields.

module Types

type Value =
    private { value: uint32 }

    static member Zero = { value = 0u }
    static member Create(value: int) = { value = uint value }

Assembly B:

Anything that uses the = operator on the type.

module Test

open NUnit.Framework
open Types

[<Test>]
let Equality () =
    let zero = Value.Create 0

    Assert.That(Value.Zero = zero, "zero equals zero")

Expected behavior

Equality comparison works.

Actual behavior

Runtime exception:

System.MethodAccessException : Attempt by method 'Test.Equality()' to access method 'Types+Value.Equals(Value, System.Collections.IEqualityComparer)' failed.

Known workarounds

  • Remove the private modifier.
  • Replace = with Equals call.

Related information

  • .NET SDK 8.0.400-preview.0.24324.5

Activity

  1. added this to the Backlog milestone on Jul 25, 2024
  2. vzarytovskii commented on Jul 25, 2024

    @vzarytovskii
    Member

    Likely caused by #16857 (or by realsig, or by mixture of both)

  3. vzarytovskii commented on Jul 25, 2024

    @vzarytovskii
    Member

    Method is generated as internal for some reason:
    image

  4. vzarytovskii commented on Jul 25, 2024

    @vzarytovskii
    Member

    @iminashi I suppose itermediate workaroud would be to add IVT attribute to the first assembly (the one which test is using), until we implement a fix.

  5. psfinaki commented on Jul 25, 2024

    @psfinaki
    Contributor

    Yes, the above helps (putting InternalsVisibleTo).

  6. iminashi commented on Jul 25, 2024

    @iminashi
    Author

    I was just trying out the VS preview version (to see if it had the fix for #17161) when I ran into this, so this does not really affect me that much for now.

  7. psfinaki commented on Jul 25, 2024

    @psfinaki
    Contributor

    Note, --realsig+ also seems to fix the issue.

  8. added a commit that references this issue on Jul 31, 2024
    44e7bfa
  9. OkkeHendriks commented on Sep 5, 2024

    @OkkeHendriks

    Hello (@KevinRansom), will this be released for .NET 8.0.x? We, automatically, installed 8.0.400 through windows update on our build servers.

    Projects which did not pin the SDK (or allowed feature updates by using "rollForward": "latestFeature" in their global.json) crash, at runtime, on production, if they happen to have an equality comparison in a untested code path.

    It seems to me that this should be a major issue for many users? Or are we doing something out of the ordinary here?

  10. added a commit that references this issue on Sep 24, 2024
    5236b25
  11. added a commit that references this issue on Oct 7, 2024
    c4850d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions