Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[API Proposal]: change 'Unsafe.AsPointer' parameter to be 'ref readonly' #114189

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

Closed
Tracked by #114179
Sergio0694 opened this issue Apr 3, 2025 · 7 comments · Fixed by #114406
Closed
Tracked by #114179

[API Proposal]: change 'Unsafe.AsPointer' parameter to be 'ref readonly' #114189

Sergio0694 opened this issue Apr 3, 2025 · 7 comments · Fixed by #114406
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime.CompilerServices
Milestone

Comments

@Sergio0694
Copy link
Contributor

Sergio0694 commented Apr 3, 2025

Follow up from #85911

Background and motivation

This proposal is about also updating Unsafe.AsPointer to use ref readonly. This is the same change we did for the other APIs in #85911, and it is not breaking. We didn't include AsPointer at the time as we didn't have any use cases. This would now benefit us in CsWinRT (see #114024), as it would simplify all property accessors to get the CCW vtables for projected and custom mapped types.

API Proposal

namespace System.Runtime.CompilerServices
{
    public static class Unsafe
    {
-       public static void* AsPointer<T>(ref T value);
+       public static void* AsPointer<T>(ref readonly T value);
    }
}

API Usage

public static class SomeTypeImpl
{
    [FixedAddressValueType];
    private static readonly SomeTypeVtbl Vftbl;

    public static nint Vftbl => (nint)Unsafe.AsPointer(in Vftbl);

    static SomeTypeImpl()
    {
        // Initialize vtable
    }
}

Risks

None. Unsafe.AsPointer is already an unsafe API, and it's in an unsafe namespace. This API makes things simpler, but developer could already achieve the same, just by adding an Unsafe.AsRef call on top of this chain. Just updating the signature for AsPointer simplifies all callsites instead.

@Sergio0694 Sergio0694 added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.CompilerServices labels Apr 3, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 3, 2025
Copy link
Contributor

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

@ericstj ericstj added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner labels Apr 4, 2025
@ericstj ericstj added this to the 10.0.0 milestone Apr 4, 2025
@ericstj
Copy link
Member

ericstj commented Apr 4, 2025

@tannergooding FYI - marked this one ready for review since it seems to be minor addition to past work.

@bartonjs
Copy link
Member

bartonjs commented Apr 8, 2025

Video

  • Looks good as proposed
namespace System.Runtime.CompilerServices
{
    public static class Unsafe
    {
-       public static void* AsPointer<T>(ref T value);
+       public static void* AsPointer<T>(ref readonly T value);
    }
}

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Apr 8, 2025
@EgorBo
Copy link
Member

EgorBo commented Apr 9, 2025

Risks
None. Unsafe.AsPointer is already an unsafe API, and it's in an unsafe namespace. This API makes things simpler, but developer could already achieve the same, just by adding an Unsafe.AsRef call on top of this chain. Just updating the signature for AsPointer simplifies all callsites instead.

IMO this makes an unsafe api even more unsafe since it implicitly converts an immutable ref to a mutable where previously developer had to state that with AsRef. Should we continue making unsafe code more convenient to write? PS: I'm not against this change, just hoping we won't continue in this direction

@hamarb123
Copy link
Contributor

hamarb123 commented Apr 9, 2025

I don't see how it's really any different to allowing &myLocalOfStructType.ReadonlyField (which gives a pointer, which is therefore mutable)

@tannergooding
Copy link
Member

tannergooding commented Apr 10, 2025

Right, this was covered in API review a bit.

The reason this one is "ok" is because the point of AsPointer<T>(...) is to go from a byref to a pointer without pinning and going directly from a ref readonly T or a in T to a void* via pinning is already allowed.

We don't want to do this for things like Add because that would be two levels of unsafety that isn't already trivially possible. We'd rather want some kind of "matched mutability" concept so we can say if the input is ref readonly the output is ref readonly, but if the input is ref the output is ref.

@Sergio0694
Copy link
Contributor Author

<permission P> when??? 👀 /s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime.CompilerServices
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants