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

A ref T field where T is unmanaged is itself treated as unmanaged #62528

Closed
DaZombieKiller opened this issue Jul 9, 2022 · 5 comments
Closed

Comments

@DaZombieKiller
Copy link

Tested with a daily SDK build (7.0.100-preview.7.22358.13):

unsafe
{
    var byref = default(ByReference<int>);
    var byptr = &byref; // does not error
}

readonly ref struct ByReference<T>
{
    public readonly ref T Value;
}

Expected Behavior:
error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('ByReference<int>')

Actual Behavior:
The code compiles successfully.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 9, 2022
@DaZombieKiller
Copy link
Author

Worth noting: assuming this is unintended behaviour, it is currently being relied upon by the reflection stack.

@RikkiGibson
Copy link
Contributor

It feels like it would be reasonable to make people use https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.unsafe.aspointer?view=net-6.0 if they want to keep doing this.

@Sergio0694
Copy link
Contributor

Note, for that they'd currently be forced to use a function pointer jump, which adds an extra indirection. As in:

unsafe
{
    var byref = default(ByReference<int>);
    var f1 = (delegate*<ref byte, ref byte>)&Unsafe.As<byte, byte>;
    var f2 = (delegate*<ref ByReference<int>, ref byte>)f1;
    
    void* byptr = Unsafe.AsPointer(ref f2(ref byref));
}

That said, it would work around the issue until ref struct-s in generics are allowed 😄

@jcouv jcouv removed the untriaged Issues and PRs which have not yet been triaged by a lead label Jul 28, 2022
@jcouv jcouv added this to the 17.4 milestone Jul 28, 2022
@jcouv jcouv added the Bug label Jul 28, 2022
@MichalPetryka
Copy link

@jaredpar Did #63209 fix this too?

@cston
Copy link
Member

cston commented Sep 23, 2022

Thanks for reporting this issue. It has been fixed in #64064.

The compiler now reports:

warning CS8500: This takes the address of, gets the size of, or declares a pointer to
    a managed type ('ByReference<int>')

cc @jcouv

@cston cston closed this as completed Sep 23, 2022
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

6 participants