Skip to content

Commit

Permalink
Do not crash on a never true typeof(T) == typeof(Gen<>) (#98836)
Browse files Browse the repository at this point in the history
Fixes #98821.
  • Loading branch information
MichalStrehovsky committed Feb 23, 2024
1 parent 34ecd6f commit ec7cc7b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,10 @@ private static bool TryExpandTypeEquality(MethodIL methodIL, byte[] body, Opcode
if (!ReadGetTypeFromHandle(ref reader, methodIL, flags))
return false;

// No value in making this work for definitions
if (type1.IsGenericDefinition || type2.IsGenericDefinition)
return false;

// Dataflow runs on top of uninstantiated IL and we can't answer some questions there.
// Unfortunately this means dataflow will still see code that the rest of the system
// might have optimized away. It should not be a problem in practice.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ public static void Run()

class TestTypeEquals
{
sealed class Gen<T> { }

sealed class Never { }

static Type s_type = null;
Expand All @@ -350,6 +352,9 @@ public static void Run()
// despite the typeof
Console.WriteLine(s_type == typeof(Never));

// This was a compiler crash
Console.WriteLine(typeof(object) == typeof(Gen<>));

#if !DEBUG
ThrowIfPresent(typeof(TestTypeEquals), nameof(Never));
#endif
Expand Down

0 comments on commit ec7cc7b

Please sign in to comment.