Skip to content

cmd/compile: cannot compile with alias type used in multiple files #50925

@mandelsoft

Description

@mandelsoft

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

$ go version
go version go1.17.6 linux/amd64

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=""
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="some path"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/contrib/inst/go1.17.6"
GOSUMDB="off"
GOTMPDIR=""
GOTOOLDIR="/usr/local/contrib/inst/go1.17.6/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
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-build3458372569=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I have two go file in a package, one with a type definitial and an alias declaration

package compilererror

type A struct {
	c C
}

type O  = A

and a second file with a used type and a using type

package compilererror

type Dummy interface {
//	Dummy(A)
}

type B interface {
	// cannot use O here, because compiler crashes
        // F0(*A)
	F1() (*O, error)
	F2() (*O, error)
}

type C interface {
	F3(*O) error
}

What did you expect to see?

Compiling this package succeeds.

What did you see instead?

Compiling this package results in a compiler error:

compilererror/c.go:21:6: internal compiler error: cannot handle alias type declaration (issue #25838): %!v(PANIC=Format method: runtime error: invalid memory address or nil pointer dereference)

Please file a bug report including a short program that triggers the error.
https://golang.org/issue/new

With go release 1.16 a similar error is reported but with a working error message (not the nil pointer exception) complaining about the type alias.

Possibilities to circumvent this problem.

There are several strange ways to avoid this compile error:

  • add a dummy interface (as shown in b.go) USING the original type instead of the alias (just uncomment this line)
  • replace the FIRST occurrence of the alias type (O) in a file by the original type (A) (or just uncomment F0)
  • rename the file c.go to be alphabetically before b.c (or just e.g. a.go)
  • put all the stuff in one file

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions