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: unexpected Defn:" on if true { return } #48033

Closed
heinrichsmythe opened this issue Aug 28, 2021 · 5 comments
Closed

Comments

@heinrichsmythe
Copy link

@heinrichsmythe heinrichsmythe commented Aug 28, 2021

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

$ go version
go version go1.17 linux/amd64

Does this issue reproduce with the latest release?

Yes, it used to compile on go 1.16

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3898192828=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The code and playground links are below.

If I remove the if true {} block the code compiles.

https://play.golang.org/p/hegRGlzb751

package main

import (
	"fmt"
	"strings"
)

type app struct {
	Name string
}

func bug() func() {
	return func() {


		// the issue is this if true block
		if true {
			return
		}

		var xx = []app{}
		var gapp app
		for _, app := range xx {
			if strings.ToUpper("") == app.Name {
				fmt.Printf("%v\n", app)
				gapp = app
			}
		}
		fmt.Println(gapp)
	}
}

func main() {
	bug()
}

What did you expect to see?

A function that immediately returns
A compiled application.

What did you see instead?

A compiler bug.

# command-line-arguments
./test.go:21:10: internal compiler error: unexpected Defn: 
.   RANGE-init
.   .   DCL # test.go:21
.   .   .   NAME-main.app tc(1) Class:PAUTO Offset:0 OnStack Used main.app # test.go:21
.   RANGE tc(1) Def # test.go:21
.   .   NAME-main.xx tc(1) Class:PAUTO Offset:0 OnStack Used SLICE-[]app # test.go:19
.   RANGE-Key
.   .   NAME-main._ tc(1) Offset:0 blank
.   .   NAME-main.app tc(1) Class:PAUTO Offset:0 OnStack Used main.app # test.go:21
.   RANGE-Body
.   .   IF tc(1) # test.go:22
.   .   IF-Cond
.   .   .   EQ tc(1) bool # test.go:22 bool
.   .   .   .   CALLFUNC tc(1) Use:1 string # test.go:22 string
.   .   .   .   .   NAME-strings.ToUpper tc(1) Class:PFUNC Offset:0 Used FUNC-func(string) string # strings.go:550
.   .   .   .   CALLFUNC-Args
.   .   .   .   .   LITERAL-"" tc(1) string # test.go:22
.   .   .   .   DOT tc(1) string # test.go:22 main.Name string
.   .   .   .   .   NAME-main.app tc(1) Class:PAUTO Offset:0 OnStack Used main.app # test.go:21
.   .   IF-Body
.   .   .   CALLFUNC tc(1) Use:3 STRUCT-(int, error) # test.go:23 STRUCT-(int, error)
.   .   .   .   NAME-fmt.Printf tc(1) Class:PFUNC Offset:0 Used FUNC-func(string, ...interface {}) (int, error) # print.go:212
.   .   .   CALLFUNC-Args
.   .   .   .   LITERAL-"%v\n" tc(1) string # test.go:23
.   .   .   .   CONVIFACE tc(1) Implicit INTER-interface {} # test.go:23 INTER-interface {}
.   .   .   .   .   NAME-main.app tc(1) Class:PAUTO Offset:0 OnStack Used main.app # test.go:21
.   .   .   AS tc(1) # test.go:24
.   .   .   .   NAME-main.gapp tc(1) Class:PAUTO Offset:0 OnStack Used main.app # test.go:20
.   .   .   .   NAME-main.app tc(1) Class:PAUTO Offset:0 OnStack Used main.app # test.go:21

Please file a bug report including a short program that triggers the error.
https://golang.org/issue/new
@heinrichsmythe heinrichsmythe changed the title "internal compiler error: unexpected Defn:" on immediate if true { return true } "internal compiler error: unexpected Defn:" on immediate if true { return } Aug 28, 2021
@heinrichsmythe heinrichsmythe changed the title "internal compiler error: unexpected Defn:" on immediate if true { return } "internal compiler error: unexpected Defn:" on if true { return } Aug 28, 2021
@ALTree
Copy link
Member

@ALTree ALTree commented Aug 28, 2021

Also crashes tip.

Loading

@ALTree ALTree added this to the Go1.18 milestone Aug 28, 2021
@ALTree
Copy link
Member

@ALTree ALTree commented Aug 28, 2021

Loading

@ALTree ALTree changed the title "internal compiler error: unexpected Defn:" on if true { return } cmd/compile: "internal compiler error: unexpected Defn:" on if true { return } Aug 28, 2021
@cuonglm
Copy link
Member

@cuonglm cuonglm commented Aug 28, 2021

The problem seems that we are now allowed inlining function with for loop. This is fixed with unified IR, though, not sure it's worth/qualified to fix.

If we do fix this, then I think a safe way is allowing *ir.RangeStmt, the same as we're allowing *ir.TypeSwitchGuard.

Loading

@cuonglm
Copy link
Member

@cuonglm cuonglm commented Aug 28, 2021

Also cc @danscales as this involves inlining.

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 28, 2021

Change https://golang.org/cl/345436 mentions this issue: cmd/compile: workaround inlining of closures with type switches

Loading

@gopherbot gopherbot closed this in 08e2519 Sep 2, 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
4 participants