dotnet/roslyn

Debugger fails to expand ref members in the same way it expands pointer members.

Open

#57,220 opened on Oct 18, 2021

 (3 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-InteractiveConcept-Continuous Improvementhelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

C# 10 Steps to Reproduce:

Consider this code (a bit contrived but it shows the problem)

namespace ConsoleApp38
{
    internal class Program
    {

        static void Main(string[] args)
        {
            Demo demo = new Demo();

            demo.Ref.f0 = 123;
        }
    }

    public unsafe struct Demo
    {
        private readonly Fields * fields;

        public Demo()
        {
            // Just allocate some memory so we can write to it
            fields = (Fields*)Marshal.AllocHGlobal(sizeof(Fields));
            fields->f0 = 0;
            fields->f1 = 0;
        }

        public Fields * Ptr { get { return fields; } }

        public ref Fields Ref { get { return ref (*fields); } }
    }

    public struct Fields
    {
        public int f0;
        public double f1;
    }
}

Expected Behavior:

When debugged with a break on the line that assigns 123 to f0, we can see in the Locals window that demo can be expanded as can Ptr (but not Ref).

Actual Behavior:

The debugger does not let us 'expand' the 'ref', which is a ref return property.

If you execute the line you can also see that the structs fields f0 is updated via the ref, so we can clearly see that the ref and ptr are valid values.

I'm regarding this as a bug but there may be more to this...

Contributor guide