Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
iminashi opened this issue Jul 25, 2024 · 7 comments · Fixed by #17467
Closed

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

iminashi opened this issue Jul 25, 2024 · 7 comments · Fixed by #17467

Comments

@iminashi
Copy link

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
@vzarytovskii
Copy link
Member

vzarytovskii commented Jul 25, 2024

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

@vzarytovskii
Copy link
Member

Method is generated as internal for some reason:
image

@vzarytovskii
Copy link
Member

vzarytovskii commented Jul 25, 2024

@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.

@psfinaki
Copy link
Member

Yes, the above helps (putting InternalsVisibleTo).

@iminashi
Copy link
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.

@psfinaki
Copy link
Member

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

@OkkeHendriks
Copy link

OkkeHendriks commented Sep 5, 2024

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?

KevinRansom added a commit to KevinRansom/fsharp that referenced this issue Sep 24, 2024
… record with private fields (dotnet#17467)

* Fix17447

* tests + readme
vzarytovskii added a commit that referenced this issue Oct 7, 2024
* Update azure-pipelines.yml

* Refactor CE checking (#17464) (#17493)

Co-authored-by: Vlad Zarytovskii <[email protected]>
Co-authored-by: vzarytovskii <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update azure-pipelines.yml

* Localized file check-in by OneLocBuild Task: Build definition ID 499: Build ID 2524346 (#17610)

* Fixes #17447 -MethodAccessException on equality comparison of a record with private fields (#17467)

* Fix17447

* tests + readme

* fixes #17541 - Equals visibility for DU's (#17548)

* update version number

* Switch to new VMR control set (#17703) (port from main) (#17788)

* Now that fsharp is on 9.0, we can switch to the new control set. Generally:
- DotNetBuildFromSource -> DotNetBuildSourceOnly - Building a source-only build.
- DotnetBuildFromSourceFlavor == Product -> DotNetBuildOrchestrator == true - Building in the VMR, could be source-only or MS's build.
- ArcadeBuildFromSource -> DotNetBuildRepo == true -> Indicates an outer repo build.
- ExcludeFromSourceBuild -> ExcludeFromSourceOnlyBuild

* Split out source build args

* Split out source build args

* Remove unnecessary source build env var set

* Add properties to the bootstrap compiler build

* BuildRepo -> BuildInnerRepo

* Only build proto repo in inner build

* Additional VMR properties for completeness

* Rename sourcebuild.props -> dotnetbuild.props

---------

Co-authored-by: Petr <[email protected]>

* respect generic arity in method uniqueness

* [17.12] Turn off realsig when building product and proto (#17808)

* Bugfix : make sure nullness does not break XmlDoc info import for methods and types (#17741)

* Remove nullness signal in string-based type encoding of a symbol (since it is used for xmldoc lookup)

* release notes

* Discard unused values

* Rendering AllowsRefStruct for type parameters (#17706)

* Update azure-pipelines.yml

---------

Co-authored-by: Vlad Zarytovskii <[email protected]>
Co-authored-by: Petr <[email protected]>
Co-authored-by: Tomas Grosup <[email protected]>
Co-authored-by: Kevin Ransom (msft) <[email protected]>
Co-authored-by: vzarytovskii <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kevin Ransom <[email protected]>
Co-authored-by: Matt Mitchell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment