Skip to content

cmd/compile: cannot index pointer to generic array #49447

@josharian

Description

@josharian

This does not compile:

package p

type a1 [16]byte
type a2 [32]byte

type A interface {
	a1 | a2
}

func elem0[T A](t *T) *byte {
	return &t[0]
}

Error: ./x.go:11:10: invalid operation: cannot index t (variable of type *T)

This shows up in practice if you want to embed an array in a generic struct to avoid extra indirection/allocs, like this:

type S struct {
  // other fields
  arr A
}

but pass pointers to the array around internally to avoid making lots of copies of it (particularly as the array gets large).

The equivalent non-generic code compiles:

func elem0concrete(t *a1) *byte {
	return &t[0]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions