Skip to content
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

cmd/compile/internal/types2: review implementation of indexing of generic types #49275

Closed
danscales opened this issue Nov 1, 2021 · 3 comments
Labels
NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@danscales
Copy link
Contributor

danscales commented Nov 1, 2021

On go tip (generics), the below program gets a compile error

./test.go:6:15: invalid operation: cannot index x (variable of type T constrained by interface{[]int64|map[int]int64})

Program:

package main

import "fmt"

func g[T interface{ []int64 | map[int]int64 }](x T) int64 {
	return x[3])
}

func main() {
	x := make(map[int]int64)
	y := make([]int64, 4)

	fmt.Printf("%v %v\n", g(x), g(y))
}

That happens even though individually the functions:

func g[T interface{ []int64 }](x T) int64 {
	return x[3]
}

and

func g[T interface{ map[int]int64 }](x T) int64 {
	return x[3]
}

compile/run fine. We need a fix and/or need to decide exactly which of map/slice/array/string types can be combined in a type constraint, and allow indexing on the type param. (And make rules more explicit if we disallow certain cases.)

@griesemer @findleyr

@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels Nov 1, 2021
@griesemer griesemer added this to the Go1.18 milestone Nov 1, 2021
@griesemer
Copy link
Contributor

griesemer commented Nov 1, 2021

The current generic indexing is both too relaxed and not tight enough. While this could be made to work, maps are sufficiently different from all the other indexable types that it gets very quickly extremely complicated. We probably should differentiate between all maps and all something else in the type set for now.

@griesemer griesemer changed the title cmd/compile/internal/types2: should handle indexing for typeparam that is map or slice cmd/compile/internal/types2: review implementation of indexing of generic types Nov 1, 2021
@griesemer
Copy link
Contributor

griesemer commented Nov 2, 2021

Follow-up: This code is working as intended. If we have a map in the the type set, all types must be maps with the same underlying type, otherwise we cannot index.

What is not working correctly (missing tests!) is assignments to indexed generic expression that are not assignable (because e.g. the type set contains a string). Fix forthcoming.

@gopherbot
Copy link

gopherbot commented Nov 2, 2021

Change https://golang.org/cl/360603 mentions this issue: cmd/compile/internal/types2: fix indexing of generic types

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants