-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Merged
Merged
Fix compareTypesForEquality #97062
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9c69c9
Fix compareTypesForEquality
jkotas e621449
Fix build break
jkotas e00f6b1
Add test
jkotas da7f0bd
Fix managed type system implementation
jkotas b9b0d66
Additional test
jkotas 08b95ac
Merge branch 'main' into issue-96876
jkotas 50eb486
Update src/tests/Regressions/coreclr/GitHub_96876/test96876.csproj
jkotas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
Member
Author
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; | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[]); | ||
| } |
11 changes: 11 additions & 0 deletions
11
src/tests/Regressions/coreclr/GitHub_96876/test96876.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.