Compiler error in Go 1.23 with generics, type aliases and indexing

443 views
Skip to first unread message

Peter Hynes

unread,
Aug 15, 2024, 12:59:33 PM8/15/24
to golang-nuts
Hi community,

We have some code that works fine in Go 1.22, but throws a compiler error in Go 1.23.

We've recreated the issue with a simple example:

```
package main

import (
"fmt"
)

type A = []string

func m[T ~A](a T) {
fmt.Println(a[0])
}

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

}
```

This runs fine in Go 1.22, but throws this error in Go 1.23:
./prog.go:10:15: invalid operation: cannot index a (variable of type T constrained by ~A)

We see something similar with maps.

Is this a compiler bug?

We've noticed that if we move the type definition of A to a package then it's fine in Go 1.23. Here's an example in Go playground: 
https://go.dev/play/p/l_x4F190M3y

Thanks,

Peter

Ian Lance Taylor

unread,
Aug 15, 2024, 1:17:53 PM8/15/24
to Peter Hynes, golang-nuts
On Thu, Aug 15, 2024 at 9:59 AM 'Peter Hynes' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> We have some code that works fine in Go 1.22, but throws a compiler error in Go 1.23.

...

> Is this a compiler bug?

Yes.

Would you be able to file an issue at https://go.dev/issue? Thanks.

Ian

Peter Hynes

unread,
Aug 15, 2024, 5:27:36 PM8/15/24
to golang-nuts

Ian Lance Taylor

unread,
Aug 15, 2024, 5:32:02 PM8/15/24
to Peter Hynes, golang-nuts
On Thu, Aug 15, 2024 at 2:26 PM 'Peter Hynes' via golang-nuts
<golan...@googlegroups.com> wrote:
>
> Thanks.
>
> https://github.com/golang/go/issues/68903

Thanks for reporting this.

Ian
Reply all
Reply to author
Forward
0 new messages