Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 85 additions & 17 deletions src/Compilers/CSharp/Test/Semantic/Semantics/ForEachTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,21 @@ class C
{
static void Main()
{
foreach (int x in default(int[]))
try
{
foreach (int x in default(int[]))
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{

I think it will be useful to observe that the loop didn't execute, consider printing something from the loop, or throw a different exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CyrusNajmabadi Did you see this comment?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did. I didn't think it was worthwhile enough, and didn't think the comment was blocking. So i went ahead with merging.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did. I didn't think it was worthwhile enough, and didn't think the comment was blocking. So i went ahead with merging.

Just making sure you saw it

}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand All @@ -110,14 +117,21 @@ class C
{
static void Main()
{
foreach (char c in default(string))
try
{
foreach (char c in default(string))
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand All @@ -129,14 +143,21 @@ class C
{
static void Main()
{
foreach (var x in default(IEnumerable<int>))
try
{
foreach (var x in default(IEnumerable<int>))
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand All @@ -148,14 +169,21 @@ class C
{
static void Main()
{
foreach (var x in (IEnumerable<int>)null)
try
{
foreach (var x in (IEnumerable<int>)null)
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand All @@ -166,14 +194,21 @@ class C
{
static void Main()
{
foreach (int x in (int[])null)
try
{
foreach (int x in (int[])null)
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand All @@ -184,14 +219,21 @@ class C
{
static void Main()
{
foreach (char c in (string)null)
try
{
foreach (char c in (string)null)
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand Down Expand Up @@ -366,14 +408,21 @@ class C
{
static void Main()
{
foreach (var x in (dynamic)null)
try
{
foreach (var x in (dynamic)null)
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source, targetFramework: TargetFramework.StandardAndCSharp).VerifyEmitDiagnostics();
CompileAndVerify(source, targetFramework: TargetFramework.StandardAndCSharp, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand All @@ -384,14 +433,21 @@ class C
{
static void Main()
{
foreach (int x in (int[,])null)
try
{
foreach (int x in (int[,])null)
{
}
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand Down Expand Up @@ -451,6 +507,18 @@ public void ForeachOnNullCastToConstrainedTypeParameter()
using System.Collections.Generic;
class C
{
static void Main()
{
try
{
M<List<int>>();
}
catch (System.NullReferenceException)
{
System.Console.Write("NullReferenceException");
}
}

static void M<T>() where T : class, IEnumerable<int>
{
foreach (var x in (T)null)
Expand All @@ -460,7 +528,7 @@ static void M<T>() where T : class, IEnumerable<int>
}
""";

CreateCompilation(source).VerifyEmitDiagnostics();
CompileAndVerify(source, expectedOutput: "NullReferenceException").VerifyDiagnostics();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/45616")]
Expand Down