Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Implement CanCastTo - #1183

Merged
MichalStrehovsky merged 8 commits into
dotnet:masterfrom
MichalStrehovsky:canCastTo
Apr 23, 2016
Merged

MichalStrehovsky merged 8 commits into
dotnet:masterfrom
MichalStrehovsky:canCastTo

Conversation

@MichalStrehovsky

@MichalStrehovsky MichalStrehovsky commented Apr 21, 2016 •

Copy link
Copy Markdown
Member

Type system support for answering questions about whether a type
can be cast to another type, along with unit tests.

I largely ported the C++ code from the Project N compiler to C#. The only
divergence is in the handling of casting arrays to System.Array and array
interfaces. We won't use the "treat it as Array<T>" wart in CoreRT.
The unit tests are all new because there were none.

I'm also adding a few convenience properties to the type system, stolen
from the Project N compiler type system:

  • Added SzArray flag to TypeFlags
  • A few new HasXXX and IsXXX shortcuts on GenericParameterDesc. Makes code much more readable.
  • IsGenericParameter and IsDefType on TypeDesc. This brings the existing IsArray, IsPointer etc. to completion.
  • HasVariance on TypeDesc. This is a cached answer to whether a type has variance. It lets us quickly decide if we need to do the slow things.

Type system support for answering questions about whether a type
can be cast to another type, along with unit tests.

I largely ported the C++ code from the Project N compiler to C#. The only
divergence is in the handling of casting arrays to System.Array and array
interfaces. We won't use the "treat it as `Array<T>`" wart in CoreRT.
The unit tests are all new because there were none.

I'm also adding a few convenience properties to the type system, stolen
from the Project N compiler type system:

* `Variety` - an enum on `TypeDesc` that can easily tell what kind of type it is (Array? DefType? Generic parameter?...)
* A few new `HasXXX` and `IsXXX` shortcuts on `GenericParameterDesc`. Makes code much more readable.
* `IsGenericParameter` and `IsDefType` on `TypeDesc`. This brings the existing `IsArray`, `IsPointer` etc. to completion.
* `HasVariance` on `TypeDesc`. This is a cached answer to whether a type has variance. It lets us quickly decide if we need to do the slow things.
@MichalStrehovsky

Copy link
Copy Markdown
Member Author

@davidwrighton PTAL

break;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you pull this loop into a ComputeHasVariance function that is virtual? I believe for InstantiatedType it should return GetTypeDefinition().HasVariance, and the implementation on TypeDesc it should contain the code here.

@davidwrighton

Copy link
Copy Markdown
Member

Other than the one comment, looks good to me

@jkotas

jkotas commented Apr 22, 2016

Copy link
Copy Markdown
Member

I wish used the same structure as the other type cast implementations in the repo (src\System.Private.Reflection.Execution\src\Internal\Reflection\Execution\TypeLoader\TypeCast.cs and src\Runtime.Base\src\System\Runtime\TypeCast.cs), but I guess it is too late to ask now that you have done already... .

@jkotas

jkotas commented Apr 22, 2016

Copy link
Copy Markdown
Member

You can delete Common\src\TypeSystem\Common\TypeCast.cs

@manu-st

manu-st commented Apr 22, 2016

Copy link
Copy Markdown

This is a side note but I've seen this pattern used so much in the CoreRT C# code that I'd like to know if this is a conscious decision as in my opinion it hurts the ease of reading and understanding of the code.

The issue is the extensive use of extension methods and partial classes. I can understand somewhat the use of partial class due to way things are built and shared. However, for extension methods I really do not understand. When I look at a type in a text editor, I really want to see all the available methods and not to have to look everywhere in the source base. Extensions methods were introduced as a way to augment types that were sealed from the client point of view, but here we are in full control of the type systems and we can easily add methods to type.

In this particular case, it seems that we could benefit from dynamic binding instead of if / switch statements to pick up the right implementation of the CanCast... method.

Sorry for the digression.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

I wish used the same structure as the other type cast implementations in the repo

I looked at that, but it feels like the compiler's needs for type casting are slightly different than the runtime needs (e.g. the handling of GenericParameterDesc, or a RuntimeDeterminedType that will likely show up in the future). Aligning with NUTC seems to make sense.

@MichalStrehovsky

Copy link
Copy Markdown
Member Author

@manu-silicon Both extension methods and partial classes serve the same purpose - provide extensibility points and an a la carte access to various type system features. This is a conscious decision and a design goal.

I would say IntelliSense does a pretty good job helping with discoverability. For the "I want to see all the methods on this type in a single file" argument, I can counter with "I want to know how casting works without having to hunt down virtual overrides in 5 different files"...

@jkotas

jkotas commented Apr 22, 2016

Copy link
Copy Markdown
Member

the handling of GenericParameterDesc , or a RuntimeDeterminedType

The overall structure of the algorithm (names of methods, shape of the code, etc.) does not need to be different to handle these extra cases ... they are just extra paths in a few spots.

(I am ok with what you got ... it is just that I had to chase bugs in casting algorithm implementations number of times, and I always scratched my head on why each implementation has to always be so different.)

@manu-st

manu-st commented Apr 22, 2016

Copy link
Copy Markdown

I understand the argument of putting stuff together, however, I believe that it goes against basic OO design principles where dynamic dispatch is replaced by a sequence of if/switch because of the usage of extension methods. VS IntelliSense helps a bit to understand the final end result design but the tooling is still not perfect at giving users a full view of the OO hierarchy and classes interactions.

Anyway this was off topic. Thanks for your reply.

[Fact]
public void TestArrayCasting()
{
TypeDesc intType = _context.GetWellKnownType(WellKnownType.Int32);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you add some test for casting between array of int to array of uint and vice-versa?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See TestSameSizeArrayTypeCasting above :)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

damn me. :-)

@jkotas

jkotas commented Apr 22, 2016

Copy link
Copy Markdown
Member

LGTM otherwise

* Get rid of Variety
* Introduce TypeFlag.SzArray (and fix up places that need to handle it)
}
}

public bool IsReferenceType

@jkotas jkotas Apr 22, 2016 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ECMA 335 has defined meaning for "reference type" that is different from this one (it even includes unmanaged pointers). IsObjRef or something similar would be better here. Also, I think it should return true for interfaces as well.

@jkotas

jkotas commented Apr 22, 2016

Copy link
Copy Markdown
Member

LGTM otherwise

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants