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

Add a test observing lack of an issue. #75057

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/Compilers/CSharp/Test/Emit3/RefStructInterfacesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28916,5 +28916,81 @@ interface UsePia5 : ITest29

CompileAndVerify(compilation2, symbolValidator: metadataValidator, verify: Verification.Skipped).VerifyDiagnostics();
}

[Fact]
[WorkItem("https://github.com/dotnet/roslyn/issues/74785")]
public void Issue74785()
{
var src = @"
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

#pragma warning disable CS0649

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

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


interface IBufferInterface
{
[UnscopedRef]
public ReadOnlySpan<byte> Data { get; }
}

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

[UnscopedRef]
public ReadOnlySpan<byte> GetGenericBuffer1()
{
return genericBuffer.Data;
}

[UnscopedRef]
public ReadOnlySpan<byte> GetDirectBuffer1()
{
return directBuffer.Data;
}

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

public ReadOnlySpan<byte> GetGenericBuffer2()
{
#line 1000
return genericBuffer.Data;
}

public ReadOnlySpan<byte> GetDirectBuffer2()
{
#line 2000
return directBuffer.Data;
}
}
";
var comp = CreateCompilation(src, targetFramework: TargetFramework.Net90);

comp.VerifyEmitDiagnostics(
// (1000,16): error CS8170: Struct members cannot return 'this' or other instance members by reference
// return genericBuffer.Data;
Diagnostic(ErrorCode.ERR_RefReturnStructThis, "genericBuffer").WithLocation(1000, 16),
// (2000,16): error CS8170: Struct members cannot return 'this' or other instance members by reference
// return directBuffer.Data;
Diagnostic(ErrorCode.ERR_RefReturnStructThis, "directBuffer").WithLocation(2000, 16)
);
}
}
}