-
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
Design to support ByRefLike types in Generics #67129
Design to support ByRefLike types in Generics #67129
Conversation
Cc @steveharter |
@lambdageek for mono and @tgani-msft for C++/CLI FYI. |
Record APIs where compiler constraint analysis should be suppressed. Defined analysis suppression attribute.
Co-authored-by: Jared Parsons <[email protected]>
a5c4b95
to
75fd91b
Compare
Any other feedback on this design? |
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.
Are these types importable via the COM metadata APIs? The C++ compiler uses only these.
@tgani-msft As in, can they be acquired through the |
I'll send you a link to the source code directly, via email. |
I think this came up elsewhere (maybe from @teo-tsirpanis), but is there any need to take special effort to make types "conditionally by-ref-like" depending on their type arguments? For example, imagine if we wanted to let people use nullable ref structs. It would be good to allow things like boxing, heap escape, etc. depending on the type arguments. ref struct RS
{
public ref int X;
}
namespace System
{
// somehow we denote here that this is "only a ref struct if T is a ref struct".
public struct Nullable<T> where T : struct allow T : ref struct // or whatever syntax we use
{
T value;
// ...
}
}
void M(RS? maybeRs, ref int X)
{
if (maybeRs != null)
{
maybeRs.Value.X = ref X;
}
object obj = maybeRs; // error
}
void M2(int? maybeInt)
{
object obj = maybeInt; // ok
} It's possible the answer here is no, don't bother doing this, or punt it to something further down the line, but seems worth considering as part of this whole area. |
That seems like a much broader feature given |
The only prior art I can think of for this sort of thing is in Interop when a |
FWIW, there are a few existing places in the compiler where the types of fields after substitution affect usage of a struct. For example: SharpLab. (it sounds like this might be very similar/related to blittability.) struct S<T>
{
public T field;
}
class Program
{
unsafe void M1(S<int>* ptr) { } // ok
unsafe void M2(S<object>* ptr) { } // error
} |
Agreed, but not metadata. For example, the following might be unintuitive from a reflection standpoint: struct S<T> allow T : ref struct
ref struct when T : ref struct
{ }
Console.WriteLine(typeof(S<>).IsByRefLike); // False
Console.WriteLine(typeof(S<int>).IsByRefLike); // False
Console.WriteLine(typeof(S<Span<int>>).IsByRefLike); // True |
How about enabling overload by generic constraints so we can have both |
@AaronRobinsonMSFT, can this be merged? Presumably we're fine updating the design doc as the design evolves and we don't need to block the PR on having 100% of the answers set in stone? |
I think so. It is accurate regarding the current state of support in the runtime.
Agreed. |
Adding in a proposal to support ByRefLike type in Generics. This work is supported by #63768 and contributes toward #65112.
/cc @jkotas @davidwrighton @jaredpar @cston @RikkiGibson @AlekseyTs