Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions src/Compilers/CSharp/Portable/Binder/Binder.ValueChecks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,6 @@ private bool CheckFieldValueKind(SyntaxNode node, BoundFieldAccess fieldAccess,

if (RequiresRefAssignableVariable(valueKind))
{
Debug.Assert(!fieldSymbol.IsStatic);
Debug.Assert(valueKind == BindValueKind.RefAssignable);

switch (fieldSymbol.RefKind)
Expand All @@ -1399,7 +1398,16 @@ private bool CheckFieldValueKind(SyntaxNode node, BoundFieldAccess fieldAccess,
return false;
case RefKind.Ref:
case RefKind.RefReadOnly:
return CheckIsValidReceiverForVariable(node, fieldAccess.ReceiverOpt, BindValueKind.Assignable, diagnostics);
if (fieldSymbol.IsStatic)
{
Debug.Assert(fieldAccess.ReceiverOpt is null or BoundTypeExpression);
break;
}
else
{
Debug.Assert(fieldAccess.ReceiverOpt is not null);
return CheckIsValidReceiverForVariable(node, fieldAccess.ReceiverOpt, BindValueKind.Assignable, diagnostics);
}
default:
throw ExceptionUtilities.UnexpectedValue(fieldSymbol.RefKind);
}
Expand Down
62 changes: 62 additions & 0 deletions src/Compilers/CSharp/Test/Semantic/Semantics/RefFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,32 @@ .field public int32& F2
CompileAndVerify(comp, verify: Verification.Skipped);
}

[WorkItem(78700, "https://github.com/dotnet/roslyn/issues/78700")]
[Fact]
public void StaticRefFieldInClass()
{
var code = """
class Program
{
static int g_3 = -6;
static int g_4 = 123;
static ref int g_2 = ref g_3;

static void Main(){
g_2 = ref g_4;
}
}
""";
var comp = CreateCompilation(code, references: [], parseOptions: TestOptions.Regular13, targetFramework: TargetFramework.Net70);
comp.VerifyEmitDiagnostics(
// (5,20): error CS0106: The modifier 'static' is not valid for this item
// static ref int g_2 = ref g_3;
Diagnostic(ErrorCode.ERR_BadMemberFlag, "g_2").WithArguments("static").WithLocation(5, 20),
// (5,20): error CS9059: A ref field can only be declared in a ref struct.
// static ref int g_2 = ref g_3;
Diagnostic(ErrorCode.ERR_RefFieldInNonRefStruct, "g_2").WithLocation(5, 20));
}

[Fact]
public void RefAndReadonlyRefStruct_01()
{
Expand Down Expand Up @@ -2069,6 +2095,42 @@ public void NonRefStructContainer(string type)
Diagnostic(ErrorCode.ERR_RefFieldInNonRefStruct, "F").WithLocation(4, 13));
}

[Theory]
[InlineData("class")]
[InlineData("struct")]
[InlineData("record")]
[InlineData("record struct")]
Copy link
Member

@RikkiGibson RikkiGibson May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we also have a test for static ref int F; in ref struct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public void NonRefStructContainerWithStaticRefField(string type)
{
var source =
$@"#pragma warning disable 169
{type} R
{{
static ref int F;
}}";

var comp = CreateCompilation(source, parseOptions: TestOptions.Regular10, targetFramework: TargetFramework.Net70);
comp.VerifyEmitDiagnostics(
// (4,12): error CS8936: Feature 'ref fields' is not available in C# 10.0. Please use language version 11.0 or greater.
// static ref int F;
Diagnostic(ErrorCode.ERR_FeatureNotAvailableInVersion10, "ref int").WithArguments("ref fields", "11.0").WithLocation(4, 12),
// (4,20): error CS0106: The modifier 'static' is not valid for this item
// static ref int F;
Diagnostic(ErrorCode.ERR_BadMemberFlag, "F").WithArguments("static").WithLocation(4, 20),
// (4,20): error CS9059: A ref field can only be declared in a ref struct.
// static ref int F;
Diagnostic(ErrorCode.ERR_RefFieldInNonRefStruct, "F").WithLocation(4, 20));

comp = CreateCompilation(source, targetFramework: TargetFramework.Net70);
comp.VerifyEmitDiagnostics(
// (4,20): error CS0106: The modifier 'static' is not valid for this item
// static ref int F;
Diagnostic(ErrorCode.ERR_BadMemberFlag, "F").WithArguments("static").WithLocation(4, 20),
// (4,20): error CS9059: A ref field can only be declared in a ref struct.
// static ref int F;
Diagnostic(ErrorCode.ERR_RefFieldInNonRefStruct, "F").WithLocation(4, 20));
}

/// <summary>
/// Determination of enum underlying type should ignore ref fields
/// and fields with required custom modifiers.
Expand Down