Skip to content

[release/dev17.14] Fix nullable crash for field keyword in partial property #78720

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

Merged
merged 2 commits into from
May 28, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ private NullableAnnotation ComputeInferredNullableAnnotation()
return NullableAnnotation.Annotated;
}

getAccessor = (SourcePropertyAccessorSymbol?)getAccessor.PartialImplementationPart ?? getAccessor;
// If the get accessor is auto-implemented, the property is not null-resilient.
if (getAccessor.IsAutoPropertyAccessor)
return NullableAnnotation.NotAnnotated;

getAccessor = (SourcePropertyAccessorSymbol?)getAccessor.PartialImplementationPart ?? getAccessor;
var binder = getAccessor.TryGetBodyBinder() ?? throw ExceptionUtilities.UnexpectedValue(getAccessor);
var boundGetAccessor = binder.BindMethodBody(getAccessor.SyntaxNode, BindingDiagnosticBag.Discarded);

Expand Down
72 changes: 72 additions & 0 deletions src/Compilers/CSharp/Test/Emit3/FieldKeywordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12638,5 +12638,77 @@ public void M()
// Prop.ToString(); // unexpected warning
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Prop").WithLocation(14, 9));
}

[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/78592")]
[InlineData(""" = "a";""", "")]
[InlineData("", """ = "a";""")]
public void PartialProperty_AutoImplGetter_PropertyInitializer(string defInitializer, string implInitializer)
{
var source = $$"""
#nullable enable

partial class C
{
public partial string Prop { get; set; }{{defInitializer}}
}

partial class C
{
public partial string Prop { get; set => Set(ref field, value); }{{implInitializer}}

private void Set(ref string dest, string value)
{
dest = value;
}
}
""";
var comp = CreateCompilation(source);
comp.VerifyEmitDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78592")]
public void Repro_78592()
{
var source1 = """
#nullable enable

using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace TestLibrary
{
public partial class Class1
{
public partial int P1 { get; set; } = -1;

protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
{
return false;
}

storage = value;

return true;
}
}

}
""";

var source2 = """
namespace TestLibrary
{
public partial class Class1
{
public partial int P1 { get; set => SetProperty(ref field, value); }
}
}
""";

var comp = CreateCompilation([source1, source2]);
comp.VerifyEmitDiagnostics();
}
}
}