From 3eccef8b91e0b25a5a4169135edd0b3c4b5d638b Mon Sep 17 00:00:00 2001 From: Aaron R Robinson Date: Mon, 24 Jun 2024 14:32:31 -0700 Subject: [PATCH] Updates --- docs/design/features/byreflike-generics.md | 40 ++++++++++++++++------ 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/design/features/byreflike-generics.md b/docs/design/features/byreflike-generics.md index bb4f6953ab1f4..e65bbd2723076 100644 --- a/docs/design/features/byreflike-generics.md +++ b/docs/design/features/byreflike-generics.md @@ -142,7 +142,9 @@ With the above IL composition implemented, the following C# describes the follow ```csharp struct S {} +struct S {} ref struct RS {} +ref struct RS {} interface I {} class C {} class C {} @@ -150,17 +152,29 @@ class C {} // Not currently valid C# void M(T t) where T: allows ref struct { - if (t is int i) // Valid - if (t is S vc) // Valid - if (t is RS rs) // Valid - if (t is Span s) // Valid - if (t is string str) // Valid - if (t is I itf) // Valid - if (t is C c) // Valid - if (t is C ci) // Valid - if (t is C cu) // Invalid - if (t is U u) // Invalid - if (t is Span su) // Invalid + // Valid + if (t is int i) + + if (t is S s) + if (t is S sc) + if (t is S su) + + if (t is RS rs) + if (t is RS rsc) + if (t is RS rsu) + + if (t is string str) + if (t is C c) + if (t is C ci) + if (t is C cu) + + // Can be made to work in IL. + if (t is I itf) // A new local "I" would not be used for ByRefLike scenarios. + // The local would be the ByRefLike type, not "I". + + // Invalid + if (t is object o) // ByRefLike types evaluate "true" for object. + if (t is U u) } ``` @@ -179,6 +193,10 @@ namespace System.Runtime.CompilerServices where TTo: allows ref struct; // Replacement for the [box; isinst; unbox.any] sequence. + // Would throw InvalidCastException for invalid use at run-time. + // For example: + // TFrom: RS, TTo: object => always throws + // TFrom: RS, TTo: => always throws public static TTo CastTo(TFrom source) where TFrom: allows ref struct where TTo: allows ref struct;