From 634d8448feb1ce670f9e9dab208cb903199819e3 Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Tue, 14 Jun 2022 14:40:52 -0700 Subject: [PATCH 1/2] Skip assignment nullability warning in error case --- .../Portable/FlowAnalysis/NullableWalker.cs | 2 +- .../Semantics/NullableReferenceTypesTests.cs | 32 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs index 7702ed82c32f9..70bc67e558d31 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs @@ -8083,7 +8083,7 @@ private TypeWithState VisitConversion( TypeWithState resultType = calculateResultType(targetTypeWithNullability, fromExplicitCast, resultState, isSuppressed, targetType); - if (operandType.Type?.IsErrorType() != true && !targetType.IsErrorType()) + if (operandType.Type?.IsErrorType() != true && !targetType.IsErrorType() && !conversionOperand.HasErrors) { // Need to report all warnings that apply since the warnings can be suppressed individually. if (reportTopLevelWarnings) diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index d845f86af464d..575a9c90bdbe7 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -2646,18 +2646,12 @@ static void M(C x) // (6,26): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('C') // C* y1 = &x; Diagnostic(ErrorCode.ERR_ManagedAddr, "&x").WithArguments("C").WithLocation(6, 26), - // (6,26): warning CS8619: Nullability of reference types in value of type 'C*' doesn't match target type 'C*'. - // C* y1 = &x; - Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "&x").WithArguments("C*", "C*").WithLocation(6, 26), // (7,9): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('C') // C* y2 = &x!; Diagnostic(ErrorCode.ERR_ManagedAddr, "C*").WithArguments("C").WithLocation(7, 9), // (7,26): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('C') // C* y2 = &x!; Diagnostic(ErrorCode.ERR_ManagedAddr, "&x!").WithArguments("C").WithLocation(7, 26), - // (7,26): warning CS8619: Nullability of reference types in value of type 'C*' doesn't match target type 'C*'. - // C* y2 = &x!; - Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "&x!").WithArguments("C*", "C*").WithLocation(7, 26), // (7,27): error CS8598: The suppression operator is not allowed in this context // C* y2 = &x!; Diagnostic(ErrorCode.ERR_IllegalSuppression, "x").WithLocation(7, 27) @@ -156369,5 +156363,31 @@ public static void M(C c1, C? c2) Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "c2").WithLocation(25, 13) ); } + + [Fact, WorkItem(61462, "https://github.com/dotnet/roslyn/issues/61462")] + public void NoNullabilityWarningUnlessAssignmentConversionExists() + { + var comp = CreateCompilation(""" +#nullable enable + +public static class S +{ + public const string A = "a"; +} + +public class C +{ + public (string, string) M() + { + return ("a", S.B); + } +} +"""); + comp.VerifyDiagnostics( + // (12,24): error CS0117: 'S' does not contain a definition for 'B' + // return ("a", S.B); + Diagnostic(ErrorCode.ERR_NoSuchMember, "B").WithArguments("S", "B").WithLocation(12, 24) + ); + } } } From 3c39f99c1ffea12b89a69a1d2be67304966e347f Mon Sep 17 00:00:00 2001 From: Julien Couvreur Date: Thu, 16 Jun 2022 18:35:49 -0700 Subject: [PATCH 2/2] Address feedback --- src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs index 70bc67e558d31..ac30d97169e64 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs @@ -8083,7 +8083,7 @@ private TypeWithState VisitConversion( TypeWithState resultType = calculateResultType(targetTypeWithNullability, fromExplicitCast, resultState, isSuppressed, targetType); - if (operandType.Type?.IsErrorType() != true && !targetType.IsErrorType() && !conversionOperand.HasErrors) + if (!conversionOperand.HasErrors && !targetType.IsErrorType()) { // Need to report all warnings that apply since the warnings can be suppressed individually. if (reportTopLevelWarnings)