Skip to content

cmd/go2go: Translation fails when inferring a aliased generic type #39797

@firelizzard18

Description

@firelizzard18

If package a defines type S as a generic type, package b defines type S = b.S, if a expression of type S is passed to a generic function without specifying the function's type parameters (relying on inference), go tool go2go build will fail in a peculiar way. It does not fail if the type parameter is passed explicitly.

package a

type S(type T) struct {
	F T
}
package b

import "./a"

type S = a.S

func F1(type T)(T) { }

// fails
func F2() { F1(S(string){}) }

// compiles
func F3() { F1(S(string))(S(string){}) }

Replacing return err with panic(err) on rewrite.go:361 in rewriteAST produces the following:

panic: cannot find package "a" in any of:
        /usr/lib/go-tip/src/a (from $GOROOT)
        /home/firelizzard/go/src/a (from $GOPATH)

goroutine 1 [running]:
go/go2go.rewriteAST(0xc00010c240, 0xc000024420, 0x0, 0x0, 0xc0000714f0, 0xc00011c180, 0x7f44c7755201, 0xc00010c2c0, 0x816bc0)
        /usr/lib/go-tip/src/go/go2go/rewrite.go:361 +0x23b5
go/go2go.rewriteFile(0x693853, 0x1, 0xc00010c240, 0xc000024420, 0x0, 0x0, 0xc0000714f0, 0xc000012438, 0x6, 0xc00011c180, ...)
        /usr/lib/go-tip/src/go/go2go/rewrite.go:221 +0xb8
go/go2go.rewriteFilesInPath(0xc000024420, 0x0, 0x0, 0x693853, 0x1, 0xc00000c1c0, 0x1, 0x2, 0x0, 0x0, ...)
        /usr/lib/go-tip/src/go/go2go/go2go.go:104 +0xc11
go/go2go.rewriteToPkgs(0xc000024420, 0x0, 0x0, 0x693853, 0x1, 0xc00000c1a0, 0xc0000748d0, 0xc0000748a0, 0xc000074870, 0xc000074840)
        /usr/lib/go-tip/src/go/go2go/go2go.go:46 +0x165
go/go2go.Rewrite(...)
        /usr/lib/go-tip/src/go/go2go/go2go.go:30
main.translate(0xc000024420, 0x693853, 0x1)
        /usr/lib/go-tip/src/cmd/go2go/translate.go:15 +0x47
main.main()
        /usr/lib/go-tip/src/cmd/go2go/main.go:69 +0x9ec
exit status 2

During *translator.translate, *translator.addTypePackages adds typ.Obj().Pkg() to t.typePackages, which is to generate a list of import statements. Because typ.Obj().Pkg().Path() only includes the package name ("a") and not the full path, the generated import statement is just import "a", and that causes problems. This can eventually be traced back to the fact that go/go2go.parseFiles uses the package <name> as the package path instead of a full path.

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

$ go version
go version devel +da7932368b Mon Jun 22 19:06:44 2020 +0000 linux/amd64

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

go env Output
$ go env
GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"

What did you do?

  • git clone https://gitlab.com/firelizzard/go-iter
  • cd go-iter/issues/i2
  • go tool go2go build

What did you expect to see?

A successful build

What did you see instead?

cannot find package "a" in any of:
        /usr/lib/go-tip/src/a (from $GOROOT)
        /home/REDACTED/go/src/a (from $GOPATH)

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions