Skip to content

Commit 607979a

Browse files
[release/dev17.14] Fix nullable crash for field keyword in partial property (#78720)
* Fix nullable crash for field keyword in partial property * Update src/Compilers/CSharp/Test/Emit3/FieldKeywordTests.cs --------- Co-authored-by: Rikki Gibson <[email protected]>
1 parent 8edf7bc commit 607979a

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

src/Compilers/CSharp/Portable/Symbols/Synthesized/SynthesizedBackingFieldSymbol.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ private NullableAnnotation ComputeInferredNullableAnnotation()
170170
return NullableAnnotation.Annotated;
171171
}
172172

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

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

src/Compilers/CSharp/Test/Emit3/FieldKeywordTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12638,5 +12638,77 @@ public void M()
1263812638
// Prop.ToString(); // unexpected warning
1263912639
Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "Prop").WithLocation(14, 9));
1264012640
}
12641+
12642+
[Theory, WorkItem("https://github.com/dotnet/roslyn/issues/78592")]
12643+
[InlineData(""" = "a";""", "")]
12644+
[InlineData("", """ = "a";""")]
12645+
public void PartialProperty_AutoImplGetter_PropertyInitializer(string defInitializer, string implInitializer)
12646+
{
12647+
var source = $$"""
12648+
#nullable enable
12649+
12650+
partial class C
12651+
{
12652+
public partial string Prop { get; set; }{{defInitializer}}
12653+
}
12654+
12655+
partial class C
12656+
{
12657+
public partial string Prop { get; set => Set(ref field, value); }{{implInitializer}}
12658+
12659+
private void Set(ref string dest, string value)
12660+
{
12661+
dest = value;
12662+
}
12663+
}
12664+
""";
12665+
var comp = CreateCompilation(source);
12666+
comp.VerifyEmitDiagnostics();
12667+
}
12668+
12669+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78592")]
12670+
public void Repro_78592()
12671+
{
12672+
var source1 = """
12673+
#nullable enable
12674+
12675+
using System.Collections.Generic;
12676+
using System.Runtime.CompilerServices;
12677+
12678+
namespace TestLibrary
12679+
{
12680+
public partial class Class1
12681+
{
12682+
public partial int P1 { get; set; } = -1;
12683+
12684+
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string? propertyName = null)
12685+
{
12686+
if (EqualityComparer<T>.Default.Equals(storage, value))
12687+
{
12688+
return false;
12689+
}
12690+
12691+
storage = value;
12692+
12693+
return true;
12694+
}
12695+
}
12696+
12697+
}
12698+
""";
12699+
12700+
var source2 = """
12701+
namespace TestLibrary
12702+
{
12703+
public partial class Class1
12704+
{
12705+
public partial int P1 { get; set => SetProperty(ref field, value); }
12706+
}
12707+
}
12708+
""";
12709+
12710+
var comp = CreateCompilation([source1, source2]);
12711+
comp.VerifyEmitDiagnostics();
12712+
}
1264112713
}
1264212714
}

0 commit comments

Comments
 (0)