Skip to content

Commit

Permalink
Add test for GetType call on ptr constrained to nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
BrzVlad committed Nov 1, 2021
1 parent 807b896 commit 92ee613
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/tests/JIT/Directed/nullabletypes/gettype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Linq;
using System.Collections.Generic;

class C<T>
{
public IEnumerable<T> Data { get; set; }

public C() { }

public bool Check()
{
return Data.ElementAt(0).GetType() == typeof(bool);
}
}

public class P
{
public static int Main()
{
C<bool?> c = new();

// Try a nullable with value
c.Data = new List<bool?> { true };
if(!c.Check())
return 666;

// Try a nullable without value. Should throw NRE
c.Data = new List<bool?> { new Nullable<bool>() };

bool thrown = false;
try
{
c.Check();
}
catch(NullReferenceException)
{
thrown = true;
}
if(!thrown)
return 667;
return 100;
}
}

13 changes: 13 additions & 0 deletions src/tests/JIT/Directed/nullabletypes/gettype_d.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<PropertyGroup>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="gettype.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/tests/JIT/Directed/nullabletypes/gettype_do.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<PropertyGroup>
<DebugType>Full</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="gettype.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/tests/JIT/Directed/nullabletypes/gettype_r.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>False</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="gettype.cs" />
</ItemGroup>
</Project>
13 changes: 13 additions & 0 deletions src/tests/JIT/Directed/nullabletypes/gettype_ro.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<CLRTestPriority>1</CLRTestPriority>
</PropertyGroup>
<PropertyGroup>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="gettype.cs" />
</ItemGroup>
</Project>

0 comments on commit 92ee613

Please sign in to comment.