-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Methods to support ByRefLike types in "is-type" IL sequences #105435
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices |
public static TTo CastTo<TFrom, TTo>(TFrom source)
where TFrom: allows ref struct
where TTo: allows ref struct; Would it be more helpful to have the same signature as
|
Agree, the
Yes, we need both since we can't return |
I do not think that it works well with |
I meant if we have a single ref TTo TryCastTo<TFrom,TTo>(ref TFrom source); Can't we return a null reference on failure? We have But I see this won't work because...
I see. Because if I have In that case I think these operations need better names. |
Can't we just define the semantics as the Rolsyn team prefers? I don't see why the |
yes, I meant my |
Right. These APIs are specifically designed to support Roslyn pattern matching. The APIs are not meant to be callable by the ordinary user code (nothing prevents that though). The API proposal should mention that. This: U M<T, U>(T t) where T: allows ref struct where U: allows ref struct
{
if (t is U u)
return u;
return default;
} is going to be lower by Roslyn into something like this: U M<T, U>(T t) where T: allows ref struct where U: allows ref struct
{
if (RuntimeHelpers.IsInstanceOf<T,U>(t))
{
U u = RuntimeHelpers.CastTo<T,U>(t);
return u;
}
return default;
} The APIs need to maintain the behavior of the existing C# pattern matching that dictates how they need to behave for non-byref like types. It is why it is not feasible to change the arguments to byrefs (consider |
I agree we may need better names. Are there names we can borrow from C# spec? |
What happened to the "Special IL sequences"? Will they still be supported? Will they be emitted on .NET 9 only? Are they going to be added to the ECMA-335 addendum? Thanks! |
The special IL sequences were not implemented and their specification removed. These APIs are designed to avoid the complexity that we faced in trying to special case these IL sequences and reconcile them with higher level language semantics. |
So, presumably you will not be able to write code that would require such constructs in 9, but will be able to in .NET 10 (using these APIs)? |
Yes, that is the current plan. |
It's already working if you write IL manually: https://godbolt.org/z/rqKP71d6f |
The special IL sequences are invalid IL today. Invalid IL has undefined behavior. It may appear to work, but it is not guaranteed to work. |
Why not include? public static bool TryCastTo<TFrom, TTo>(TFrom source, out TTo destination)
where TFrom: allows ref struct
where TTo: allows ref struct; Doing so would save one check, otherwise |
Background and motivation
Due to the non-boxing requirment of ByRefLike types, it is complicated to express generic IL instructions for methods where the
allows ref struct
constraint is applied, given existing semantics of certain IL instructions. This is particularly limiting when attempting an "is-type" check (that is,x is Y y
). These APIs will be provided and their semantics defined in coordination with the Roslyn team to help us avoid augmenting the definitions of certain IL instruction, in specific sequences - see option two in the below design notes. ECMA-335 doesn't dictate details of .NET APIs, so adding APIs avoids any ECMA-335 augmentation with respect to IL instruction semantics.See further design notes in byreflike-generics.md.
API Proposal
API Usage
This API is not meant to be used directly by user code. It called by Roslyn generated code to perform "is-type" checks involving ByRefLike types.
Instead of the following IL sequences for "is-type":
The following C# will now be possible:
Alternative Designs
The troublesome IL sequences can be identified to support ByRefLike types when detected by the JIT or Interpreter. This requires changing the semantic meaning of some IL instructions, but only in certain sequences and then updating ECMA-335. This was the approach attempted in .NET 9 and has proven to be very difficult to reconcile with the broader ecosystem with the needed language.
Risks
No response
The text was updated successfully, but these errors were encountered: