IPropertySymbol for array indexer

Topics: APIs
Apr 29, 2014 at 2:33 PM
Edited Apr 30, 2014 at 8:08 PM
If I have code like this:
var arr = new int[10];
arr[0] = 1;
var length = arr.Length;
The symbol for the SimpleMemberAccessExpression of the 'Length' property correctly returns an IPropertySymbol representing that property. But no symbol is returned for the ElementAccessExpression representing 'arr[0]'. Please add IPropertySymbols for arrays representing their indexers.
Apr 29, 2014 at 5:09 PM
I don't see how you could get a IPropertySymbol for that, arrays do not have indexers.
Apr 29, 2014 at 5:48 PM
Edited Apr 29, 2014 at 5:51 PM
Well maybe not an IPropertySymbol then, but something that represents the semantics of an array element access. I understand that there are certain differences between an array element access and an access of a custom indexer, in that the former uses no actual property and can be used like a variable. However, it is not unreasonable to want to treat this access as if it were an indexer usage. There could be a special implementation of IPropertySymbol created for this scenario with implementations like these:
  • IsIndexer - True
  • Name - this[]
  • Type - The element type of the array.
  • Parameters - a collection of int parameters whose size is the arity of the array
  • GetMethod - A similarly generated IMethodSymbol representing the get access semantics of the array access
  • SetMethod - A similarly generated IMethodSymbol representing the set access semantics of the array access
Also, arrays implement the IList and IList<element_type> interfaces, which require that an indexer be defined, so I assume under the covers there is some sort of entity satisfying those requirements. And not having this symbol has the other problem that when you call FindImplementationForInterfaceMember with the indexer symbol from the IList<element_type> interface, you get back a null reference.
Apr 29, 2014 at 7:18 PM
I understand that there are certain differences between an array element access and an access of a custom indexer
Yes but there's more to it. To be able to model an array access with a property you need something that C# doesn't support, ref return. The property you're asking for needs to look something like:
ref int this[int index] { get; }
Besides, what kind of information would such a symbol provide that isn't already available from GetTypeInfo/GetSymbolInfo?
arrays implement the IList and IList<element_type> interfaces, which require that an indexer be defined
That's completely unrelated, those indexers are not involved in anyway in array accesses. They're explicit implementation so they're only accessible through those interfaces.
And not having this symbol has the other problem that when you call FindImplementationForInterfaceMember with the indexer symbol from the IList<element_type> interface, you get back a null reference.
That's also unrelated, the implementation of IList<element_type> doesn't exist, it is magically provided by the runtime.
Apr 29, 2014 at 7:27 PM
Edited Apr 29, 2014 at 7:30 PM
Well if the runtime magically provides something, why can't Roslyn?

As for the return type issue, perhaps the IPropertySymbol can expose more information so the type can be differentiated from a normal property's type. Or maybe some other ISymbol can be returned for this type of expression.

The kind of information I'm looking for is the semantics of that ElementAccessExpression in my original example. Since I get back nothing, I now need to special case my logic to see what the Expression of the ElementAccessExpression is, get its type symbol, see if its an IArrayTypeSymbol, check what the arity and element types are, ...

It would be much nicer if you always got back a valid ISymbol for an ElementAccessExpression (when the code has no errors).