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

Documentation for field accessors is incorrect #11059

Closed
JakeSays opened this issue Mar 8, 2025 · 5 comments
Closed

Documentation for field accessors is incorrect #11059

JakeSays opened this issue Mar 8, 2025 · 5 comments

Comments

@JakeSays
Copy link

JakeSays commented Mar 8, 2025

Type of issue

Code doesn't work

Description

The document states that ref is optional for read-only field accessors. This appears to be incorrect, as an BadImageException is thrown with "Invalid usage of UnsafeAccessorAttribute" if ref is not specified.

This line in the documentation should be changed:
"The return value for an accessor to a field can be ref if setting of the field is desired."
to indicate that ref is always required for both read-only and read-write access.

Given this class:

public class Foo
{
    private string _field;
}

According to the documentation as written this is valid:

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_field")]
static extern string GetField(Foo foo);

When in fact it throws the above mentioned exception.
This code runs correctly:

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_field")]
static extern ref string GetField(Foo foo);

Page URL

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafeaccessorattribute?view=net-9.0

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Runtime.CompilerServices/UnsafeAccessorAttribute.xml

Document Version Independent Id

d8b3e8e8-a5e1-1884-99f2-678cf6a77a45

Platform Id

cbce44e6-89f0-7d37-b0fd-cb328dfbdd90

Article author

@dotnet-bot

Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 8, 2025
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 8, 2025
@gewarren gewarren added area-System.Runtime.CompilerServices and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 14, 2025
@michaelgsharp
Copy link
Member

Thanks for finding this. Based on the code comments you are 100% correct.

// The return type must be byref.
// If the non-static field access is for a
// value type, the instance must be byref.

@ericstj
Copy link
Member

ericstj commented Mar 24, 2025

Appears to be addressed with #10268

@ericstj ericstj added in-pr and removed untriaged New issue has not been triaged by the area owner labels Mar 24, 2025
@michaelgsharp
Copy link
Member

Resolved by #10268

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants