Skip to content

generics: indexing operations don't work on pointers to constraints derived from arrays #52802

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

Closed
DeedleFake opened this issue May 9, 2022 · 1 comment

Comments

@DeedleFake
Copy link

What version of Go are you using (go version)?

Playground with v=gotip.

Does this issue reproduce with the latest release?

Yes.

What did you do?

Playground Link

I tried to write a function that could copy between two pointers to types with an underlying array type.

It can be worked around by removing the array constraint and then manually converting when calling the function, such as in this example. This significantly hurts the ergonomics of the usage of the function, however.

What did you expect to see?

Successful compilation.

What did you see instead?

It claimed that range and indexing aren't allowed with the given constraints.

./prog.go:18:17: cannot range over out (variable of type *OA)
./prog.go:19:6: invalid operation: cannot index out (variable of type *OA)
./prog.go:19:16: invalid operation: cannot index in (variable of type *IA)

Go build failed.
@ianlancetaylor
Copy link
Contributor

This is working as expected for the current spec and implementation. Indexing is only supported if the constraint of the type being indexed has a core type (https://go.dev/ref/spec#Core_types). We may try to address this in the future, but it's complicated because we have to think about a dependent type: a generic type that is not one of the type parameters (in your example the index result is one of the type parameters, but in a slightly different example it wouldn't be).

@golang golang locked and limited conversation to collaborators May 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants