@@ -76,7 +76,7 @@ public nanoGenericParamTable(
76
76
{
77
77
foreach ( var gp in items )
78
78
{
79
- var methodWithGenericParam = _context . MethodDefinitionTable . Items . SingleOrDefault ( m => m . GenericParameters . Contains ( gp ) ) ;
79
+ MethodDefinition methodWithGenericParam = _context . MethodDefinitionTable . Items . SingleOrDefault ( m => m . GenericParameters . Contains ( gp ) ) ;
80
80
81
81
if ( methodWithGenericParam != null )
82
82
{
@@ -85,27 +85,63 @@ public nanoGenericParamTable(
85
85
mr => mr . DeclaringType . GetElementType ( ) == methodWithGenericParam . DeclaringType &&
86
86
mr . Name == methodWithGenericParam . Name ) as GenericInstanceMethod ;
87
87
88
- Debug . Assert ( instanceMethod != null , $ "Couldn't find a method specification for type { methodWithGenericParam . DeclaringType } when processing generic parameter { gp } .") ;
88
+ Debug . Assert (
89
+ instanceMethod != null ,
90
+ $ "Couldn't find a method specification for type { methodWithGenericParam . DeclaringType } when processing generic parameter { gp } .") ;
89
91
90
- _typeForGenericParam . Add ( gp , instanceMethod . GenericArguments . ElementAt ( gp . Position ) ) ;
92
+ _typeForGenericParam . Add (
93
+ gp ,
94
+ instanceMethod . GenericArguments . ElementAt ( gp . Position ) ) ;
91
95
}
92
96
else
93
97
{
94
- var typeWithGenericParam = _context . TypeDefinitionTable . Items . SingleOrDefault ( t => t . GenericParameters . Contains ( gp ) ) ;
98
+ TypeDefinition typeWithGenericParam = _context . TypeDefinitionTable . Items . SingleOrDefault ( t => t . GenericParameters . Contains ( gp ) ) ;
95
99
96
100
if ( typeWithGenericParam != null )
97
101
{
98
102
if ( _context . MethodReferencesTable . Items . Any ( ) )
99
103
{
100
- var genericInstance = _context . MethodReferencesTable . Items . FirstOrDefault (
101
- mr => mr . DeclaringType . GetElementType ( ) == typeWithGenericParam )
102
- . DeclaringType as GenericInstanceType ;
103
- Debug . Assert ( genericInstance != null , $ "Couldn't find a method reference for type { typeWithGenericParam } when processing generic parameter { gp } .") ;
104
-
105
- _typeForGenericParam . Add ( gp , genericInstance . GenericArguments . ElementAt ( gp . Position ) ) ;
104
+ // try to find an existing GenericInstanceType in the TypeReferencesTable
105
+ GenericInstanceType genericInstance = _context . TypeReferencesTable . Items
106
+ . OfType < GenericInstanceType > ( )
107
+ . FirstOrDefault ( gt => gt . ElementType == typeWithGenericParam ) ;
108
+
109
+ // fallback to whatever TryGetTypeSpecification gives us
110
+ if ( genericInstance == null )
111
+ {
112
+ TypeReference spec = _context . TypeSpecificationsTable . TryGetTypeSpecification ( typeWithGenericParam . MetadataToken ) ;
113
+
114
+ // If it really is an instance, grab its args...
115
+ if ( spec is GenericInstanceType genericInstanceType )
116
+ {
117
+ genericInstance = genericInstanceType ;
118
+ }
119
+ else
120
+ {
121
+ // ... otherwise it's just an open generic definition (unbound T),
122
+ // so our "argument" for gp is gp itself:
123
+ _typeForGenericParam . Add ( gp , gp ) ;
124
+
125
+ // done here
126
+ continue ;
127
+ }
128
+ }
129
+
130
+ // at this point we know we've got a GenericInstanceType,
131
+ // so pull out the actual generic‐argument at position gp.Position:
132
+ Debug . Assert (
133
+ genericInstance != null ,
134
+ $ "Couldn't find a generic instance for type { typeWithGenericParam } when processing generic parameter { gp } ."
135
+ ) ;
136
+
137
+ _typeForGenericParam . Add (
138
+ gp ,
139
+ genericInstance . GenericArguments . ElementAt ( gp . Position )
140
+ ) ;
106
141
}
107
142
else
108
143
{
144
+ // No methods in scope → no instantiation possible
109
145
_typeForGenericParam . Add ( gp , null ) ;
110
146
}
111
147
}
0 commit comments