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

Add annotations for ref fields to public API surface #71265

Merged

Conversation

AaronRobinsonMSFT
Copy link
Member

This adds the necessary annotations to facilitate updating to C# 11. They will all be removed and converted to use the scoped keyword once we update to the latest Roslyn with ref field support.

The desire is to also backport this to Preview 6.

/cc @stephentoub @jaredpar @cston

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost
Copy link

ghost commented Jun 24, 2022

Tagging subscribers to this area: @dotnet/area-meta
See info in area-owners.md if you want to be subscribed.

Issue Details

This adds the necessary annotations to facilitate updating to C# 11. They will all be removed and converted to use the scoped keyword once we update to the latest Roslyn with ref field support.

The desire is to also backport this to Preview 6.

/cc @stephentoub @jaredpar @cston

Author: AaronRobinsonMSFT
Assignees: -
Labels:

area-Meta

Milestone: 7.0.0

@AaronRobinsonMSFT
Copy link
Member Author

/backport to release/7.0-preview6

@github-actions
Copy link
Contributor

Started backporting to release/7.0-preview6: https://github.com/dotnet/runtime/actions/runs/2556869823

@@ -671,7 +671,7 @@ public static void Write<T>(void* destination, T value)
// Mono:AsRef
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T AsRef<T>(in T source)
public static ref T AsRef<T>([LifetimeAnnotation(true, false)] in T source)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: this is effectively encoding that AsRef is the single method for stripping all safety from a ref value. It now removes in (changes mutability) and scoped (changes lifetime). I'm 100% okay with this, it's the design I prefer, however IIRC there was some debate on whether we should do this or have a separate method for each type of safety we strip. Wanted to double check that this debate was had and we're not accidentally just making that decision here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas Do you have any thoughts on @jaredpar's recollection?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: don't think it necessarily needs to hold up this merge. Can fix this later. Just didn't want this to be accidentally enshrined as a decision if we hadn't had the proper discussion on it yet

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 100% okay with this, it's the design I prefer,

+1

I recall discussions about fine grained Unsafe. For example, allow unsafe code to express difference between unsafe readonly refs and unsafe writeable refs. We would need to add a number of new methods to Unsafe to the fine grained Unsafe work well end-to-end.

cc @tannergooding @GrabYourPitchforks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main concern on my end is making unsafe code harder to reason about. There are both pros and cons to having a single method here and I think its overall fine for this scenario.

I think the bigger annoyance on my end is actually the IL bloat and readability issues around having to strip in when doing more complex Unsafe logic, even where the underlying value isn't mutated, but that's a separate issue and one that is ideally handled by the planned language to allow migration from ref to in and ref readonly without being a binary or source breaking change.

@AaronRobinsonMSFT
Copy link
Member Author

Wasm failure (1) is acknowledged at #70991 (comment). Failure (2) is #71096.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming this matches what was merged for P6, LGTM

@AaronRobinsonMSFT AaronRobinsonMSFT merged commit 56e4c03 into dotnet:main Jun 27, 2022
@AaronRobinsonMSFT AaronRobinsonMSFT deleted the add_ref_field_annotations branch June 27, 2022 16:50
@ghost ghost locked as resolved and limited conversation to collaborators Jul 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants