Skip to content

Commit

Permalink
Check for BackingField instead of IsAutoProperty in NullableWalker (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkiGibson authored Nov 21, 2024
1 parent 1207044 commit 9b2cd5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ static IEnumerable<Symbol> getAllMembersToBeDefaulted(Symbol requiredMember)
}

static Symbol getFieldSymbolToBeInitialized(Symbol requiredMember)
=> requiredMember is SourcePropertySymbol { IsAutoProperty: true } prop ? prop.BackingField : requiredMember;
=> requiredMember is SourcePropertySymbolBase { BackingField: { } backingField } ? backingField : requiredMember;
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/Compilers/CSharp/Test/Symbol/Symbols/RequiredMembersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4517,17 +4517,19 @@ public C(bool unused) : this()
);
}

[Fact, CompilerTrait(CompilerFeature.NullableReferenceTypes)]
[Theory, CompilerTrait(CompilerFeature.NullableReferenceTypes)]
[WorkItem(6754, "https://github.com/dotnet/csharplang/issues/6754")]
public void RequiredMemberSuppressesNullabilityWarnings_MemberNotNull_ChainedConstructor_03()
[InlineData("public string Property2 { get; set; }")]
[InlineData("public string Property2 { get => field; set => field = value; }")]
public void RequiredMemberSuppressesNullabilityWarnings_MemberNotNull_ChainedConstructor_03(string property2Definition)
{
var code = """
var code = $$"""
using System.Diagnostics.CodeAnalysis;
#nullable enable
class C
{
public required string Property1 { get => Property2; [MemberNotNull(nameof(Property2))] set => Property2 = value; }
public string Property2 { get; set; }
{{property2Definition}}
public C() { }
public C(bool unused) : this()
Expand All @@ -4549,17 +4551,19 @@ public C(bool unused) : this()
);
}

[Fact, CompilerTrait(CompilerFeature.NullableReferenceTypes)]
[Theory, CompilerTrait(CompilerFeature.NullableReferenceTypes)]
[WorkItem(6754, "https://github.com/dotnet/csharplang/issues/6754")]
public void RequiredMemberSuppressesNullabilityWarnings_MemberNotNull_ChainedConstructor_04()
[InlineData("public string Property2 { get; set; }")]
[InlineData("public string Property2 { get => field; set => field = value; }")]
public void RequiredMemberSuppressesNullabilityWarnings_MemberNotNull_ChainedConstructor_04(string property2Definition)
{
var code = """
var code = $$"""
using System.Diagnostics.CodeAnalysis;
#nullable enable
class C
{
public required string Property1 { get => Property2; [MemberNotNull(nameof(Property2))] set => Property2 = value; }
public string Property2 { get; set; }
{{property2Definition}}
public C() { }
[SetsRequiredMembers]
Expand Down

0 comments on commit 9b2cd5d

Please sign in to comment.