Go: cmd/go2go: panic if using generic type with badly defined method as an interface

Created on 17 Jun 2020  路  6Comments  路  Source: golang/go

Playground

package main

import (
    "fmt"
)

type Example(type T) struct {
    v T
}

// Note missing type parameter from Example.
func (e Example) String() string {
    return fmt.Sprint(e.v)
}

func main() {
    var str fmt.Stringer
    str = Example(int){
        v: 3,
    }
    fmt.Println(str)
}

Output:

panic: assertion failed [recovered]
    panic: assertion failed [recovered]
    panic: assertion failed

goroutine 1 [running]:
go/types.(*Checker).handleBailout(0xc00007c900, 0xc000149be0)
    /usr/local/go-faketime/src/go/types/check.go:251 +0x98
panic(0x642580, 0x6d26a0)
    /usr/local/go-faketime/src/runtime/panic.go:969 +0x175
go/types.(*Checker).stmt.func1(0xc00007c900, 0xc0001733b0)
    /usr/local/go-faketime/src/go/types/stmt.go:304 +0x85
panic(0x642580, 0x6d26a0)
    /usr/local/go-faketime/src/runtime/panic.go:975 +0x3e9
go/types.assert(...)
    /usr/local/go-faketime/src/go/types/errors.go:19
go/types.makeSubstMap(0x0, 0x0, 0x0, 0xc00001cab0, 0x1, 0x1, 0x6)
    /usr/local/go-faketime/src/go/types/subst.go:26 +0x2ca
go/types.(*Checker).missingMethod(0xc00007c900, 0x6de1a0, 0xc000174900, 0xc000104600, 0xc000104601, 0xf147ef2c6b20a501, 0x0)
    /usr/local/go-faketime/src/go/types/lookup.go:399 +0x43a
go/types.(*operand).assignableTo(0xc000072a00, 0xc00007c900, 0x6ddc00, 0xc00007a7e0, 0xc000148b58, 0xc00007a7e0)
    /usr/local/go-faketime/src/go/types/operand.go:270 +0x354
go/types.(*Checker).assignment(0xc00007c900, 0xc000072a00, 0x6ddc00, 0xc00007a7e0, 0x690d53, 0xa)
    /usr/local/go-faketime/src/go/types/assignments.go:65 +0x1ed
go/types.(*Checker).assignVar(0xc00007c900, 0x6daa60, 0xc00000c540, 0xc000072a00, 0x0, 0xc00000e2a0)
    /usr/local/go-faketime/src/go/types/assignments.go:203 +0x576
go/types.(*Checker).assignVars(0xc00007c900, 0xc00001c620, 0x1, 0x1, 0xc00001c690, 0x1, 0x1)
    /usr/local/go-faketime/src/go/types/assignments.go:281 +0x22f
go/types.(*Checker).stmt(0xc00007c900, 0x0, 0x6da420, 0xc0000725c0)
    /usr/local/go-faketime/src/go/types/stmt.go:406 +0x359d
go/types.(*Checker).stmtList(0xc00007c900, 0x0, 0xc000072640, 0x3, 0x4)
    /usr/local/go-faketime/src/go/types/stmt.go:125 +0xd1
go/types.(*Checker).funcBody(0xc00007c900, 0xc0001747e0, 0xc00001a44c, 0x4, 0xc0001748a0, 0xc000010d80, 0x0, 0x0)
    /usr/local/go-faketime/src/go/types/stmt.go:42 +0x257
go/types.(*Checker).funcDecl.func1()
    /usr/local/go-faketime/src/go/types/decl.go:786 +0x67
go/types.(*Checker).processDelayed(0xc00007c900, 0x0)
    /usr/local/go-faketime/src/go/types/check.go:315 +0x3e
go/types.(*Checker).checkFiles(0xc00007c900, 0xc00003dcb8, 0x1, 0x1, 0x0, 0x0)
    /usr/local/go-faketime/src/go/types/check.go:283 +0x145
go/types.(*Checker).Files(...)
    /usr/local/go-faketime/src/go/types/check.go:256
go/types.(*Config).Check(0xc000072680, 0xc00001a3e0, 0x4, 0xc000072240, 0xc00003dcb8, 0x1, 0x1, 0xc000055360, 0x0, 0x4b6d2f, ...)
    /usr/local/go-faketime/src/go/types/api.go:387 +0x188
go/go2go.RewriteBuffer(0xc0000553b0, 0x7ffce9ea6dd8, 0x1e, 0xc000138000, 0x10a, 0x30a, 0x0, 0xc000010a50, 0xc000010a20, 0xc0000109f0, ...)
    /usr/local/go-faketime/src/go/go2go/go2go.go:127 +0x24f
main.translateFile(0xc0000553b0, 0x7ffce9ea6dd8, 0x1e)
    /usr/local/go-faketime/src/cmd/go2go/translate.go:26 +0xa9
main.main()
    /usr/local/go-faketime/src/cmd/go2go/main.go:64 +0x2ea

Go build failed.
NeedsInvestigation

All 6 comments

The code is incorrect but the type checker should return an error rather than panicking.

My thoughts exactly. Was a bit confused when it happened. Thought it was a runtime error in my code.

Simpler reproducer:

package p

type T(type _) struct {}

func (T) m()

func _() {
    var x interface { m() }
    x = T(int){}
    _ = x
}

The error is actually reported, but the type-checker dies later.

Change https://golang.org/cl/238625 mentions this issue: [dev.go2go] go/types: don't crash when receiver type doesn't declare type parameters

Fixed in dev.go2o branch.

Was this page helpful?
0 / 5 - 0 ratings