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/compile: internal compiler error: InitTodoFunc still has declarations (with -G=3) #45722

Closed
mccolljr opened this issue Apr 23, 2021 · 5 comments
Closed
Assignees
Milestone

Comments

@mccolljr
Copy link

@mccolljr mccolljr commented Apr 23, 2021

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

$ go version
go version devel go1.17-105a6e9518 Fri Apr 23 11:33:03 2021 +0000 darwin/amd6

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jmccollum/Library/Caches/go-build"
GOENV="/Users/jmccollum/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/jmccollum/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jmccollum/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/jmccollum/go/src/github.com/golang/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/jmccollum/go/src/github.com/golang/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="devel go1.17-105a6e9518 Fri Apr 23 11:33:03 2021 +0000"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/jmccollum/go/src/github.com/golang/go/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/0d/ncmx73fn4s1_n9bz83jdl0lh0000gn/T/go-build2307230618=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
	"log"
)

func try[T any](v T, err error) T {
	if err != nil {
		panic(err)
	}
	return v
}

func handle(handle func(error)) {
	if issue := recover(); issue != nil {
		if e, ok := issue.(error); ok && e != nil {
			handle(e)
		} else {
			handle(fmt.Errorf("%v", e))
		}
	}
}

func main() {
	defer handle(func(e error) { log.Fatalln(e) })
	_ = try(fmt.Println("test"))
}

What did you expect to see?

Program compiles and runs with no output

What did you see instead?

Program fails to compile with an internal compiler error:

<unknown line number>: internal compiler error: InitTodoFunc still has declarations

goroutine 1 [running]:
runtime/debug.Stack()
        /Users/jmccollum/go/src/github.com/golang/go/src/runtime/debug/stack.go:24 +0x65
cmd/compile/internal/base.FatalfAt({0xa2b00, 0xc0}, {0x1934032, 0x191a737}, {0x0, 0x10000000100cc5b, 0x22097f0})
        /Users/jmccollum/go/src/github.com/golang/go/src/cmd/compile/internal/base/print.go:227 +0x157
cmd/compile/internal/base.Fatalf(...)
        /Users/jmccollum/go/src/github.com/golang/go/src/cmd/compile/internal/base/print.go:196
cmd/compile/internal/pkginit.Task()
        /Users/jmccollum/go/src/github.com/golang/go/src/cmd/compile/internal/pkginit/init.go:66 +0x8b6
cmd/compile/internal/gc.Main(0x1949ab8)
        /Users/jmccollum/go/src/github.com/golang/go/src/cmd/compile/internal/gc/main.go:197 +0xb38
main.main()
        /Users/jmccollum/go/src/github.com/golang/go/src/cmd/compile/main.go:55 +0xdd
@mccolljr
Copy link
Author

@mccolljr mccolljr commented Apr 23, 2021

This is the code section that is originating the panic:

if typecheck.InitTodoFunc.Dcl != nil {
// We only generate temps using InitTodoFunc if there
// are package-scope initialization statements, so
// something's weird if we get here.
base.Fatalf("InitTodoFunc still has declarations")
}

I will do a bit more digging.

Loading

@mccolljr
Copy link
Author

@mccolljr mccolljr commented Apr 23, 2021

This is being generated directly from gc.Main:

// Build init task.
if initTask := pkginit.Task(); initTask != nil {
typecheck.Export(initTask)
}

The decls that are causing the panic seem to be either the results of fmt.Println("test") in the call _ = try(fmt.Println("test")) or the arguments to the try[int] instantiation of try[T] from that call:

.   NAME-main..autotmp_0 esc(N) Class:PAUTO Offset:0 AutoTemp OnStack Used int
.   NAME-main..autotmp_1 esc(N) Class:PAUTO Offset:0 AutoTemp OnStack Used error

Loading

@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented Apr 26, 2021

Loading

@cherrymui cherrymui added this to the Go1.18 milestone Apr 26, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Apr 26, 2021

Clearly related to the new code dealing with generic functions. Replacing the generic try function with:

func try(v int, err error) int {
	if err != nil {
		panic(err)
	}
	return v
}

makes the error go away.

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Apr 27, 2021

Change https://golang.org/cl/313869 mentions this issue: cmd/compile: fix handling of ir.CurFunc during stenciling

Loading

@gopherbot gopherbot closed this in 55c517a Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants