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

go/types, types2: index out of range panic in Checker.arguments #61931

Closed
kralicky opened this issue Aug 10, 2023 · 9 comments
Closed

go/types, types2: index out of range panic in Checker.arguments #61931

kralicky opened this issue Aug 10, 2023 · 9 comments
Assignees
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

@kralicky
Copy link

gopls version: (devel) (go1.21.0) (built from 47c5305)
gopls flags:
update flags: proxy
extension version: 0.39.1
go version: 1.21.0
environment: Visual Studio Code linux
initialization error: undefined
issue timestamp: Thu, 10 Aug 2023 15:53:47 GMT
restart history:
Thu, 10 Aug 2023 15:33:15 GMT: activation (enabled: true)
Thu, 10 Aug 2023 15:52:58 GMT: manual (enabled: true)
Thu, 10 Aug 2023 15:53:29 GMT: manual (enabled: true)

panic: runtime error: index out of range [2] with length 2 [recovered]
	panic: runtime error: index out of range [2] with length 2

goroutine 227 [running]:
go/types.(*Checker).handleBailout(0xc0002c05a0, 0xc0004a7578)
	/home/joe/sdk/go1.21.0/src/go/types/check.go:336 +0x88
panic({0xe748e0?, 0xc00003c7e0?})
	/home/joe/sdk/go1.21.0/src/runtime/panic.go:914 +0x21f
go/types.(*Checker).arguments(0xc0002c05a0, 0xc0000fc300, 0xc000381a80, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}, {0xc0008204f8, ...}, ...)
	/home/joe/sdk/go1.21.0/src/go/types/call.go:648 +0x17a5
go/types.(*Checker).callExpr(0xc0002c05a0, 0xc000381800, 0xc0000fc300)
	/home/joe/sdk/go1.21.0/src/go/types/call.go:302 +0x86d
go/types.(*Checker).exprInternal(0xc0002c05a0, {0x0, 0x0}, 0xc000381800, {0x116bf60, 0xc0000fc300?}, {0x0?, 0x0?})
	/home/joe/sdk/go1.21.0/src/go/types/expr.go:1359 +0x1809
go/types.(*Checker).rawExpr(0xc0002c05a0, {0x0, 0x0}, 0xc000381800, {0x116bf60?, 0xc0000fc300?}, {0x0?, 0x0?}, 0x0)
	/home/joe/sdk/go1.21.0/src/go/types/expr.go:965 +0x1a5
go/types.(*Checker).stmt(0xc0002c05a0, 0x0, {0x116c3b0?, 0xc000692070?})
	/home/joe/sdk/go1.21.0/src/go/types/stmt.go:397 +0x19a
go/types.(*Checker).stmtList(0xc0002c05a0?, 0x0, {0xc000692080?, 0x0?, 0x6d4cfb?})
	/home/joe/sdk/go1.21.0/src/go/types/stmt.go:124 +0x85
go/types.(*Checker).funcBody(0xc0002c05a0, 0xc00052c720, {0xc00061e014?, 0xc000116b90?}, 0xc000381540, 0xc000620180, {0x0, 0x0})
	/home/joe/sdk/go1.21.0/src/go/types/stmt.go:44 +0x345
go/types.(*Checker).funcDecl.func1()
	/home/joe/sdk/go1.21.0/src/go/types/decl.go:826 +0x39
go/types.(*Checker).processDelayed(0xc0002c05a0, 0x0)
	/home/joe/sdk/go1.21.0/src/go/types/check.go:439 +0x15c
go/types.(*Checker).checkFiles(0xc0002c05a0, {0xc0000b6418, 0x1, 0x1})
	/home/joe/sdk/go1.21.0/src/go/types/check.go:383 +0x245
go/types.(*Checker).Files(...)
	/home/joe/sdk/go1.21.0/src/go/types/check.go:341
golang.org/x/tools/gopls/internal/lsp/cache.doTypeCheck({0x116d688, 0xc00058ef30}, 0xc00052c240, {{0xc0008826f6, 0xa}, {0xc0008826f6, 0xa}, {0xc000882710, 0x4}, {0xc0001a21f0, ...}, ...})
	/home/joe/tools/gopls/internal/lsp/cache/check.go:1561 +0x8a5
golang.org/x/tools/gopls/internal/lsp/cache.typeCheckImpl({0x116d688, 0xc00058edb0}, 0x2?, {{0xc0008826f6, 0xa}, {0xc0008826f6, 0xa}, {0xc000882710, 0x4}, {0xc0001a21f0, ...}, ...})
	/home/joe/tools/gopls/internal/lsp/cache/check.go:1420 +0x205
golang.org/x/tools/gopls/internal/lsp/cache.(*typeCheckBatch).checkPackage(0xc0000c3ed8?, {0x116d688, 0xc00058e480}, 0xc000592240)
	/home/joe/tools/gopls/internal/lsp/cache/check.go:674 +0x245
golang.org/x/tools/gopls/internal/lsp/cache.(*typeCheckBatch).handleSyntaxPackage(0xc00052c240, {0x116d688, 0xc00058e480}, 0xc0006c5860?, {0xc0008826f6, 0xa})
	/home/joe/tools/gopls/internal/lsp/cache/check.go:532 +0x42a
golang.org/x/tools/gopls/internal/lsp/cache.(*snapshot).forEachPackageInternal.func2()
	/home/joe/tools/gopls/internal/lsp/cache/check.go:394 +0x2b
golang.org/x/sync/errgroup.(*Group).Go.func1()
	/home/joe/go/pkg/mod/golang.org/x/sync@v0.3.0/errgroup/errgroup.go:75 +0x56
created by golang.org/x/sync/errgroup.(*Group).Go in goroutine 158
	/home/joe/go/pkg/mod/golang.org/x/sync@v0.3.0/errgroup/errgroup.go:72 +0x96

Here is a minimal program that can reproduce the bug. Deleting the last ')' on line 8 will cause gopls to crash.

package main

func A[T any](func(T) T, ...any) {}
func B[T any](t T) T { return t }
func C(args ...func(any)) any { return nil }

func main() {
	A[any](B, C(func(any) {})) // Delete the last ) on this line
}
@findleyr
Copy link
Contributor

Thanks very much for the repro! This appears to be a type-checker bug.

@findleyr findleyr changed the title gopls: automated issue report (crash) go/types, types2: index out of range panic in Checker.arguments Aug 10, 2023
@findleyr findleyr transferred this issue from golang/vscode-go Aug 10, 2023
@findleyr findleyr added this to the Go1.21.1 milestone Aug 10, 2023
@findleyr findleyr added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 10, 2023
@findleyr
Copy link
Contributor

@gopherbot please backport this to 1.21; it is a type checker and compiler panic.

@gopherbot
Copy link
Contributor

Backport issue(s) opened: #61932 (for 1.21).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

@findleyr
Copy link
Contributor

Bisected to https://go.dev/cl/495395

@griesemer
Copy link
Contributor

Simpler reproducer (for go/types):

package p

func A(func(any), ...any) {}
func B[T any](T)          {}

func _() {
	A(B, nil
}

Run as testdata/manual.go with go test -run Manual.

@griesemer
Copy link
Contributor

The optimization "Optimization: Only if the callee's parameter list was adjusted do we need to..." in call.go seems incorrect. If we always do it, the crash disappears. Investigating.

@findleyr
Copy link
Contributor

The crash was self explanatory, I thought: previously we would exit early if type parameters did not match signature.

It is another question how the code should be optimally structured...

@griesemer
Copy link
Contributor

It's just the normal arguments (not the type arguments); and we exited early if some of the arguments were invalid (not because their number was incorrect). That check simply hid another bug (which doesn't appear with types2 because the syntax parser produces a different syntax tree in this case). So this is worth getting to the bottom of it.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/519456 mentions this issue: go/types: use correct signature when checking argument passing

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