From 8d370df57b2e700ac1663fca66c536df4ccfb006 Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Wed, 23 Oct 2024 13:29:48 +0200 Subject: [PATCH 1/2] Propagate spread suppression --- .../Portable/Binder/Binder_Conversions.cs | 1 + .../Semantics/CollectionExpressionTests.cs | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs index c4fe66e0d5bb2..7df03440b692e 100644 --- a/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs +++ b/src/Compilers/CSharp/Portable/Binder/Binder_Conversions.cs @@ -991,6 +991,7 @@ BoundNode bindSpreadElement(BoundCollectionExpressionSpreadElement element, Type var expressionSyntax = element.Expression.Syntax; var elementPlaceholder = new BoundValuePlaceholder(expressionSyntax, enumeratorInfo.ElementType) { WasCompilerGenerated = true }; + elementPlaceholder = (BoundValuePlaceholder)elementPlaceholder.WithSuppression(element.Expression.IsSuppressed); var convertElement = CreateConversion( expressionSyntax, elementPlaceholder, diff --git a/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs b/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs index 289a5fd0d79d4..d2e62f576a2d8 100644 --- a/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs +++ b/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs @@ -14178,6 +14178,56 @@ public void Add(T t) { } CompileAndVerify(comp, expectedOutput: "[],"); } + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")] + public void Nullable_Spread_01() + { + var source = """ + #nullable enable + using System.Collections.Generic; + IEnumerable a = [null]; + object[] b = [..a]; + object[] c = [..a!]; + """; + CreateCompilation(source).VerifyDiagnostics( + // (4,17): warning CS8601: Possible null reference assignment. + // object[] b = [..a]; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "a").WithLocation(4, 17)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")] + public void Nullable_Spread_02() + { + var source = """ + #nullable enable + using System.Collections.Generic; + IEnumerable? a = null; + object[] b = [..a]; + object[] c = [..a!]; + """; + CreateCompilation(source).VerifyDiagnostics( + // (4,17): warning CS8602: Dereference of a possibly null reference. + // object[] b = [..a]; + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a").WithLocation(4, 17)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")] + public void Nullable_Spread_03() + { + var source = """ + #nullable enable + using System.Collections.Generic; + IEnumerable a1 = [null]; + IEnumerable a2 = [null]; + object[] b = [..(m() ? a1 : a2)]; + object[] c = [..(m() ? a1 : a2)!]; + bool m() => throw null!; + """; + CreateCompilation(source).VerifyDiagnostics( + // (5,18): warning CS8601: Possible null reference assignment. + // object[] b = [..(m() ? a1 : a2)]; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "m() ? a1 : a2").WithLocation(5, 18)); + } + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69447")] public void NullableValueType_ImplicitConversion() { From 81da7ebb5ca75070917c96b5c8206a06d251083e Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Fri, 25 Oct 2024 10:12:06 +0200 Subject: [PATCH 2/2] Strenghten tests --- .../Semantics/CollectionExpressionTests.cs | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs b/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs index d2e62f576a2d8..c2350f829f9eb 100644 --- a/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs +++ b/src/Compilers/CSharp/Test/Emit3/Semantics/CollectionExpressionTests.cs @@ -14184,14 +14184,19 @@ public void Nullable_Spread_01() var source = """ #nullable enable using System.Collections.Generic; - IEnumerable a = [null]; - object[] b = [..a]; - object[] c = [..a!]; + { + IEnumerable a = [null]; + object[] b = [..a]; + } + { + IEnumerable a = [null]; + object[] b = [..a!]; + } """; CreateCompilation(source).VerifyDiagnostics( - // (4,17): warning CS8601: Possible null reference assignment. - // object[] b = [..a]; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "a").WithLocation(4, 17)); + // (5,21): warning CS8601: Possible null reference assignment. + // object[] b = [..a]; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "a").WithLocation(5, 21)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")] @@ -14200,14 +14205,19 @@ public void Nullable_Spread_02() var source = """ #nullable enable using System.Collections.Generic; - IEnumerable? a = null; - object[] b = [..a]; - object[] c = [..a!]; + { + IEnumerable? a = null; + object[] b = [..a]; + } + { + IEnumerable? a = null; + object[] b = [..a!]; + } """; CreateCompilation(source).VerifyDiagnostics( - // (4,17): warning CS8602: Dereference of a possibly null reference. - // object[] b = [..a]; - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a").WithLocation(4, 17)); + // (5,21): warning CS8602: Dereference of a possibly null reference. + // object[] b = [..a]; + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "a").WithLocation(5, 21)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75560")] @@ -14216,16 +14226,22 @@ public void Nullable_Spread_03() var source = """ #nullable enable using System.Collections.Generic; - IEnumerable a1 = [null]; - IEnumerable a2 = [null]; - object[] b = [..(m() ? a1 : a2)]; - object[] c = [..(m() ? a1 : a2)!]; + { + IEnumerable a1 = [null]; + IEnumerable a2 = [null]; + object[] b = [..(m() ? a1 : a2)]; + } + { + IEnumerable a1 = [null]; + IEnumerable a2 = [null]; + object[] b = [..(m() ? a1 : a2)!]; + } bool m() => throw null!; """; CreateCompilation(source).VerifyDiagnostics( - // (5,18): warning CS8601: Possible null reference assignment. - // object[] b = [..(m() ? a1 : a2)]; - Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "m() ? a1 : a2").WithLocation(5, 18)); + // (6,22): warning CS8601: Possible null reference assignment. + // object[] b = [..(m() ? a1 : a2)]; + Diagnostic(ErrorCode.WRN_NullReferenceAssignment, "m() ? a1 : a2").WithLocation(6, 22)); } [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/69447")]