-
Couldn't load subscription status.
- Fork 5.2k
Fix compareTypesForEquality #97062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix compareTypesForEquality #97062
Changes from all commits
a9c69c9
e621449
e00f6b1
da7f0bd
b9b0d66
08b95ac
50eb486
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4448,6 +4448,61 @@ TypeCompareState CEEInfo::compareTypesForCast( | |
| return result; | ||
| } | ||
|
|
||
| // Returns true if hnd1 and hnd2 are guaranteed to represent different types. Returns false if hnd1 and hnd2 may represent the same type. | ||
| static bool AreGuaranteedToRepresentDifferentTypes(TypeHandle hnd1, TypeHandle hnd2) | ||
| { | ||
| STANDARD_VM_CONTRACT; | ||
|
|
||
| CorElementType et1 = hnd1.GetSignatureCorElementType(); | ||
| CorElementType et2 = hnd2.GetSignatureCorElementType(); | ||
|
|
||
| TypeHandle canonHnd = TypeHandle(g_pCanonMethodTableClass); | ||
| if ((hnd1 == canonHnd) || (hnd2 == canonHnd)) | ||
| { | ||
| return CorTypeInfo::IsObjRef(et1) != CorTypeInfo::IsObjRef(et2); | ||
| } | ||
|
|
||
| if (et1 != et2) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| switch (et1) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This improves the original implementation in addition to make it correct. For example, this can prove that |
||
| { | ||
| case ELEMENT_TYPE_ARRAY: | ||
| if (hnd1.GetRank() != hnd2.GetRank()) | ||
| return true; | ||
| return AreGuaranteedToRepresentDifferentTypes(hnd1.GetTypeParam(), hnd2.GetTypeParam()); | ||
| case ELEMENT_TYPE_SZARRAY: | ||
| case ELEMENT_TYPE_BYREF: | ||
| case ELEMENT_TYPE_PTR: | ||
| return AreGuaranteedToRepresentDifferentTypes(hnd1.GetTypeParam(), hnd2.GetTypeParam()); | ||
|
|
||
| default: | ||
| if (!hnd1.IsTypeDesc()) | ||
| { | ||
| if (!hnd1.AsMethodTable()->HasSameTypeDefAs(hnd2.AsMethodTable())) | ||
| return true; | ||
|
|
||
| if (hnd1.AsMethodTable()->HasInstantiation()) | ||
| { | ||
| Instantiation inst1 = hnd1.AsMethodTable()->GetInstantiation(); | ||
| Instantiation inst2 = hnd2.AsMethodTable()->GetInstantiation(); | ||
| _ASSERTE(inst1.GetNumArgs() == inst2.GetNumArgs()); | ||
|
|
||
| for (DWORD i = 0; i < inst1.GetNumArgs(); i++) | ||
| { | ||
| if (AreGuaranteedToRepresentDifferentTypes(inst1[i], inst2[i])) | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /*********************************************************************/ | ||
| // See if types represented by cls1 and cls2 compare equal, not | ||
| // equal, or the comparison needs to be resolved at runtime. | ||
|
|
@@ -4473,30 +4528,12 @@ TypeCompareState CEEInfo::compareTypesForEquality( | |
| { | ||
| result = (hnd1 == hnd2 ? TypeCompareState::Must : TypeCompareState::MustNot); | ||
| } | ||
| // If either or both types are canonical subtypes, we can sometimes prove inequality. | ||
| else | ||
| { | ||
| // If either is a value type then the types cannot | ||
| // be equal unless the type defs are the same. | ||
| if (hnd1.IsValueType() || hnd2.IsValueType()) | ||
| // If either or both types are canonical subtypes, we can sometimes prove inequality. | ||
| if (AreGuaranteedToRepresentDifferentTypes(hnd1, hnd2)) | ||
| { | ||
| if (!hnd1.GetMethodTable()->HasSameTypeDefAs(hnd2.GetMethodTable())) | ||
| { | ||
| result = TypeCompareState::MustNot; | ||
| } | ||
| } | ||
| // If we have two ref types that are not __Canon, then the | ||
| // types cannot be equal unless the type defs are the same. | ||
| else | ||
| { | ||
| TypeHandle canonHnd = TypeHandle(g_pCanonMethodTableClass); | ||
| if ((hnd1 != canonHnd) && (hnd2 != canonHnd)) | ||
| { | ||
| if (!hnd1.GetMethodTable()->HasSameTypeDefAs(hnd2.GetMethodTable())) | ||
| { | ||
| result = TypeCompareState::MustNot; | ||
| } | ||
| } | ||
| result = TypeCompareState::MustNot; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // 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.Runtime.CompilerServices; | ||
| using Xunit; | ||
|
|
||
| public class Test96876 | ||
| { | ||
| [Fact] | ||
| public static void TestEntryPoint() | ||
| { | ||
| Assert.True(foo<string>(new string[1])); | ||
| Assert.False(foo<object>(new string[1])); | ||
|
|
||
| Assert.True(foo2<string>()); | ||
| Assert.False(foo2<object>()); | ||
| } | ||
|
|
||
| // Validate that the type equality involving shared array types is handled correctly | ||
| // in shared generic code. | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static bool foo<T>(string[] list) => typeof(T[]) == list.GetType(); | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static bool foo2<T>() => typeof(T[]) == typeof(string[]); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <CLRTestPriority>1</CLRTestPriority> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="test96876.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" /> | ||
| </ItemGroup> | ||
| </Project> |
Uh oh!
There was an error while loading. Please reload this page.