-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Covariant Returns Feature #35308
Covariant Returns Feature #35308
Changes from 1 commit
bf7501f
fef49b2
f10119d
8d5d87e
a14af6f
0e4e472
698eafb
6848c40
5a85da8
89bb5c3
cbecf78
1154b90
f97b9de
1329fea
997074f
ed9fb4c
6eaaa1a
688c263
3636d03
c05f075
7ccd3ed
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 |
---|---|---|
|
@@ -20,6 +20,31 @@ public static bool CanCastTo(this TypeDesc thisType, TypeDesc otherType) | |
return thisType.CanCastToInternal(otherType, null); | ||
} | ||
|
||
public static bool IsCompatibleWith(this TypeDesc thisType, TypeDesc otherType) | ||
{ | ||
// Structs can be cast to the interfaces they implement, but they are not compatible according to ECMA I.8.7.1 | ||
bool isCastFromValueTypeToReferenceType = otherType.IsValueType && !thisType.IsValueType; | ||
if (isCastFromValueTypeToReferenceType) | ||
{ | ||
return false; | ||
} | ||
|
||
// Nullable<T> can be cast to T, but this is not compatible according to ECMA I.8.7.1 | ||
bool isCastFromNullableOfTtoT = thisType.IsNullable && otherType.IsEquivalentTo(thisType.Instantiation[0]); | ||
if (isCastFromNullableOfTtoT) | ||
{ | ||
return false; | ||
} | ||
|
||
return otherType.CanCastTo(thisType); | ||
} | ||
|
||
private static bool IsEquivalentTo(this TypeDesc thisType, TypeDesc otherType) | ||
{ | ||
// TODO: Once type equivalence is implemented, this implementation needs to be enhanced to honor it. | ||
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. Is type equivalence used for anything outside COM? We don't intend to model COM in the managed type system so if this is exclusive to COM, the use of this can be replaced with 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. I think that it is used just by COM, @davidwrighton can you please confirm that? |
||
return thisType.Equals(otherType); | ||
} | ||
|
||
private static bool CanCastToInternal(this TypeDesc thisType, TypeDesc otherType, StackOverflowProtect protect) | ||
{ | ||
if (thisType == otherType) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still going to throw if thisType or otherType is a pointer or function pointer. Do we need to handle that? (I had that feedback in #35308 (comment) but it probably got lost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, let me add those to the test suite. It seems pointers cannot be compatible-with each other unless they are the same. I am actually not sure if the function pointers correspond to "method-signature compatible-with" case in the ECMA spec.