From 20d2c1601f6b9ea40929840e480051f3c18fae91 Mon Sep 17 00:00:00 2001 From: AlekseyTs Date: Wed, 11 Sep 2024 18:06:12 -0700 Subject: [PATCH] Adjust an assert in EmitArgument Fixes #75035. --- .../CSharp/Portable/CodeGen/EmitExpression.cs | 3 +- .../Test/Semantic/Semantics/RefFieldTests.cs | 39 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs b/src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs index 513f6fa480311..ceb96c1c31902 100644 --- a/src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs +++ b/src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs @@ -733,7 +733,8 @@ private void EmitArgument(BoundExpression argument, RefKind refKind) if (unexpectedTemp != null) { // interestingly enough "ref dynamic" sometimes is passed via a clone - Debug.Assert(argument.Type.IsDynamic(), "passing args byref should not clone them into temps"); + // receiver of a ref field can be cloned too + Debug.Assert(argument.Type.IsDynamic() || argument is BoundFieldAccess { FieldSymbol.RefKind: RefKind.Ref }, "passing args byref should not clone them into temps"); AddExpressionTemp(unexpectedTemp); } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs index ac8589853251d..d74247b8d3510 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs @@ -30084,5 +30084,44 @@ ref struct RS // public RS() => ri = 0; Diagnostic(ErrorCode.WRN_UseDefViolationRefField, "ri").WithArguments("ri").WithLocation(4, 20)); } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75035")] + public void RefField_AsRefArgument() + { + var verifier = CompileAndVerify(""" + ref struct S + { + public ref int F1; + + public static S GetS() => default; + + static void M(ref int x){} + + static void Test1() + { + M(ref GetS().F1); + } + } + """, + verify: Verification.Skipped, + targetFramework: TargetFramework.NetCoreApp); + + verifier.VerifyDiagnostics(); + + verifier.VerifyIL("S.Test1", +@" +{ + // Code size 19 (0x13) + .maxstack 1 + .locals init (S V_0) + IL_0000: call ""S S.GetS()"" + IL_0005: stloc.0 + IL_0006: ldloca.s V_0 + IL_0008: ldfld ""ref int S.F1"" + IL_000d: call ""void S.M(ref int)"" + IL_0012: ret +} +"); + } } }