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

Adjust an assert in EmitArgument #75067

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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");
AlekseyTs marked this conversation as resolved.
Show resolved Hide resolved
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
}
");
}
}
}