Skip to content

ref field support in .NET runtimes #63768

Closed

Description

@AaronRobinsonMSFT

There are several moving parts to support ref fields, this issue represents work in the actual runtime.

The high-level tasks for the runtime are below to address the ref fields work.

This contributes to enabling #45152.

Activity

  1. added this to the 7.0.0 milestone on Jan 13, 2022
  2. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 13, 2022
  3. modified the milestone: 7.0.0 on Jan 13, 2022
  4. AaronRobinsonMSFT commented on Jan 13, 2022

    @AaronRobinsonMSFT
    MemberAuthor
  5. jkotas commented on Jan 14, 2022

    @jkotas
    Member

    Remove specialized type handling in runtime – Make ArgIterator, RuntimeArgumentHandle, and TypedReference “just types”.

    I am not sure what this is about. The runtime does not have any special handling of these types. (Roslyn does - but you have it under separate check box.)

    Also in the list should be: Delete the internal ByReference<T> type and replace its use with ref fields.

  6. jaredpar commented on Jan 14, 2022

    @jaredpar
    Member

    Remove specialized type handling in runtime – Make ArgIterator, RuntimeArgumentHandle, and TypedReference “just types”.

    I believe @davidwrighton believes there is some spec work necessary before we can remove the notion of special types. Likely I think we will also need a feature flag in corelib to identify runtimes where Roslyn is safe to remove the notion of restricted types and depend solely on the type definition being correct. But agree bulk of the work here is in Roslyn.

  7. AaronRobinsonMSFT commented on Jan 14, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    /cc @tommcdon @noahfalk for Diagnostics impact.

  8. removed
    untriagedNew issue has not been triaged by the area owner
    on Jan 14, 2022
  9. vitek-karas commented on Jan 18, 2022

    @vitek-karas
    Member

    We may possibly need dotnet/linker and mono/cecil work - it needs to be able to roundtrip assembly with ref fields correctly. I know it's not specifically about runtime - but it will be needed in order to use it in the runtime (since we run the code through linker during build).

  10. AaronRobinsonMSFT commented on Jan 18, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    We may possibly need dotnet/linker and mono/cecil work

    This was brought up in the initial planning meeting. If I recall, @davidwrighton wasn't sure there should be any impact.

  11. vitek-karas commented on Jan 19, 2022

    @vitek-karas
    Member

    I created dotnet/linker#2519 to track the validation on the linker side - at the very least we should add some tests for this in the linker.

  12. John0King commented on Jul 1, 2022

    @John0King
  13. svick commented on Jul 1, 2022

    @svick
    Member

    @John0King That is likely going to be fixed, see dotnet/roslyn#62098.

  14. 5 remaining items

  15. AaronRobinsonMSFT commented on Jul 19, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    Nope, this is done. Tests that can/should be in C# have been converted.

  16. vitek-karas commented on Jul 19, 2022

    @vitek-karas
    Member

    We haven't look at linker yet though... dotnet/linker#2519
    @agocke

  17. AaronRobinsonMSFT commented on Jul 19, 2022

    @AaronRobinsonMSFT
    MemberAuthor

    Shoot. That wasn't a checkbox :(

  18. mangod9 commented on Aug 8, 2022

    @mangod9
    Member

    is this expected to be complete this week?

  19. vitek-karas commented on Aug 8, 2022

    @vitek-karas
    Member

    Yes - @jtschuster is working on it in the linker repo right now.

  20. John0King commented on Aug 16, 2022

    @John0King

    ref itself seem's being cut ? eg.

    public struct TreeNode
    {
        public ref TreeNode Left;
        public ref TreeNode Right;
    
        ....
    
    }
  21. hamarb123 commented on Aug 16, 2022

    @hamarb123
    Contributor

    @John0King , ref fields should be in a ref struct, and is only .NET 7+, C# 11+

  22. John0King commented on Aug 16, 2022

    @John0King

    @hamarb123 so there still no way to write a BineryTree or other Tree in struct ?

  23. hamarb123 commented on Aug 16, 2022

    @hamarb123
    Contributor

    (As far as I can tell) No, because a ref field must be in ref struct, but a ref field cannot refer to a ref struct. If your value type is a unmanaged struct, then you could stackalloc some memory and manage everything manually (i.e. create a new allocator like malloc for that segment of memory, re-interpretation of memory for the value type, 'pointers' to other values which could just be an offset from the start etc.) then you could possibly create something of use, but it seems like a lot of effort to me and has the issue of the stackalloc'd memory running out, but the ref field wouldn't really help there and this could be done before already.

  24. John0King commented on Aug 16, 2022

    @John0King

    I think the ref of the field is not the same size of the struct, (I understand the ref is like pointer , it should be 32/64 bit ) so a struct has a ref or another struct of it's type is

    (in byte)
    ----------------
    ref  file a           int filed b    ref field c
    0 1 2 3 4 5 6 7      0 1 2 3        0  1 2 3 4 5 6 7
    
  25. hamarb123 commented on Aug 16, 2022

    @hamarb123
    Contributor

    ref fields are indeed implemented as managed pointers (which are just pointers that move with the GC). The reason that you can't stick them in any type (such as a struct that is not a ref struct) is due to the span safety rules, if you want to read in-depth what they are and why they are what they are see here https://github.com/dotnet/csharplang/blob/main/proposals/low-level-struct-improvements.md. ref fields to ref structs are planned for C# 12 / .NET 8 currently.

  26. ghost locked as resolved and limited conversation to collaborators on Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions