Go: cmd/compile: "invalid recursive type" error is not as helpful as go/types

Created on 23 Sep 2020  路  5Comments  路  Source: golang/go

$ go version
go version devel +150bd4ffd4 Wed Sep 23 07:51:17 2020 +0000 linux/amd64
$ cat recursive.go 
package p

type T1 struct {
    f2 T2
}

type T2 struct {
    f1 T1
}
$ go build recursive.go 
# command-line-arguments
./recursive.go:7:6: invalid recursive type T2
$ go build -o gotype $(go env GOROOT)/src/go/types/gotype.go
$ ./gotype recursive.go 
recursive.go:3:6: illegal cycle in declaration of T1
recursive.go:3:6:   T1 refers to
recursive.go:7:6:   T2 refers to
recursive.go:3:6:   T1

I had a cyclic type involving four types across three files, so I really did have to reach for gotype to understand what was going on. I think we should improve the compiler's typechecker to provide a similarly detailed error.

cc @griesemer @mdempsky @cuonglm

NeedsInvestigation

Most helpful comment

We are seriously investigating using a revamped/adjusted go/types for the compiler in the future. The result should be consistent and better error messages from both, the compiler and the type checker.

All 5 comments

Also cc @findleyr given his recent work on go/types

We are seriously investigating using a revamped/adjusted go/types for the compiler in the future. The result should be consistent and better error messages from both, the compiler and the type checker.

Change https://golang.org/cl/256837 mentions this issue: cmd/compile: make invalid recursive type error for struct clearer

Change https://golang.org/cl/258177 mentions this issue: cmd/compile: report type loop for invalid recursive types

In Go 1.16, cmd/compile will report:

recursive.go:3:6: invalid recursive type T1
    recursive.go:3:6: T1 refers to
    recursive.go:7:6: T2 refers to
    recursive.go:3:6: T1
Was this page helpful?
0 / 5 - 0 ratings