golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.88k stars 17.52k forks source link

dev.typeparams, cmd/compile: panic: interface conversion: ir.Ntype is *ir.Ident, not *ir.FuncType #43530

Closed OneOfOne closed 3 years ago

OneOfOne commented 3 years ago

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

$ git describe --all; git describe --always; go version
heads/dev.typeparams
a8fe098a12
go version devel +a8fe098a12 2021-01-05 10:13:59 -0800 linux/amd64

What did you do?

Source: https://go2goplay.golang.org/p/1qkRFfQNDNz

package main

import (
    "fmt"
    "sort"
)

type Interface interface {
    Len() int
    Less(i, j int) bool
    Swap(i, j int)
}

type comparable interface {
    type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string
}

type comparableSlice[T comparable] []T

func (s comparableSlice[T]) Len() int           { return len(s) }
func (s comparableSlice[T]) Less(i, j int) bool { return s[i] < s[j] }
func (s comparableSlice[T]) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }

func main() {
    s := []int{1, 3, 2}
    sort.Sort(comparableSlice[int](s))
    fmt.Println(s)
}

What did you expect to see?

A successful compile or at least no panics since it works on dev.go2go and the go2 playground.

What did you see instead?

● go build -gcflags '-G -G'
# tmp/go-scratch.3BF
panic: interface conversion: ir.Ntype is *ir.Ident, not *ir.FuncType

goroutine 1 [running]:
cmd/compile/internal/noder.(*noder).interfaceType(0xc0001ba360, 0xc0000c5a10, 0xc0000c5a10, 0xe11000000002)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:1157 +0x73d
cmd/compile/internal/noder.(*noder).expr(0xc0001ba360, 0xf852e8, 0xc0000c5a10, 0x20000e060, 0xa)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:994 +0x2492
cmd/compile/internal/noder.(*noder).typeExpr(0xc0001ba360, 0xf852e8, 0xc0000c5a10, 0xc0003eeee0, 0xc0000c57a0)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:1095 +0x45
cmd/compile/internal/noder.(*noder).typeExprOrNil(...)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:1107
cmd/compile/internal/noder.(*noder).typeDecl(0xc0001ba360, 0xc0003eade0, 0xc0003eade0, 0xe06000000002)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:728 +0x22e
cmd/compile/internal/noder.(*noder).decls(0xc0001ba360, 0xc000038400, 0x9, 0x10, 0x0, 0xc0000bb860, 0xc0000bb810)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:524 +0x557
cmd/compile/internal/noder.(*noder).node(0xc0001ba360)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:481 +0xea
cmd/compile/internal/noder.ParseFiles(0xc00001e230, 0x1, 0x1, 0x2)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:155 +0xacd
cmd/compile/internal/noder.LoadPackage(0xc00001e230, 0x1, 0x1)
        /usr/src/go2/src/cmd/compile/internal/noder/noder.go:33 +0xc5
cmd/compile/internal/gc.Main(0xe615e0)
        /usr/src/go2/src/cmd/compile/internal/gc/main.go:206 +0xafc
main.main()
        /usr/src/go2/src/cmd/compile/main.go:54 +0xb1
OneOfOne commented 3 years ago

@gopherbot, please add labels go2

ALTree commented 3 years ago

I don't think actual, current issues in the dev.typeparams branch need to be labelled as Go2, that's mostly used for broad language changes and stuff like that (for reference: https://github.com/golang/go/issues?q=is%3Aissue+dev.typeparams none of these has the label).

I'm removing it, if you don't mind.

ALTree commented 3 years ago

cc @griesemer

griesemer commented 3 years ago

This (-G -G or -G=2) is not expected to work at this point; the respective code doesn't exist yet and is work in progress.

OneOfOne commented 3 years ago

@griesemer is there any way to compile go2 code with that branch?

ianlancetaylor commented 3 years ago

@OneOfOne It's work in progress. The branch is there to develop the ability to compile code that uses type parameters. Until that work is done, the code on the branch won't work. That's why it's on a branch.

griesemer commented 3 years ago

@OneOfOne What @ianlancetaylor said. You can type-check go2 code with a single -G option (or -G=1) for now.