From 417785a2bee94a9cf2807a14f72d2ae24a3548dc Mon Sep 17 00:00:00 2001 From: AlekseyTs Date: Mon, 5 Jun 2023 08:00:50 -0700 Subject: [PATCH] `makeNotNullMembersMaybeNull` - skip primary constructor capture fields Fixes #68384. --- .../Portable/FlowAnalysis/NullableWalker.cs | 3 + .../Semantics/PrimaryConstructorTests.cs | 67 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs index 071424a5d9935..eeda541534b67 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs @@ -883,6 +883,9 @@ void makeNotNullMembersMaybeNull() case PropertySymbol: // skip any manually implemented non-required properties. continue; + case FieldSymbol { OriginalDefinition: SynthesizedPrimaryConstructorParameterBackingFieldSymbol }: + // Skip primary constructor capture fields, compiler initializes them with parameters' values + continue; case FieldSymbol { IsConst: true }: continue; case FieldSymbol { AssociatedSymbol: PropertySymbol prop }: diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs index e3905450d2eff..e3e15050ed716 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PrimaryConstructorTests.cs @@ -15389,6 +15389,73 @@ .property instance int32 P2() ".Replace("[mscorlib]", ExecutionConditionUtil.IsDesktop ? "[mscorlib]" : "[netstandard]")); } + [Fact] + [WorkItem("https://github.com/dotnet/roslyn/issues/68384")] + public void ParameterCapturing_152_NullableAnalysis() + { + var source = @" +#nullable enable + +class B(string s) +{ + public string S { get; } = s; +} + +class C(B b) + : B(b.S) +{ + string T() => b.S; +}"; + var comp = CreateCompilation(source, options: TestOptions.ReleaseDll); + comp.VerifyDiagnostics(); + } + + [Fact] + [WorkItem("https://github.com/dotnet/roslyn/issues/68384")] + public void ParameterCapturing_153_NullableAnalysis() + { + var source = @" +#nullable enable + +class B(System.Func s) +{ + public string S { get; } = s(); +} + +class C(B b) + : B(() => b.S) +{ + string T() => b.S; +}"; + var comp = CreateCompilation(source, options: TestOptions.ReleaseDll); + comp.VerifyDiagnostics(); + } + + [Fact] + [WorkItem("https://github.com/dotnet/roslyn/issues/68384")] + public void ParameterCapturing_154_NullableAnalysis() + { + var source = @" +#nullable enable + +class B(System.Func s) +{ + public string S { get; } = s(); +} + +class C(B b) + : B(() => + { + string local() => b.S; + return local(); + }) +{ + string T() => b.S; +}"; + var comp = CreateCompilation(source, options: TestOptions.ReleaseDll); + comp.VerifyDiagnostics(); + } + [Fact] public void CycleDueToIndexerNameAttribute_01() {