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 importing a recursive type (recursive through a type constraint) #51219

Closed
Karitham opened this issue Feb 16, 2022 · 12 comments
Labels
NeedsInvestigation
Milestone

Comments

@Karitham
Copy link

@Karitham Karitham commented Feb 16, 2022

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

❯ g version
go version devel go1.18-293ecd8 Tue Feb 15 22:54:38 2022 +0000 linux/amd64

Does this issue reproduce with the latest release?

1.18beta1, beta2 & gotip

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

go env Output
❯ g env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/karitham/.cache/go-build"
GOENV="/home/karitham/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/karitham/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/karitham/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/karitham/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/karitham/sdk/gotip/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.18-293ecd8 Tue Feb 15 22:54:38 2022 +0000"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/karitham/corde/go.mod"
GOWORK=""
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-build4188918709=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Using type parameters, I get a compiler panic in an assignment.

I have tried extremely hard to provide a better program, but haven't managed to reproduce the bug easily on the playground.

See https://github.com/Karitham/corde/blob/error/compiler-panic/components/interactions.go#L148-L156 for the whole codebase with the log

What did you expect to see?

Either a compile error that makes sense, or my program building (most likely the second one, but I'm not familiar enough with generics yet to expect my code to be 100% valid)

What did you see instead?

Nonsensical error message and/or compiler panic

@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Feb 16, 2022

CC @ianlancetaylor, @randall77.

@dmitshur dmitshur added the NeedsInvestigation label Feb 16, 2022
@dmitshur dmitshur added this to the Go1.18 milestone Feb 16, 2022
@bcmills bcmills changed the title cmd/go: internal compiler error cmd/compile: internal compiler error Feb 16, 2022
@randall77
Copy link
Contributor

@randall77 randall77 commented Feb 16, 2022

To reproduce:

$ git clone https://github.com/Karitham/corde
$ cd corde
$ git checkout 1848bb7405f089e2e68952b015278f94afe3f053
$ cd components
$ go build
# github.com/Karitham/corde/components
./interactions.go:152:11: cannot use bt.Data (variable of type JsonRaw) as type JsonRaw in assignment

I think there's some confusion as to what JsonRaw is. Inside UnmarshalJSON, JsonRaw is a type parameter. But in the definition of _basicT, JsonRaw is a concrete type (a []byte, defined in json.go).
So the error is correct, it is just confusing because you have two different types with the same name.

Same as if you did:

package main

type T int

var x T

func f() {
	type T float64
	var y T
	x = y
}
tmp1.go:10:4: cannot use y (type T) as type T in assignment

Closing, as this is not a bug. Maybe there would be ways to improve the error messages (include definition line of the type name when it appears more than once in the error message?) but this is currently working as expected.

@Karitham
Copy link
Author

@Karitham Karitham commented Feb 16, 2022

Have you tried removing the assignment, though?

Just to be clear, if you do remove the assignment you get a compiler internal error, which is for sure unexpected

It's also where an issue comes in, JsonRaw isn't defined as a type & a variable, it's used as a type in the Type Parameter on the receiver, but nowhere is it defined by something else.

I'm not sure why the type parameter on the receiver would turn out to be its own new type identifier when it shouldn't be?
Why would I not be able to assign to a variable of that type inside the function, especially since both underlying types are exactly the same? I can't cast either? Is this a lost cause?

It makes it literally impossible to make specific implementations of methods on structures with type parameters?

@Karitham
Copy link
Author

@Karitham Karitham commented Feb 16, 2022

I should have been more clear earlier, my mistake; I tried diving into the problem but it ended up like an X/Y problem

To reproduce the error building & the error message

git clone https://github.com/Karitham/corde
cd corde
git checkout ae8ae03a3401df2f1a6a365e81742bf90acfaf94
go test ./...

@randall77

Full stack trace
❯ g test
# github.com/Karitham/corde [github.com/Karitham/corde.test]
./router.go:91:11: internal compiler error: unexpected expr: COMPLIT Mux{...}

goroutine 1 [running]:
runtime/debug.Stack()
        /home/karitham/sdk/gotip/src/runtime/debug/stack.go:24 +0x65
cmd/compile/internal/base.FatalfAt({0xd4ca71?, 0x0?}, {0xd2fd6a, 0x16}, {0xc0000e90a8, 0x2, 0x2})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/base/print.go:227 +0x1d7
cmd/compile/internal/base.Fatalf(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/base/print.go:196
cmd/compile/internal/escape.(*escape).exprSkipInit(0xc001265e60, {0xc001272700?, 0xc0000e91a8?, 0x0?, 0x0?}, {0xe98e20, 0xc000de3e80?})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/expr.go:39 +0x1825
cmd/compile/internal/escape.(*escape).expr(0xc001272700?, {0xc001272700?, 0x0?, 0x0?, 0x30?}, {0xe98e20, 0xc000de3e80})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/expr.go:20 +0xad
cmd/compile/internal/escape.(*escape).exprSkipInit(0xc001265e60, {0xc001272700?, 0x6f4368?, 0x0?, 0x90?}, {0xe98330, 0xc0004ec320?})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/expr.go:64 +0xbed
cmd/compile/internal/escape.(*escape).expr(0xc001272700?, {0xc001272700?, 0x0?, 0x0?, 0x50?}, {0xe98330, 0xc0004ec320})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/expr.go:20 +0xad
cmd/compile/internal/escape.(*escape).assignList(0xc001265e60, {0xc0000e96b8, 0x1, 0xc0004e0a90?}, {0xc0000e96a8, 0x1, 0xc001265e60?}, {0xd1d7d2, 0x6}, {0xe98650, ...})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/assign.go:93 +0x3d6
cmd/compile/internal/escape.(*escape).stmt(0xc001265e60, {0xe98650, 0xc0004ec370?})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/stmt.go:143 +0x3b8
cmd/compile/internal/escape.(*escape).stmts(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/stmt.go:190
cmd/compile/internal/escape.(*escape).block(0xc001265e60?, {0xc000696e00?, 0x3?, 0xc0005b6a20?})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/stmt.go:197 +0x72
cmd/compile/internal/escape.(*batch).walkFunc(0xc0005b6a20, 0xc000d9c160)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/escape.go:227 +0xe5
cmd/compile/internal/escape.Batch({0xc001265e28?, 0x2, 0x3}, 0x0?)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/escape.go:143 +0x228
cmd/compile/internal/ir.(*bottomUpVisitor).visit(0xc00124b7d0, 0xc000d9c160)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/scc.go:127 +0x2f0
cmd/compile/internal/ir.(*bottomUpVisitor).visit.func1({0xe99398?, 0xc000d9c160?})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/scc.go:81 +0x45
cmd/compile/internal/ir.(*bottomUpVisitor).visit.func2({0xe9a018, 0xc000d8ba00})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/scc.go:91 +0x7e
cmd/compile/internal/ir.Visit.func1({0xe9a018, 0xc000d8ba00})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:105 +0x30
cmd/compile/internal/ir.(*CallExpr).doChildren(0xc000cd6ea0, 0xc00126ed38)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/node_gen.go:260 +0x56
cmd/compile/internal/ir.DoChildren(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:94
cmd/compile/internal/ir.Visit.func1({0xe98a38, 0xc000cd6ea0})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:106 +0x57
cmd/compile/internal/ir.(*AssignStmt).doChildren(0xc000d82640, 0xc00126ed38)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/node_gen.go:152 +0x7c
cmd/compile/internal/ir.DoChildren(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:94
cmd/compile/internal/ir.Visit.func1({0xe98650, 0xc000d82640})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:106 +0x57
cmd/compile/internal/ir.doNodes(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/node_gen.go:1512
cmd/compile/internal/ir.(*Func).doChildren(0xe99398?, 0xc00126ed38?)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/func.go:156 +0x6e
cmd/compile/internal/ir.DoChildren(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:94
cmd/compile/internal/ir.Visit.func1({0xe99398, 0xc000d9cdc0})
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:106 +0x57
cmd/compile/internal/ir.Visit({0xe99398, 0xc000d9cdc0}, 0xc001251c40)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/visit.go:108 +0xb8
cmd/compile/internal/ir.(*bottomUpVisitor).visit(0xc00124b7d0, 0xc000d9cdc0)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/scc.go:87 +0x1c8
cmd/compile/internal/ir.VisitFuncsBottomUp({0xc000517000, 0x87, 0x2?}, 0xd522c8)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/ir/scc.go:60 +0x105
cmd/compile/internal/escape.Funcs(...)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/escape/escape.go:118
cmd/compile/internal/gc.Main(0xd521b0)
        /home/karitham/sdk/gotip/src/cmd/compile/internal/gc/main.go:277 +0xe30
main.main()
        /home/karitham/sdk/gotip/src/cmd/compile/main.go:55 +0xdd

FAIL    github.com/Karitham/corde [build failed]

@randall77 randall77 reopened this Feb 16, 2022
@danscales
Copy link
Contributor

@danscales danscales commented Feb 17, 2022

This relates to importing a complex recursive type. This issue only happens if the recursion happens through the type constraint. (In this program, the recursion actually goes through two different type constraints!)

I have the rough fix (small changes to the importers), but creating a minimized test case may take a while. I believe this is not a release-blocker, because of the unusual situation of recursion through the type constraint.

@danscales danscales self-assigned this Feb 17, 2022
@danscales danscales changed the title cmd/compile: internal compiler error cmd/compile: internal compiler error importing a recursive type (recursive through a type constraint) Feb 17, 2022
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 17, 2022

Change https://go.dev/cl/386335 mentions this issue: cmd/compile: fix importers to deal with recursion through type constraints

@findleyr
Copy link
Contributor

@findleyr findleyr commented Feb 18, 2022

I went to port this fix to x/tools/go/internal/gcimporter, and observed that it doesn't reproduce with the x/tools importer/exporter. @danscales mentioned something similar, that the error was not locally reproducible with the types2 importer. This suggests that there's some inconsistency across the types/import/export APIs. Reopening because we should nail down the root cause.

@findleyr findleyr reopened this Feb 18, 2022
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 24, 2022

Change https://go.dev/cl/387814 mentions this issue: test: add new test case for 51219 that triggers the types2 issue

gopherbot pushed a commit that referenced this issue Feb 28, 2022
The existing test for 51219 didn't actually trigger the types2 issue - I
hadn't been able to minimize the test case yet properly. This new test
case issue51219b.go now does trigger the types2 issue (it's only
slightly different).

Updates #51219

Change-Id: Iaba8144b4702ff4fefec86c899b8acef127b10dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/387814
Trust: Dan Scales <danscales@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 14, 2022

Change https://go.dev/cl/392475 mentions this issue: go/internal/gcimporter: guard against infinite recursion with recursive

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 14, 2022

Change https://go.dev/cl/392634 mentions this issue: gopls: update go.mod file for v0.8.1

gopherbot pushed a commit to golang/tools that referenced this issue Mar 14, 2022
For golang/go#51219

Change-Id: I62982e0ea084f78e81ec5ea6d6a0b6a039941968
Reviewed-on: https://go-review.googlesource.com/c/tools/+/392634
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 14, 2022

Change https://go.dev/cl/392734 mentions this issue: go/internal/gcimporter: add a test case for issue 51219

gopherbot pushed a commit to golang/tools that referenced this issue Mar 14, 2022
type parameters

This is a partial port of CL 386335 to x/tools. I have not yet been able
to reproduce the crash in a test.

For golang/go#51219

Change-Id: I262e6a9dba936b18513ee5f11a2a72d4155d3833
Reviewed-on: https://go-review.googlesource.com/c/tools/+/392475
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 15, 2022

Change https://go.dev/cl/392915 mentions this issue: gopls: update go.mod file to pick up CL 392914

gopherbot pushed a commit to golang/tools that referenced this issue Mar 15, 2022
For golang/go#51219

Change-Id: Ib60fc1f22ce9ff094bc87ec36ad9e2c07c422c6e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/392915
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

6 participants