diff --git a/src/Compilers/CSharp/Portable/Binder/ForEachLoopBinder.cs b/src/Compilers/CSharp/Portable/Binder/ForEachLoopBinder.cs index 92533471bc877..7617e77095cba 100644 --- a/src/Compilers/CSharp/Portable/Binder/ForEachLoopBinder.cs +++ b/src/Compilers/CSharp/Portable/Binder/ForEachLoopBinder.cs @@ -938,7 +938,11 @@ EnumeratorResult getEnumeratorInfo(SyntaxNode syntax, SyntaxNode collectionSynta if (collectionExprType is null) // There's no way to enumerate something without a type. { - if (!ReportConstantNullCollectionExpr(collectionExpr, diagnostics)) + if (collectionExpr is BoundLiteral && collectionExpr.ConstantValueOpt is { IsNull: true }) + { + diagnostics.Add(ErrorCode.ERR_NullNotValid, collectionExpr.Syntax.Location); + } + else { // Anything else with a null type is a method group or anonymous function diagnostics.Add(ErrorCode.ERR_AnonMethGrpInForEach, collectionSyntax.Location, collectionExpr.Display); @@ -965,10 +969,6 @@ EnumeratorResult getEnumeratorInfo(SyntaxNode syntax, SyntaxNode collectionSynta // The spec specifically lists the collection, enumerator, and element types for arrays and dynamic. if (collectionExprType.Kind == SymbolKind.ArrayType || collectionExprType.Kind == SymbolKind.DynamicType) { - if (ReportConstantNullCollectionExpr(collectionExpr, diagnostics)) - { - return EnumeratorResult.FailedAndReported; - } builder = GetDefaultEnumeratorInfo(syntax, builder, diagnostics, collectionExprType); return EnumeratorResult.Succeeded; } @@ -979,10 +979,6 @@ EnumeratorResult getEnumeratorInfo(SyntaxNode syntax, SyntaxNode collectionSynta if (SatisfiesGetEnumeratorPattern(syntax, collectionSyntax, ref builder, unwrappedCollectionExpr, isAsync, viaExtensionMethod: false, diagnostics)) { collectionExpr = unwrappedCollectionExpr; - if (ReportConstantNullCollectionExpr(collectionExpr, diagnostics)) - { - return EnumeratorResult.FailedAndReported; - } return createPatternBasedEnumeratorResult(ref builder, unwrappedCollectionExpr, isAsync, viaExtensionMethod: false, diagnostics); } @@ -1014,11 +1010,6 @@ EnumeratorResult getEnumeratorInfo(SyntaxNode syntax, SyntaxNode collectionSynta // Lowering will not use iterator info with strings, so it is ok. if (!isAsync && collectionExprType.SpecialType == SpecialType.System_String) { - if (ReportConstantNullCollectionExpr(collectionExpr, diagnostics)) - { - return EnumeratorResult.FailedAndReported; - } - builder = GetDefaultEnumeratorInfo(syntax, builder, diagnostics, collectionExprType); return EnumeratorResult.Succeeded; } @@ -1078,11 +1069,6 @@ private EnumeratorResult SatisfiesIEnumerableInterfaces(SyntaxNode collectionSyn return EnumeratorResult.FailedNotReported; } - if (ReportConstantNullCollectionExpr(collectionExpr, diagnostics)) - { - return EnumeratorResult.FailedAndReported; - } - SyntaxNode errorLocationSyntax = collectionSyntax; if (foundMultipleGenericIEnumerableInterfaces) @@ -1203,17 +1189,6 @@ private EnumeratorResult SatisfiesIEnumerableInterfaces(SyntaxNode collectionSyn return EnumeratorResult.Succeeded; } - private bool ReportConstantNullCollectionExpr(BoundExpression collectionExpr, BindingDiagnosticBag diagnostics) - { - if (collectionExpr.ConstantValueOpt is { IsNull: true }) - { - // Spec seems to refer to null literals, but Dev10 reports anything known to be null. - diagnostics.Add(ErrorCode.ERR_NullNotValid, collectionExpr.Syntax.Location); - return true; - } - return false; - } - private void GetDisposalInfoForEnumerator(SyntaxNode syntax, ref ForEachEnumeratorInfo.Builder builder, BoundExpression expr, bool isAsync, BindingDiagnosticBag diagnostics) { // NOTE: if IDisposable is not available at all, no diagnostics will be reported - we will just assume that diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAwaitForeachTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAwaitForeachTests.cs index 21249e80d708a..ec192b19169ed 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAwaitForeachTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenAwaitForeachTests.cs @@ -9929,11 +9929,7 @@ public static async Task Main() } }"; CreateCompilationWithTasksExtensions(new[] { source, AsyncStreamsTypes }, parseOptions: TestOptions.Regular9) - .VerifyDiagnostics( - // (9,33): error CS0186: Use of null is not valid in this context - // await foreach (var i in (IAsyncEnumerable)null) - Diagnostic(ErrorCode.ERR_NullNotValid, "(IAsyncEnumerable)null").WithLocation(9, 33) - ); + .VerifyEmitDiagnostics(); } [Fact] @@ -9956,11 +9952,7 @@ public static async Task Main() public IAsyncEnumerator GetAsyncEnumerator() => throw null; }"; CreateCompilationWithTasksExtensions(new[] { source, AsyncStreamsTypes }, parseOptions: TestOptions.Regular9) - .VerifyDiagnostics( - // (9,33): error CS0186: Use of null is not valid in this context - // await foreach (var i in (C)null) - Diagnostic(ErrorCode.ERR_NullNotValid, "(C)null").WithLocation(9, 33) - ); + .VerifyEmitDiagnostics(); } [Fact] diff --git a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenForEachTests.cs b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenForEachTests.cs index 69dabcc2fd877..54a702a536f03 100644 --- a/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenForEachTests.cs +++ b/src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenForEachTests.cs @@ -2434,11 +2434,7 @@ public static void Main() } }"; CreateCompilation(source, parseOptions: TestOptions.Regular9) - .VerifyDiagnostics( - // (8,27): error CS0186: Use of null is not valid in this context - // foreach (var i in (IEnumerable)null) - Diagnostic(ErrorCode.ERR_NullNotValid, "(IEnumerable)null").WithLocation(8, 27) - ); + .VerifyEmitDiagnostics(); } [Fact] @@ -2460,11 +2456,7 @@ public static void Main() public IEnumerator GetEnumerator() => throw null; }"; CreateCompilation(source, parseOptions: TestOptions.Regular9) - .VerifyDiagnostics( - // (8,27): error CS0186: Use of null is not valid in this context - // foreach (var i in (C)null) - Diagnostic(ErrorCode.ERR_NullNotValid, "(C)null").WithLocation(8, 27) - ); + .VerifyEmitDiagnostics(); } [Fact] @@ -2483,11 +2475,7 @@ public static void Main() } }"; CreateCompilation(source, parseOptions: TestOptions.Regular9) - .VerifyDiagnostics( - // (7,27): error CS0186: Use of null is not valid in this context - // foreach (var i in (int[])null) - Diagnostic(ErrorCode.ERR_NullNotValid, "(int[])null").WithLocation(7, 27) - ); + .VerifyEmitDiagnostics(); } [Fact] diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs index 6ac8cbcb83698..5766ef907f609 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs @@ -84,24 +84,472 @@ static void Main() ); } - [WorkItem(540957, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/540957")] - [Fact] - public void TestErrorDefaultOfArrayType() + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefaultArray() { - var text = @" -class C -{ - static void Main() - { - foreach (int x in default(int[])) + var source = """ + class C + { + static void Main() + { + foreach (int x in default(int[])) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefaultString() { + var source = """ + class C + { + static void Main() + { + foreach (char c in default(string)) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); } - } -}"; - CreateCompilation(text).VerifyDiagnostics( - // (7,27): error CS0186: Use of null is not valid in this context - Diagnostic(ErrorCode.ERR_NullNotValid, "default(int[])")); + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefaultIEnumerable() + { + var source = """ + using System.Collections.Generic; + class C + { + static void Main() + { + foreach (var x in default(IEnumerable)) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToIEnumerable() + { + var source = """ + using System.Collections.Generic; + class C + { + static void Main() + { + foreach (var x in (IEnumerable)null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToArray() + { + var source = """ + class C + { + static void Main() + { + foreach (int x in (int[])null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToString() + { + var source = """ + class C + { + static void Main() + { + foreach (char c in (string)null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnPlainNull() + { + var source = """ + class C + { + static void Main() + { + foreach (int x in null) + { + } + } + } + """; + + CreateCompilation(source).VerifyDiagnostics( + // (5,27): error CS0186: Use of null is not valid in this context + // foreach (int x in null) + Diagnostic(ErrorCode.ERR_NullNotValid, "null").WithLocation(5, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefaultArrayWithNullableEnabled() + { + var source = """ + #nullable enable + class C + { + static void Main() + { + foreach (int x in default(int[])) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (6,27): warning CS8602: Dereference of a possibly null reference. + // foreach (int x in default(int[])) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "default(int[])").WithLocation(6, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefaultStringWithNullableEnabled() + { + var source = """ + #nullable enable + class C + { + static void Main() + { + foreach (char c in default(string)) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (6,28): warning CS8602: Dereference of a possibly null reference. + // foreach (char c in default(string)) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "default(string)").WithLocation(6, 28)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefaultIEnumerableWithNullableEnabled() + { + var source = """ + #nullable enable + using System.Collections.Generic; + class C + { + static void Main() + { + foreach (var x in default(IEnumerable)) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (7,27): warning CS8602: Dereference of a possibly null reference. + // foreach (var x in default(IEnumerable)) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "default(IEnumerable)").WithLocation(7, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToIEnumerableWithNullableEnabled() + { + var source = """ + #nullable enable + using System.Collections.Generic; + class C + { + static void Main() + { + foreach (var x in (IEnumerable)null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (7,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // foreach (var x in (IEnumerable)null) + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(IEnumerable)null").WithLocation(7, 27), + // (7,27): warning CS8602: Dereference of a possibly null reference. + // foreach (var x in (IEnumerable)null) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(IEnumerable)null").WithLocation(7, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToArrayWithNullableEnabled() + { + var source = """ + #nullable enable + class C + { + static void Main() + { + foreach (int x in (int[])null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (6,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // foreach (int x in (int[])null) + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(int[])null").WithLocation(6, 27), + // (6,27): warning CS8602: Dereference of a possibly null reference. + // foreach (int x in (int[])null) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(int[])null").WithLocation(6, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToStringWithNullableEnabled() + { + var source = """ + #nullable enable + class C + { + static void Main() + { + foreach (char c in (string)null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (6,28): warning CS8600: Converting null literal or possible null value to non-nullable type. + // foreach (char c in (string)null) + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(string)null").WithLocation(6, 28), + // (6,28): warning CS8602: Dereference of a possibly null reference. + // foreach (char c in (string)null) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(string)null").WithLocation(6, 28)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToDynamic() + { + var source = """ + class C + { + static void Main() + { + foreach (var x in (dynamic)null) + { + } + } + } + """; + + CreateCompilation(source, targetFramework: TargetFramework.StandardAndCSharp).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToMultidimensionalArray() + { + var source = """ + class C + { + static void Main() + { + foreach (int x in (int[,])null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToDynamicWithNullableEnabled() + { + var source = """ + #nullable enable + class C + { + static void Main() + { + foreach (var x in (dynamic)null) + { + } + } + } + """; + + CreateCompilation(source, targetFramework: TargetFramework.StandardAndCSharp).VerifyEmitDiagnostics( + // (6,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // foreach (var x in (dynamic)null) + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(dynamic)null").WithLocation(6, 27), + // (6,27): warning CS8602: Dereference of a possibly null reference. + // foreach (var x in (dynamic)null) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(dynamic)null").WithLocation(6, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToMultidimensionalArrayWithNullableEnabled() + { + var source = """ + #nullable enable + class C + { + static void Main() + { + foreach (int x in (int[,])null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (6,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // foreach (int x in (int[,])null) + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(int[,])null").WithLocation(6, 27), + // (6,27): warning CS8602: Dereference of a possibly null reference. + // foreach (int x in (int[,])null) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(int[,])null").WithLocation(6, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToConstrainedTypeParameter() + { + var source = """ + using System.Collections.Generic; + class C + { + static void M() where T : class, IEnumerable + { + foreach (var x in (T)null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics(); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnNullCastToConstrainedTypeParameterWithNullableEnabled() + { + var source = """ + #nullable enable + using System.Collections.Generic; + class C + { + static void M() where T : class, IEnumerable + { + foreach (var x in (T)null) + { + } + } + } + """; + + CreateCompilation(source).VerifyEmitDiagnostics( + // (7,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // foreach (var x in (T)null) + Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(T)null").WithLocation(7, 27), + // (7,27): warning CS8602: Dereference of a possibly null reference. + // foreach (var x in (T)null) + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(T)null").WithLocation(7, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnDefault() + { + var source = """ + class C + { + static void Main() + { + foreach (var x in default) + { + } + } + } + """; + + CreateCompilation(source).VerifyDiagnostics( + // (5,27): error CS8716: There is no target type for the default literal. + // foreach (var x in default) + Diagnostic(ErrorCode.ERR_DefaultLiteralNoTargetType, "default").WithLocation(5, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnImplicitObjectCreation() + { + var source = """ + class C + { + static void Main() + { + foreach (var x in new()) + { + } + } + } + """; + + CreateCompilation(source).VerifyDiagnostics( + // (5,27): error CS9176: There is no target type for 'new()' + // foreach (var x in new()) + Diagnostic(ErrorCode.ERR_ImplicitObjectCreationNoTargetType, "new()").WithArguments("new()").WithLocation(5, 27)); + } + + [Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")] + public void ForeachOnCollectionExpression() + { + var source = """ + class C + { + static void Main() + { + foreach (var x in []) + { + } + } + } + """; + + CreateCompilation(source).VerifyDiagnostics( + // (5,27): error CS9176: There is no target type for the collection expression. + // foreach (var x in []) + Diagnostic(ErrorCode.ERR_CollectionExpressionNoTargetType, "[]").WithLocation(5, 27)); } [Fact] diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs index e4de84518d4ec..931e8e599c32d 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs @@ -90543,25 +90543,19 @@ static void F() } }"; var comp = CreateCompilation(new[] { source }, options: WithNullableEnable()); - comp.VerifyDiagnostics( - // (7,27): error CS0186: Use of null is not valid in this context - // foreach (var x in (IEnumerable?)null) // 1 - Diagnostic(ErrorCode.ERR_NullNotValid, "(IEnumerable?)null").WithLocation(7, 27), - // (10,27): error CS0186: Use of null is not valid in this context - // foreach (var y in (IEnumerable)default) // 2 - Diagnostic(ErrorCode.ERR_NullNotValid, "(IEnumerable)default").WithLocation(10, 27), - // (13,27): error CS0186: Use of null is not valid in this context - // foreach (var z in default(IEnumerable)) // 3 - Diagnostic(ErrorCode.ERR_NullNotValid, "default(IEnumerable)").WithLocation(13, 27), - // (7,27): warning CS8602: Dereference of a possibly null reference. + comp.VerifyEmitDiagnostics( + // 0.cs(7,27): warning CS8602: Dereference of a possibly null reference. // foreach (var x in (IEnumerable?)null) // 1 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(IEnumerable?)null").WithLocation(7, 27), - // (10,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // 0.cs(10,27): warning CS8600: Converting null literal or possible null value to non-nullable type. // foreach (var y in (IEnumerable)default) // 2 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(IEnumerable)default").WithLocation(10, 27), - // (10,27): warning CS8602: Dereference of a possibly null reference. + // 0.cs(10,27): warning CS8602: Dereference of a possibly null reference. // foreach (var y in (IEnumerable)default) // 2 - Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(IEnumerable)default").WithLocation(10, 27)); + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(IEnumerable)default").WithLocation(10, 27), + // 0.cs(13,27): warning CS8602: Dereference of a possibly null reference. + // foreach (var z in default(IEnumerable)) // 3 + Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "default(IEnumerable)").WithLocation(13, 27)); } [WorkItem(23493, "https://github.com/dotnet/roslyn/issues/23493")] @@ -91444,8 +91438,10 @@ namespace System public class Object { } public struct Void { } public class ValueType { } + public abstract class Enum : ValueType { } public struct Boolean { } public struct Int32 { } + public struct Byte { } public class Exception { } public struct Char { @@ -91469,6 +91465,33 @@ public abstract class Attribute { protected Attribute() { } } + + public enum AttributeTargets + { + Assembly = 0x0001, + Module = 0x0002, + Class = 0x0004, + Struct = 0x0008, + Enum = 0x0010, + Constructor = 0x0020, + Method = 0x0040, + Property = 0x0080, + Field = 0x0100, + Event = 0x0200, + Interface = 0x0400, + Parameter = 0x0800, + Delegate = 0x1000, + ReturnValue = 0x2000, + GenericParameter = 0x4000, + All = 0x7FFF + } + + public sealed class AttributeUsageAttribute : Attribute + { + public AttributeUsageAttribute(AttributeTargets validOn) { } + public bool AllowMultiple { get; set; } + public bool Inherited { get; set; } + } } namespace System.Runtime.CompilerServices @@ -91535,23 +91558,22 @@ void M(string s, string? s2) }"; var comp = CreateEmptyCompilation(new[] { source, systemSource }, options: WithNullableEnable()); - comp.VerifyDiagnostics( - // (11,13): warning CS8600: Converting null literal or possible null value to non-nullable type. + comp.VerifyEmitDiagnostics( + // warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options. + Diagnostic(ErrorCode.WRN_NoRuntimeMetadataVersion).WithLocation(1, 1), + // 0.cs(11,13): warning CS8600: Converting null literal or possible null value to non-nullable type. // s = null; // 1 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "null").WithLocation(11, 13), - // (12,27): warning CS8602: Dereference of a possibly null reference. + // 0.cs(12,27): warning CS8602: Dereference of a possibly null reference. // foreach (var c in s) // 2 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s").WithLocation(12, 27), - // (17,27): warning CS8602: Dereference of a possibly null reference. + // 0.cs(17,27): warning CS8602: Dereference of a possibly null reference. // foreach (var c in s2) // 3 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "s2").WithLocation(17, 27), - // (22,27): error CS0186: Use of null is not valid in this context - // foreach (var c in (string)null) // 4 - Diagnostic(ErrorCode.ERR_NullNotValid, "(string)null").WithLocation(22, 27), - // (22,27): warning CS8600: Converting null literal or possible null value to non-nullable type. + // 0.cs(22,27): warning CS8600: Converting null literal or possible null value to non-nullable type. // foreach (var c in (string)null) // 4 Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(string)null").WithLocation(22, 27), - // (22,27): warning CS8602: Dereference of a possibly null reference. + // 0.cs(22,27): warning CS8602: Dereference of a possibly null reference. // foreach (var c in (string)null) // 4 Diagnostic(ErrorCode.WRN_NullReferenceReceiver, "(string)null").WithLocation(22, 27)); } diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs index a76094971f824..753dd2dbe4397 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/SemanticErrorTests.cs @@ -6972,9 +6972,10 @@ static void Main() foreach (int i in (IEnumerable)null) { }; // CS0186 } }"; - DiagnosticsUtils.VerifyErrorsAndGetCompilationWithMscorlib(text, - new ErrorDescription[] { new ErrorDescription { Code = (int)ErrorCode.ERR_NullNotValid, Line = 9, Column = 27 } , - new ErrorDescription { Code = (int)ErrorCode.ERR_NullNotValid, Line = 10, Column = 27 }}); + CreateCompilation(text).VerifyDiagnostics( + // (9,27): error CS0186: Use of null is not valid in this context + // foreach (int i in null) { } // CS0186 + Diagnostic(ErrorCode.ERR_NullNotValid, "null").WithLocation(9, 27)); } [Fact] @@ -6989,8 +6990,7 @@ public static void Main(string[] args) } } "; - CreateCompilation(text). - VerifyDiagnostics(Diagnostic(ErrorCode.ERR_NullNotValid, "default(int[])")); + CreateCompilation(text).VerifyEmitDiagnostics(); } [WorkItem(540983, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/540983")]