Repository navigation
Answer generic variance and parameter attributes in reflection as .NET does #593
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
Open
jonathanperis
wants to merge
4
commits into
main
Choose a base branch
from
fix/reflection-variance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+248
−17
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4d378d2
Make an enum nested in a generic type one type per instantiation
jonathanperis 6f19ea6
Answer generic variance and parameter attributes in reflection as .NE…
jonathanperis 445be89
Merge remote-tracking branch 'origin/main' into fix/nested-generic-enum
jonathanperis 9eacdcc
Merge branch 'fix/nested-generic-enum' into fix/reflection-variance
jonathanperis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Type.IsAssignableFrom / IsAssignableTo / IsInstanceOfType / IsSubclassOf over generic variance: interfaces and delegates of the | ||
| // program at top level, nested in a class and in a generic class, with 1, 3, 16 and 17 type parameters, framework ones, nested | ||
| // variance, value-type arguments (no variance), and constructed types that appear only in typeof. Also | ||
| // GenericParameterAttributes and positions. | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| interface IVar<out A, in B, C> { } | ||
| class VarC : IVar<string, object, int> { } | ||
| class Outer | ||
| { | ||
| public interface INested<out T, in U> { } | ||
| public class NC : INested<string, object> { } | ||
| public delegate R NestedD<in T, out R>(T t); | ||
| } | ||
| class GOuter<T> | ||
| { | ||
| public interface IInner<out U> { } | ||
| public class Impl : IInner<string> { } | ||
| public delegate void D<in U>(U u); | ||
| } | ||
| interface I16<in A1, in A2, in A3, in A4, in A5, in A6, in A7, in A8, in A9, in A10, in A11, in A12, in A13, in A14, in A15, in A16> { } | ||
| class C16 : I16<object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object> { } | ||
| delegate R D17<in A1, in A2, in A3, in A4, in A5, in A6, in A7, in A8, in A9, in A10, in A11, in A12, in A13, in A14, in A15, in A16, out R>(); | ||
| interface I18<A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, out A18> { } | ||
| class C18 : I18<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, string> { } | ||
| class Con<A, B, C, D, E, F> where A : class, IDisposable, new() where B : unmanaged where C : VarC, IComparable<C> where D : A where E : notnull where F : struct, Enum { } | ||
| ref struct Stack<T> where T : allows ref struct { } | ||
|
|
||
| class Program | ||
| { | ||
| static void Rel(string name, Type to, Type from, object? instance = null) => | ||
| Console.WriteLine($"{name}: {to.IsAssignableFrom(from)} {from.IsAssignableTo(to)} {from.IsAssignableFrom(to)} {from.IsSubclassOf(to)} {to.IsInstanceOfType(instance)}"); | ||
|
|
||
| static void Parameters(Type definition) | ||
| { | ||
| Console.WriteLine(definition.Name + ": " + string.Join(" ", definition.GetGenericArguments().Select(a => | ||
| a.Name + "=" + (int)a.GenericParameterAttributes + "@" + a.GenericParameterPosition + (a.DeclaringType == definition ? "" : "!")))); | ||
| } | ||
|
|
||
| static void Main() | ||
| { | ||
| Rel("top", typeof(IVar<object, string, int>), typeof(VarC), new VarC()); | ||
| Rel("top invariant", typeof(IVar<object, string, object>), typeof(VarC), new VarC()); | ||
| Rel("top interface", typeof(IVar<object, string, int>), typeof(IVar<string, object, int>)); | ||
| Rel("nested", typeof(Outer.INested<object, string>), typeof(Outer.NC), new Outer.NC()); | ||
| Rel("nested wrong", typeof(Outer.INested<Uri, string>), typeof(Outer.NC), new Outer.NC()); | ||
| Rel("nested delegate", typeof(Outer.NestedD<string, object>), typeof(Outer.NestedD<object, string>), (Outer.NestedD<object, string>)(_ => "")); | ||
| Rel("in generic", typeof(GOuter<int>.IInner<object>), typeof(GOuter<int>.Impl), new GOuter<int>.Impl()); | ||
| Rel("in other generic", typeof(GOuter<long>.IInner<object>), typeof(GOuter<int>.Impl), new GOuter<int>.Impl()); | ||
| Rel("in generic invariant outer", typeof(GOuter<object>.IInner<object>), typeof(GOuter<string>.IInner<object>)); | ||
| Rel("in generic delegate", typeof(GOuter<int>.D<string>), typeof(GOuter<int>.D<object>)); | ||
| Rel("16", typeof(I16<string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string>), typeof(C16), new C16()); | ||
| Rel("17", typeof(D17<object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object>), | ||
| typeof(D17<object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, object, string>)); | ||
| Rel("18", typeof(I18<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, object>), typeof(C18), new C18()); | ||
| Rel("18 invariant", typeof(I18<int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, int, long, object>), typeof(C18), new C18()); | ||
| Rel("framework", typeof(IEnumerable<object>), typeof(List<string>), new List<string>()); | ||
| Rel("value argument", typeof(IEnumerable<object>), typeof(List<int>), new List<int>()); | ||
| Rel("nested variance", typeof(IEnumerable<IEnumerable<object>>), typeof(List<List<string>>), new List<List<string>>()); | ||
| Rel("Func", typeof(Func<string, object>), typeof(Func<object, string>), (Func<object, string>)(_ => "")); | ||
| Rel("Func value", typeof(Func<object>), typeof(Func<int>), (Func<int>)(() => 1)); | ||
| Rel("definition", typeof(IVar<,,>), typeof(VarC)); | ||
|
|
||
| Parameters(typeof(IVar<,,>)); | ||
| Parameters(typeof(Outer.INested<,>)); | ||
| Parameters(typeof(GOuter<>.IInner<>)); | ||
| Parameters(typeof(GOuter<>)); | ||
| Parameters(typeof(D17<,,,,,,,,,,,,,,,,>)); | ||
| Parameters(typeof(Con<,,,,,>)); | ||
| Parameters(typeof(Stack<>)); | ||
| Parameters(typeof(IEnumerable<>)); | ||
| Parameters(typeof(Func<,>)); | ||
| Parameters(typeof(Dictionary<,>)); | ||
| Parameters(typeof(Nullable<>)); | ||
| Parameters(typeof(IComparable<>)); | ||
| Parameters(typeof(Lazy<>)); | ||
| Parameters(typeof(WeakReference<>)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| top: True True False False True | ||
| top invariant: False False False False False | ||
| top interface: True True False False False | ||
| nested: True True False False True | ||
| nested wrong: False False False False False | ||
| nested delegate: True True False False True | ||
| in generic: True True False False True | ||
| in other generic: False False False False False | ||
| in generic invariant outer: False False False False False | ||
| in generic delegate: True True False False False | ||
| 16: True True False False True | ||
| 17: True True False False False | ||
| 18: True True False False True | ||
| 18 invariant: False False False False False | ||
| framework: True True False False True | ||
| value argument: False False False False False | ||
| nested variance: True True False False True | ||
| Func: True True False False True | ||
| Func value: False False False False False | ||
| definition: False False False False False | ||
| IVar`3: A=1@0 B=2@1 C=0@2 | ||
| INested`2: T=1@0 U=2@1 | ||
| IInner`1: T=0@0 U=1@1 | ||
| GOuter`1: T=0@0 | ||
| D17`17: A1=2@0 A2=2@1 A3=2@2 A4=2@3 A5=2@4 A6=2@5 A7=2@6 A8=2@7 A9=2@8 A10=2@9 A11=2@10 A12=2@11 A13=2@12 A14=2@13 A15=2@14 A16=2@15 R=1@16 | ||
| Con`6: A=20@0 B=24@1 C=0@2 D=0@3 E=0@4 F=24@5 | ||
| Stack`1: T=32@0 | ||
| IEnumerable`1: T=33@0 | ||
| Func`2: T=34@0 TResult=33@1 | ||
| Dictionary`2: TKey=0@0 TValue=0@1 | ||
| Nullable`1: T=24@0 | ||
| IComparable`1: T=34@0 | ||
| Lazy`1: T=0@0 | ||
| WeakReference`1: T=4@0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Enclosing variance flips after sixteen
Medium Severity
VarianceBitspacks only a nested type's own parameters and leaves enclosing arguments invariant. Past the 16th argument,parameter_varianceinstead uses that chain slot's real variance, including an enclosinginoroutparameter.IsAssignableFromthen treats the same enclosing parameter differently on either side of the boundary.Additional Locations (2)
src/Csharpc/CodeGen/ModuleCodeGen.TypeInfo.cs#L725-L731src/runtime/type.c#L531-L532Reviewed by Cursor Bugbot for commit 6f19ea6. Configure here.