Skip to content

cmd/compile: internal error when instantiating constrained generic type parameter #81627

Open

Description

@christoph-sg

Go version

go version go 1.27

Output of go env in your module/workspace:

I used the playground

What did you do?

Compile this program on go.dev/play (https://go.dev/play/p/8Mktvi3buTg)

package main

import (
	"cmp"
	"fmt"
)

func main() {
	Fns := []func(x, y int) bool{cmp.Less}
	fmt.Printf("%v", Fns[0](1, 2))
}

What did you see happen?

The compiler reports this internal error

./prog.go:9:34: internal compiler error: func[T cmp.Ordered](x T, y T) bool is not assignable to func(x int, y int) bool

Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new

Go build failed.

What did you expect to see?

Successful compilation, or a non-internal error, depending on how Go instantiates constrained generic type parameters

Curiously it works fine if I re-define Less locally (https://go.dev/play/p/a7jQKg1dxDd):

package main

import (
	"cmp"
	"fmt"
)

func Less[a cmp.Ordered](x, y a) bool {
	return x < y
}

func main() {
	Fns := []func(x, y int) bool{Less}
	fmt.Printf("%v", Fns[0](1, 2))
}

Activity

  1. changed the title [-]compiler: internal error when instantiating constrained generic type parameter[/-] [+]cmd/compile: internal error when instantiating constrained generic type parameter[/+] on Sep 21, 2026
  2. thepudds commented on Sep 21, 2026

    @thepudds
    Member

    This seems to reproduce on tip. A bisect landed on f757de8:

      first BAD:   f757de83f4 | 2026-04-21 03:14 | go/types, types2: improve type inference for assignment contexts
      GOOD parent: 3304e73ded | 2026-04-20 23:56 | Revert "net/http/internal/http2: prevent alloc when writing status code for responses"
    

    CC @griesemer @mrkfrmn

  3. griesemer commented on Sep 21, 2026

    @griesemer
    Contributor

    Hm. Looks like something goes awry in the UIR (superficial inspection) as this program typechecks ok outside the compiler.

    There's of course the obvious work-around which is to instantiate explicitly (playground):

    package main
    
    import "cmp"
    
    var _ = [](func(x, y int) bool){cmp.Less[int]}
    
    func main() {}

    cc: @mrkfrmn

  4. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Sep 29, 2026
  5. gopherbot commented on Sep 30, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/841725 mentions this issue: cmd/compile: handle inferred generic function values in UIR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions