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/go2go: panic if using generic type with badly defined method as an interface #39664

Closed
DeedleFake opened this issue Jun 17, 2020 · 6 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@DeedleFake
Copy link

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.
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 17, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Jun 17, 2020
@ianlancetaylor
Copy link
Contributor

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

@DeedleFake
Copy link
Author

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

@griesemer
Copy link
Contributor

Simpler reproducer:

package p

type T(type _) struct {}

func (T) m()

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

@griesemer
Copy link
Contributor

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

@gopherbot
Copy link

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

gopherbot pushed a commit that referenced this issue Jun 18, 2020
…type parameters

Fixes #39664.

Change-Id: I0cf585f0c704bdaa163a7ebc9e7c82a734fcd5e9
Reviewed-on: https://go-review.googlesource.com/c/go/+/238625
Reviewed-by: Robert Griesemer <gri@golang.org>
@griesemer
Copy link
Contributor

Fixed in dev.go2o branch.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants