-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Handle canonical types in casting logic #127146
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c74f22b
Make constraint checking canon-aware for type constraints
MichalStrehovsky bb84090
Move canon awareness from constraint helpers into CastingHelper
MichalStrehovsky 3fe978a
Handle parameterized types in canonical type arg matching
MichalStrehovsky 6f5cf18
Add regression test for MakeGenericType with constrained canonicals
MichalStrehovsky 8289ab1
Apply suggestions from code review
MichalStrehovsky 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
93 changes: 93 additions & 0 deletions
93
src/coreclr/tools/Common/TypeSystem/Canon/CastingHelper.Canon.cs
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,93 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Internal.TypeSystem | ||
| { | ||
| public static partial class CastingHelper | ||
| { | ||
| /// <summary> | ||
| /// Check if <paramref name="otherType"/> is a canonical type that <paramref name="thisType"/> | ||
| /// can be cast to. __Canon accepts any reference type; __UniversalCanon accepts any type. | ||
| /// Pointers, byrefs, and function pointers are not valid instantiation arguments. | ||
| /// </summary> | ||
| private static bool IsCanonicalCastTarget(TypeDesc thisType, TypeDesc otherType) | ||
| { | ||
| TypeSystemContext context = thisType.Context; | ||
|
|
||
| if (context.IsCanonicalDefinitionType(otherType, CanonicalFormKind.Universal)) | ||
| return true; | ||
|
|
||
| if (context.IsCanonicalDefinitionType(otherType, CanonicalFormKind.Specific)) | ||
| return thisType.IsGCPointer; | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Check if two type arguments can be considered matching because one (or both) is canonical. | ||
| /// __Canon matches any reference type; __UniversalCanon matches any type. | ||
| /// </summary> | ||
| private static bool IsCanonicalTypeArgMatch(TypeDesc type, TypeDesc otherType) | ||
| { | ||
| TypeSystemContext context = type.Context; | ||
|
|
||
| if (context.IsCanonicalDefinitionType(otherType, CanonicalFormKind.Universal)) | ||
| return true; | ||
|
|
||
| if (context.IsCanonicalDefinitionType(otherType, CanonicalFormKind.Specific)) | ||
| return type.IsGCPointer || context.IsCanonicalDefinitionType(type, CanonicalFormKind.Any); | ||
|
|
||
| if (context.IsCanonicalDefinitionType(type, CanonicalFormKind.Universal)) | ||
| return true; | ||
|
|
||
| if (context.IsCanonicalDefinitionType(type, CanonicalFormKind.Specific)) | ||
| return otherType.IsGCPointer || context.IsCanonicalDefinitionType(otherType, CanonicalFormKind.Any); | ||
|
|
||
| // For non-leaf types (e.g., Arg2<string> vs Arg2<__Canon>), check if they are | ||
| // canon-equivalent: same type definition with canon-compatible type arguments. | ||
| if (IsCanonEquivalent(type, otherType)) | ||
| return true; | ||
|
|
||
| // For parameterized types like arrays (string[] vs __Canon[]), | ||
| // recursively match the element/parameter type. | ||
| if (type is ParameterizedType paramType && otherType is ParameterizedType otherParamType | ||
| && type.Category == otherType.Category) | ||
| { | ||
| if (type is ArrayType arrayType && otherType is ArrayType otherArrayType | ||
| && arrayType.Rank != otherArrayType.Rank) | ||
| return false; | ||
|
|
||
| return IsCanonicalTypeArgMatch(paramType.ParameterType, otherParamType.ParameterType); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Check if two types are equivalent considering canonical type matching rules. | ||
| /// Same type definition with all type arguments either equal or canon-compatible. | ||
| /// </summary> | ||
| private static bool IsCanonEquivalent(TypeDesc thisType, TypeDesc otherType) | ||
| { | ||
| if (!thisType.HasSameTypeDefinition(otherType)) | ||
| return false; | ||
|
|
||
| Instantiation thisInst = thisType.Instantiation; | ||
| Instantiation otherInst = otherType.Instantiation; | ||
|
|
||
| if (thisInst.Length == 0) | ||
| return false; | ||
|
|
||
| for (int i = 0; i < thisInst.Length; i++) | ||
| { | ||
| if (thisInst[i] == otherInst[i]) | ||
| continue; | ||
|
|
||
| if (!IsCanonicalTypeArgMatch(thisInst[i], otherInst[i])) | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/coreclr/tools/Common/TypeSystem/Canon/TypeSystemConstraintsHelpers.Canon.cs
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,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.Diagnostics; | ||
|
|
||
| namespace Internal.TypeSystem | ||
| { | ||
| public static partial class TypeSystemConstraintsHelpers | ||
| { | ||
| private static bool IsSpecialTypeMeetingConstraint(TypeDesc type, GenericConstraints constraint) | ||
| { | ||
| TypeSystemContext context = type.Context; | ||
|
|
||
| return constraint switch | ||
| { | ||
| GenericConstraints.ReferenceTypeConstraint => context.IsCanonicalDefinitionType(type, CanonicalFormKind.Any), | ||
| GenericConstraints.DefaultConstructorConstraint => context.IsCanonicalDefinitionType(type, CanonicalFormKind.Any), | ||
| GenericConstraints.NotNullableValueTypeConstraint => context.IsCanonicalDefinitionType(type, CanonicalFormKind.Universal), | ||
| _ => throw new UnreachableException() | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if <paramref name="instantiationParam"/> can satisfy the type constraint | ||
| /// <paramref name="instantiatedConstraintType"/> when the param or constraint IS a canonical | ||
| /// definition type (__Canon or __UniversalCanon). Handles wildcard semantics only; | ||
| /// structural matching (interface walking, base chain, variance) is in CastingHelper. | ||
| /// </summary> | ||
| private static bool CanCastToConstraintWithCanon(TypeDesc instantiationParam, TypeDesc instantiatedConstraintType) | ||
| { | ||
| TypeSystemContext context = instantiationParam.Context; | ||
|
|
||
| // If the instantiation param is a canonical definition type (__Canon or __UniversalCanon), | ||
| // it acts as a wildcard — any concrete type substituted at runtime will be validated then. | ||
| if (context.IsCanonicalDefinitionType(instantiationParam, CanonicalFormKind.Any)) | ||
| return true; | ||
|
|
||
| // If the constraint type itself is a canonical definition type, check compatibility directly. | ||
| // E.g., "where T : U" with U=__Canon means T must be a plausible match for __Canon. | ||
| if (context.IsCanonicalDefinitionType(instantiatedConstraintType, CanonicalFormKind.Universal)) | ||
| return true; | ||
| if (context.IsCanonicalDefinitionType(instantiatedConstraintType, CanonicalFormKind.Specific)) | ||
| return instantiationParam.IsGCPointer; | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/coreclr/tools/Common/TypeSystem/Common/CastingHelper.NonCanon.cs
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,17 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Internal.TypeSystem | ||
| { | ||
| public static partial class CastingHelper | ||
| { | ||
| private static bool IsCanonicalCastTarget(TypeDesc thisType, TypeDesc otherType) | ||
| => false; | ||
|
|
||
| private static bool IsCanonicalTypeArgMatch(TypeDesc type, TypeDesc otherType) | ||
| => false; | ||
|
|
||
| private static bool IsCanonEquivalent(TypeDesc thisType, TypeDesc otherType) | ||
| => false; | ||
| } | ||
| } |
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
16 changes: 16 additions & 0 deletions
16
src/coreclr/tools/Common/TypeSystem/Common/TypeSystemConstraintsHelpers.NonCanon.cs
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,16 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Diagnostics; | ||
|
|
||
|
MichalStrehovsky marked this conversation as resolved.
|
||
| namespace Internal.TypeSystem | ||
| { | ||
| public static partial class TypeSystemConstraintsHelpers | ||
| { | ||
| private static bool IsSpecialTypeMeetingConstraint(TypeDesc type, GenericConstraints constraint) | ||
| => false; | ||
|
|
||
| private static bool CanCastToConstraintWithCanon(TypeDesc instantiationParam, TypeDesc instantiatedConstraintType) | ||
| => false; | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.