Skip to content

Commit

Permalink
Adjust an assert in EmitArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyTs committed Sep 12, 2024
1 parent 09ef0c0 commit 20d2c16
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Compilers/CSharp/Portable/CodeGen/EmitExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
39 changes: 39 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
");
}
}
}

0 comments on commit 20d2c16

Please sign in to comment.