Skip to content

go/types, types: gopls crash in unification #50929

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

Closed
virtuald opened this issue Jan 31, 2022 · 7 comments
Closed

go/types, types: gopls crash in unification #50929

virtuald opened this issue Jan 31, 2022 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker
Milestone

Comments

@virtuald
Copy link

gopls version: v0.7.5
gopls flags:
update flags: proxy
extension version: 0.31.0
go version: devel go1.18-a5c0b19
environment: Visual Studio Code darwin
initialization error: undefined
issue timestamp: Mon, 31 Jan 2022 05:48:23 GMT
restart history:
Sun, 30 Jan 2022 07:58:26 GMT: activation (enabled: true)
Mon, 31 Jan 2022 04:48:18 GMT: manual (enabled: true)
Mon, 31 Jan 2022 04:49:33 GMT: installation (enabled: true)
Mon, 31 Jan 2022 04:49:39 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:35:21 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:36:46 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:43:01 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:43:44 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:45:49 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:46:28 GMT: manual (enabled: true)
Mon, 31 Jan 2022 05:47:49 GMT: manual (enabled: true)

ATTENTION: PLEASE PROVIDE THE DETAILS REQUESTED BELOW.

Describe what you observed.

panic: assertion failed

goroutine 4561 [running]:
go/types.(*Checker).handleBailout(0xc0021fbdc0, 0xc0066ef688)
	  check.go:304  0x8b
panic({0x1854660, 0x1bce580})
	  panic.go:838  0x207
go/types.assert(...)
	  errors.go:21
go/types.(*unifier).nify(0xc0034b8320, {0x1bd3b00%3F, 0xc000520e70%3F}, {0x1bd3b00%3F, 0xc0005210a0%3F}, 0x0)
	  unify.go:461  0x1045
go/types.(*unifier).unify(...)
	  unify.go:64
go/types.(*Checker).infer(0xc0021fbdc0, {0x1bd0280, 0xc002f43300}, {0xc004d09240, 0x3, 0x4}, {0x0%3F, 0x18877a0%3F, 0xc005b74900%3F}, 0xc006dcf668, ...)
	  infer.go:193  0x599
go/types.(*Checker).arguments(0xc0021fbdc0, 0xc002f43300, 0xc007ed8fc0, {0x0, 0x0, 0x0}, {0xc0069943f8%3F, 0x1, 0x1}, {0x0, ...})
	  call.go:390  0x945
go/types.(*Checker).callExpr(0xc0021fbdc0, 0xc002e44fc0, 0xc002f43300)
	  call.go:210  0x669
go/types.(*Checker).exprInternal(0xc0021fbdc0, 0xc002e44fc0, {0x1bd5b70%3F, 0xc002f43300}, {0x0%3F, 0x0%3F})
	  expr.go:1483  0x9f2
go/types.(*Checker).rawExpr(0x100eb05%3F, 0xc0066edad8%3F, {0x1bd5b70%3F, 0xc002f43300%3F}, {0x0%3F, 0x0%3F}, 0x0)
	  expr.go:1111  0x45
go/types.(*Checker).stmt(0xc0021fbdc0, 0x0, {0x1bd5d20%3F, 0xc0016155b0%3F})
	  stmt.go:394  0x247c
go/types.(*Checker).stmtList(0xc0021fbdc0%3F, 0x0, {0xc0016155c0%3F, 0xc0021fbdc0%3F, 0x1bd1c00%3F})
	  stmt.go:125  0xc9
go/types.(*Checker).funcBody(0xc0021fbdc0, 0xc006b890e0, {0x2%3F, 0xc006dcfad0%3F}, 0xc002e44e40, 0xc001f40a80, {0x0, 0x0})
	  stmt.go:46  0x285
go/types.(*Checker).exprInternal.func1()
	  expr.go:1210  0x45
go/types.(*Checker).processDelayed(0xc0021fbdc0, 0x29)
	  check.go:375  0x39
go/types.(*Checker).stmt(0xc0021fbdc0, 0x0, {0x1bd60b0%3F, 0xc003ece820%3F})
	  stmt.go:936  0x28e5
go/types.(*Checker).stmtList(0xc0021fbdc0%3F, 0x0, {0xc001a39400%3F, 0xc0021fbdc0%3F, 0x1bd1c00%3F})
	  stmt.go:125  0xc9
go/types.(*Checker).funcBody(0xc0021fbdc0, 0xc006b890e0, {0x1bd3ab0%3F, 0xc006dcf728%3F}, 0xc007ed9ac0, 0xc001f40ae0, {0x0, 0x0})
	  stmt.go:46  0x285
go/types.(*Checker).funcDecl.func1()
	  decl.go:790  0x45
go/types.(*Checker).processDelayed(0xc0021fbdc0, 0x0)
	  check.go:375  0x39
go/types.(*Checker).checkFiles(0xc0021fbdc0, {0xc00531ee88%3F, 0xc00324ab40%3F, 0x2%3F})
	  check.go:326  0xcb
go/types.(*Checker).Files(...)
	  check.go:309
golang.org/x/tools/internal/lsp/cache.doTypeCheck({0x1bd7238, 0xc0046eff00}, 0xc0091b6500, 0xc001603860, 0x2, 0xc001bb0c00, 0x1bd6620%3F)
	  check.go:536  0x8b3
golang.org/x/tools/internal/lsp/cache.typeCheck({0x1bd7238, 0xc0046eff00}, 0xc0091b6500, 0xc001603860, 0x2, 0xc001bb0c00%3F)
	  check.go:313  0xe5
golang.org/x/tools/internal/lsp/cache.(*snapshot).buildPackageHandle.func1({0x1bd7238%3F, 0xc0046eff00}, {0x1bd0b40%3F, 0xc0091b6500})
	  check.go:124  0x233
golang.org/x/tools/internal/memoize.(*Handle).run.func1()
	  memoize.go:327  0xa9
created by golang.org/x/tools/internal/memoize.(*Handle).run
	  memoize.go:320  0x1b8
[Info  - 12:48:04 AM] 

It crashes pretty consistently when I restart with this file loaded:

 package main

 import "fmt"
 
 type RC[G any, RG any] interface {
 	~[]RG
 }
 
 type RG[G any] struct{}
 
 type RSC[G any] []*RG[G]
 
 type M[Rc RC[G, RG], G any, RG any] struct {
 	Fn func(Rc)
 }

 type NFn[Rc RC[G, RG], G any, RG any] func(Rc)

-func NC[Rc RC[G, RG], G any, RG any](nFn NFn[Rc, RG]) {
+func NC[Rc RC[G, RG], G any, RG any](nFn NFn[Rc, G, RG]) {
 	var empty Rc
 	nFn(empty)
 }
 
 func NSG[G any](c RSC[G]) {
 	fmt.Println(c)
 }
 
 func MMD[Rc RC[RG], RG any, G any]() M[Rc, RG] {
 
 	var nFn NFn[Rc, RG]
 
 	var empty Rc
 	switch any(empty).(type) {
 	case BC:
 
 	case RSC[G]:
 		nFn = NSG[G]
 	}
 
 	return M[Rc, RG]{
 		Fn: func(rc Rc) {
 			NC(nFn)
 		},
 	}
 
 	return M[Rc, RG]{}
 }

In particular, the crash has something to do with adding that G marked in the diff. Without it, no crash. With it, crashes every time.

@findleyr findleyr changed the title gopls: automated issue report (crash) go/types, types: gopls crash in unification Jan 31, 2022
@findleyr findleyr transferred this issue from golang/vscode-go Jan 31, 2022
@findleyr
Copy link
Member

CC @griesemer

I have not yet reproduced this, but the Go version here is recent (https://go.dev/cl/360615), so I don't think this crash is fixed.

That call stack indicates that this crash can occur during normal type checking.

@findleyr
Copy link
Member

Confirmed that this repros in go/types without gopls. Here is a smaller repro:

package p

type RC[G any, RG any] interface {}

type RG[G any] struct{}

type NFn[Rc RC[G, RG], G any, RG any] func(Rc)

func NC[Rc RC[G, RG], G any, RG any](nFn NFn[Rc, G, RG]) {
}

func _[Rc RC[RG], RG any]() {
	var nFn NFn[Rc, RG]
	NC(nFn)
}

@findleyr findleyr added this to the Go1.18 milestone Jan 31, 2022
@griesemer griesemer self-assigned this Jan 31, 2022
@findleyr
Copy link
Member

Ok, minimal repro:

package p

type F[A, B any] int

func G[A, B any](F[A, B]) {
}

func _() {
	var x F[int]
	G(x)
}

@griesemer
Copy link
Contributor

Thanks, @findleyr . I will take this on.

@griesemer griesemer added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 31, 2022
@virtuald
Copy link
Author

Thanks @findleyr I had tried to make my example more minimal but the error kept going away. Glad you were able to make it smaller!

@findleyr
Copy link
Member

@virtuald thanks very much for filing the report, and no need to make it perfectly minimal. It's really helpful to get crashers in any form.

In this case the type of x is invalid (with an incorrect number of type arguments), and it looks like the type checker asserts that such a type can never get to unification.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/382076 mentions this issue: go/types, types2: fix unification code for defined types

@golang golang locked and limited conversation to collaborators Jun 22, 2023
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. release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants