diff --git a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs index c2468a1eef9ea928bfbc86fd0f171ced1d6519a1..0bac26d6d7507b581ea7a4eeec2b5b177532e82f 100644 --- a/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs +++ b/src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs @@ -2505,7 +2505,7 @@ private TypeWithState VisitCallReceiver(BoundCall node) { receiverType = VisitRvalueWithState(receiverOpt); - // methods which are members of nullable (ex: ToString, GetHashCode) can be invoked on null receiver. + // methods which are members of Nullable (ex: ToString, GetHashCode) can be invoked on null receiver. // However, inherited methods (ex: GetType) are invoked on a boxed value (since base types are reference types) // and therefore in those cases nullable receivers should be checked for nullness. bool checkNullableValueType = false; diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index 7240e53ade84d0b54b55227a23265902b9b132ac..674ec9e1031cd8e77a1409d7adcbf370cbeed41e 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -78134,50 +78134,6 @@ static void F() Diagnostic(ErrorCode.ERR_NoExplicitConv, "(S?)F").WithArguments("method", "S?").WithLocation(6, 18)); } - [WorkItem(33174, "https://github.com/dotnet/roslyn/issues/33174")] - [Fact] - public void NullableCtor() - { - var source = -@" -static class Program -{ - static void Main() - { - int? x = null; - - x.GetHashCode(); // ok - - x.Extension(); // ok - - x.GetType(); // warning1 - - int? y = null; - y.MemberwiseClone(); // warning2 - - y.Lalala(); // does not exist - } - - static void Extension(this int? self) { } -} -"; - var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); - comp.VerifyDiagnostics( - // (12,9): warning CS8629: Nullable value type may be null. - // x.GetType(); // warning1 - Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "x").WithLocation(12, 9), - // (15,9): warning CS8629: Nullable value type may be null. - // y.MemberwiseClone(); // warning2 - Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y").WithLocation(15, 9), - // (15,11): error CS1540: Cannot access protected member 'object.MemberwiseClone()' via a qualifier of type 'int?'; the qualifier must be of type 'Program' (or derived from it) - // y.MemberwiseClone(); // warning2 - Diagnostic(ErrorCode.ERR_BadProtectedAccess, "MemberwiseClone").WithArguments("object.MemberwiseClone()", "int?", "Program").WithLocation(15, 11), - // (17,11): error CS1061: 'int?' does not contain a definition for 'Lalala' and no accessible extension method 'Lalala' accepting a first argument of type 'int?' could be found (are you missing a using directive or an assembly reference?) - // y.Lalala(); // does not exist - Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "Lalala").WithArguments("int?", "Lalala").WithLocation(17, 11) - ); - } - [WorkItem(33330, "https://github.com/dotnet/roslyn/issues/33330")] [Fact] public void NullableT_32() @@ -78658,6 +78614,50 @@ static void F6(T? t6) ); } + [WorkItem(33174, "https://github.com/dotnet/roslyn/issues/33174")] + [Fact] + public void NullableBaseMembers() + { + var source = +@" +static class Program +{ + static void Main() + { + int? x = null; + + x.GetHashCode(); // ok + + x.Extension(); // ok + + x.GetType(); // warning1 + + int? y = null; + y.MemberwiseClone(); // warning2 + + y.Lalala(); // does not exist + } + + static void Extension(this int? self) { } +} +"; + var comp = CreateCompilation(source, options: WithNonNullTypesTrue()); + comp.VerifyDiagnostics( + // (12,9): warning CS8629: Nullable value type may be null. + // x.GetType(); // warning1 + Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "x").WithLocation(12, 9), + // (15,9): warning CS8629: Nullable value type may be null. + // y.MemberwiseClone(); // warning2 + Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "y").WithLocation(15, 9), + // (15,11): error CS1540: Cannot access protected member 'object.MemberwiseClone()' via a qualifier of type 'int?'; the qualifier must be of type 'Program' (or derived from it) + // y.MemberwiseClone(); // warning2 + Diagnostic(ErrorCode.ERR_BadProtectedAccess, "MemberwiseClone").WithArguments("object.MemberwiseClone()", "int?", "Program").WithLocation(15, 11), + // (17,11): error CS1061: 'int?' does not contain a definition for 'Lalala' and no accessible extension method 'Lalala' accepting a first argument of type 'int?' could be found (are you missing a using directive or an assembly reference?) + // y.Lalala(); // does not exist + Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "Lalala").WithArguments("int?", "Lalala").WithLocation(17, 11) + ); + } + [Fact] public void NullableT_Using() {