Skip to content

Inconsistent report of CS8156 when using generically constrained interfaces and [UnscopedRef] #74785

Closed

Description

Version Used:
9.0.100-preview.7.24407.12

Steps to Reproduce:

[InlineArray(256)]
public struct BufferStruct : IBufferInterface
{
    private byte _data;

    [UnscopedRef]
    public ReadOnlySpan<byte> Data => this;
}


interface IBufferInterface
{
    [UnscopedRef] //<--commenting this out removes the CS8156 error below
    public ReadOnlySpan<byte> Data { get; }
}

struct Foo<T> 
    where T : struct, IBufferInterface
{
    T genericBuffer;
    BufferStruct directBuffer;
    IBufferInterface interfaceBuffer;
    

    //[UnscopedRef] //<-- Has no effect
    public ReadOnlySpan<byte> GetGenericBuffer()
    {
        //error CS8156: An expression cannot be used in this 
        //context because it may not be passed or returned by reference
        return genericBuffer.Data;
    }

    [UnscopedRef]//this is required to fix CS8156
    public ReadOnlySpan<byte> GetDirectBuffer()
    {
        //was an error, but it's fixed by [UnscopedRef]
        return directBuffer.Data;
    }

    public ReadOnlySpan<byte> GetInterfaceBuffer()
    {
        return interfaceBuffer.Data;
    }
}

Diagnostic Id:
CS8156

Expected Behavior:
The call to genericBuffer to be identical in behavior to the call to directBuffer

Actual Behavior:
The call to genericBuffer reports error CS8156

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions