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: error in Go 1.23.0 with generics, type aliases and indexing #68903

Closed
phynes-sensiblecode opened this issue Aug 15, 2024 · 7 comments
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@phynes-sensiblecode
Copy link

Go version

go1.23.0

Output of go env in your module/workspace:

Can be seen in Go Playground

What did you do?

package main

import "fmt"

type A = map[string]interface{}

func m[T ~A](a interface{}) {
	fmt.Println(a.(T)["key"])
}

func main() {
	b := A{"key": "value"}
	m[A](b)
}

See: https://go.dev/play/p/cFkeJ6ppEBk

What did you see happen?

This compiler error is seen on Go 1.23:

./prog.go:8:19: invalid operation: cannot index a.(T) (comma, ok expression of type T constrained by ~A)

The program compiles and runs as expected on Go 1.22.

If we move the type definition of A to a different package then the code runs fine in Go 1.23:
https://go.dev/play/p/l_x4F190M3y

What did you expect to see?

There should be no compilation error.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 15, 2024
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor
Copy link
Contributor

@gopherbot Please open a backport to the 1.23 release.

This appears to be a regression in the compiler.

@gopherbot
Copy link
Contributor

Backport issue(s) opened: #68905 (for 1.23).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

@timothy-king
Copy link
Contributor

Thank you for the report.

This looks like a bug in (go/types._TypeSet).underIs (and types2). This internal comment // x == under(x) for ~x terms is not true for Aliases. Needs more investigation.

You can get the 1.22 behavior by overriding the 1.23 default GODEBUG=gotypesalias=0. https://go.dev/doc/godebug#default has instructions for different ways to change the default value.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/606075 mentions this issue: go/types: unalias tilde terms in underIs

@dmitshur dmitshur added this to the Go1.24 milestone Aug 16, 2024
@dmitshur dmitshur added the NeedsFix The path to resolution is known, but the work has not been done. label Aug 16, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/607635 mentions this issue: [release-branch.go1.23] go/types, types2: unalias tilde terms in underIs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants