type returned by reflect.ArrayOf breaks the spec

69 views
Skip to first unread message

Josh Humphries

unread,
Nov 6, 2017, 5:51:49 PM11/6/17
to golang-nuts
I think I've found a bug regarding reflect.ArrayOf. The go doc is a bit light, but suggests that the resulting type is a proper array type. However, I've found that the resulting type has a few flaws:

  1. The resulting array type violates part of the language spec about arrays: it cannot be used as a map key. Using an array whose type was created via reflect.ArrayOf as a key in a map results in a runtime panic:

         runtime error: hash of unhashable type [8]int32

    See the link below for an example program that shows this error.

  2. The resulting array type seems to violate the reflect package Go doc regarding reflect.Type and type equality. The package doc states this:

        Type values are comparable, such as with the == operator. Two Type values are equal if they represent identical types.

    And the section about type identity in the language spec states this:

        Two array types are identical if they have identical element types and the same array length.

    However, an array type created using reflect.ArrayOf returns false when compared to an identical type that was not created using reflect.ArrayOf.

    The link below also shows an example of this erroneous behavior.
Demonstrations of these issues:

I am guessing I should just file a bug in a Github issue, but wanted to first check to see if this, somehow, might be expected behavior. (And if so, to learn why.)

----
Josh Humphries
jh...@bluegosling.com

Ian Lance Taylor

unread,
Nov 6, 2017, 5:57:18 PM11/6/17
to Josh Humphries, golang-nuts
This is not expected behavior. Oddly, I can not recreate the problem.
I mean, I see that it happens on the playground, but it seems to work
correctly when I run the program on my laptop. Ah, but it fails, at
least on tip, if I use `GOARCH=386`. Please do open an issue.
Thanks.

Ian
Reply all
Reply to author
Forward
0 new messages