@@ -31991,5 +31991,135 @@ static void Test()
3199131991 // M(GetValue().F);
3199231992 Diagnostic(ErrorCode.ERR_BadArgRef, "GetValue().F").WithArguments("1", "ref").WithLocation(11, 11));
3199331993 }
31994+
31995+ [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/80244")]
31996+ public void Repro_80244_NetCoreApp()
31997+ {
31998+ var comp = CreateCompilation("""
31999+ using System;
32000+ using System.Runtime.CompilerServices;
32001+ using System.Runtime.InteropServices;
32002+
32003+ ref struct SpanReader
32004+ {
32005+ long _spanEndStreamOffset;
32006+ ReadOnlySpan<byte> _buffer;
32007+ public SpanReader(ReadOnlySpan<byte> buffer, long spanStartStreamOffset)
32008+ {
32009+ _buffer = buffer;
32010+ _spanEndStreamOffset = spanStartStreamOffset + buffer.Length;
32011+ }
32012+
32013+ public ref readonly T ReadRef<T>() where T : struct
32014+ {
32015+ if (_buffer.Length >= Unsafe.SizeOf<T>())
32016+ {
32017+ ref readonly T ret = ref MemoryMarshal.Cast<byte, T>(_buffer)[0];
32018+ _buffer = _buffer.Slice(Unsafe.SizeOf<T>());
32019+ return ref ret;
32020+ }
32021+ else
32022+ {
32023+ throw new Exception();
32024+ }
32025+ }
32026+ }
32027+ """,
32028+ targetFramework: TargetFramework.NetCoreApp,
32029+ parseOptions: TestOptions.Regular14);
32030+ comp.VerifyEmitDiagnostics();
32031+ }
32032+
32033+ [Theory, WorkItem("https://github.com/dotnet/roslyn/issues/80244")]
32034+ [InlineData(LanguageVersion.CSharp8), InlineData(LanguageVersion.CSharp14)]
32035+ public void Repro_80244_NetStandard(LanguageVersion consumerLanguageVersion)
32036+ {
32037+ var spanCompilation = CreateCompilation(TestSources.Span, options: TestOptions.UnsafeReleaseDll, parseOptions: TestOptions.Regular8);
32038+ var spanReference = spanCompilation.EmitToImageReference();
32039+ var source0 = """
32040+ namespace System.Runtime.CompilerServices
32041+ {
32042+ public static class Unsafe
32043+ {
32044+ public static int SizeOf<T>() => throw null!;
32045+ }
32046+ }
32047+
32048+ namespace System.Runtime.InteropServices
32049+ {
32050+ public static class MemoryMarshal
32051+ {
32052+ public static ReadOnlySpan<TTo> Cast<TFrom, TTo>(ReadOnlySpan<TFrom> span)
32053+ where TFrom : struct
32054+ => throw null!;
32055+ }
32056+ }
32057+ """;
32058+ var source1 = """
32059+ using System;
32060+ using System.Runtime.CompilerServices;
32061+ using System.Runtime.InteropServices;
32062+
32063+ ref struct SpanReader
32064+ {
32065+ long _spanEndStreamOffset;
32066+ ReadOnlySpan<byte> _buffer;
32067+ public SpanReader(ReadOnlySpan<byte> buffer, long spanStartStreamOffset)
32068+ {
32069+ _buffer = buffer;
32070+ _spanEndStreamOffset = spanStartStreamOffset + buffer.Length;
32071+ }
32072+
32073+ public ref readonly T ReadRef<T>() where T : struct
32074+ {
32075+ if (_buffer.Length >= Unsafe.SizeOf<T>())
32076+ {
32077+ ref readonly T ret = ref MemoryMarshal.Cast<byte, T>(_buffer)[0];
32078+ _buffer = _buffer.Slice(Unsafe.SizeOf<T>());
32079+ return ref ret;
32080+ }
32081+ else
32082+ {
32083+ throw new Exception();
32084+ }
32085+ }
32086+ }
32087+ """;
32088+ var comp = CreateCompilation([source0, source1],
32089+ references: [spanReference],
32090+ parseOptions: TestOptions.Regular.WithLanguageVersion(consumerLanguageVersion),
32091+ targetFramework: TargetFramework.NetStandard20);
32092+ comp.VerifyEmitDiagnostics();
32093+ }
32094+
32095+ [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/80244")]
32096+ public void Repro_80244_Simple()
32097+ {
32098+ var source0 = """
32099+ public ref struct RS
32100+ {
32101+ public ref byte this[int i] => throw null!;
32102+ }
32103+ """;
32104+
32105+ var reference = CreateCompilation(source0, parseOptions: TestOptions.Regular8).EmitToImageReference();
32106+ var source1 = """
32107+ class Program
32108+ {
32109+ static ref byte M1(RS rs)
32110+ {
32111+ ref byte ret = ref rs[1];
32112+ return ref ret;
32113+ }
32114+
32115+ static ref byte M2(RS rs)
32116+ {
32117+ return ref rs[1];
32118+ }
32119+ }
32120+ """;
32121+ var comp = CreateCompilation(source1, references: [reference], parseOptions: TestOptions.Regular8);
32122+ comp.VerifyEmitDiagnostics();
32123+ }
3199432124 }
3199532125}
0 commit comments