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/go2go: cannot use make(map[int]X) (value of type map[int]X) as map[int]X value in struct literal #39982

Closed
arl opened this issue Jul 1, 2020 · 10 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@arl
Copy link
Contributor

arl commented Jul 1, 2020

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

$ go version
go version devel +d014fca6d4 Wed Jul 1 20:30:10 2020 +0000 linux/amd64

Does this issue reproduce with the latest release?

It does with latest commit of the dev.go2go experimental branch: d014fca

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/aurelien/.cache/go-build"
GOENV="/home/aurelien/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/aurelien/godev/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/aurelien/godev"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/aurelien/dev/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/aurelien/dev/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build789085526=/tmp/go-build -gno-record-gcc-switches"

What did you do?

A bit of context: I'm currently experimenting with the generics draft by trying to use it to develop a generics cache library.
I compacted the code into a single-file reproducer to make debugging easier but the actual code imports various packages, among which the list package of the dev.go2go source tree (generic replacement for container/list).

The original cache is based off a simple map where the value is a *list.Element(*entry(K).

package main

type (
	Element(type TElem) struct{}

	entry(type K comparable) struct{}

	Cache(type K comparable) struct {
		data map[K]*Element(*entry(K))
	}
)

func main() {
	_ = Cache(int){
		data: make(map[int](*Element(*entry(int)))), // cannot use make(map[int](*Element(*entry(int)))) (value of type map[int]*Element(*entry(int))) as map[int]*Element(*entry(int)) value in struct literal
	}
}

It does the same on the playground: https://go2goplay.golang.org/p/qoMgsPA5ifd

What did you expect to see?

a successful compilation since the provided map is, AFAICT, of the same type as the Cache.data type.

What did you see instead?

The error reported above:

cannot use make(map[int](*Element(*entry(int)))) (value of type map[int]*Element(*entry(int))) as map[int]*Element(*entry(int)) value in struct literal
@arl
Copy link
Contributor Author

arl commented Jul 1, 2020

It seems the translator sees 2 types whereas there's only one.

package main

type (
	Element(type TElem) struct{}

	entry(type K comparable) struct{}

	Cache(type K comparable) struct {
		data map[K]*Element(*entry(K))
	}
)

func main() {
	_ = Cache(int){}
	_ = make(map[int](*Element(*entry(int))))
}

gives:

# play
./prog.go2:6: instantiate୦୦Element୦୮1main୮aentry୮8int୮9 redeclared in this block
	previous declaration at ./prog.go2:10

playground: https://go2goplay.golang.org/p/B-cdj3yBgAw

Both errors (the one in the top post and this one) might be 2 faces of the same coin. In case they aren't let me know if I should open a different issue. Thanks

@arl arl changed the title cmd/go2go: cannot use make(map[int](*X) (value of type map[int]*X) as map[int]*X value in struct literal cmd/go2go: cannot use make(map[int]X) (value of type map[int]X) as map[int]X value in struct literal Jul 1, 2020
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 1, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Jul 1, 2020
@ianlancetaylor
Copy link
Contributor

This seems like a failure in the type checker. At least, I don't see why it shouldn't work.

@griesemer
Copy link
Contributor

Definitely a type-checker failure.

@gopherbot
Copy link

Change https://golang.org/cl/240737 mentions this issue: [dev.go2go] go/types: fix computation of type hash

gopherbot pushed a commit that referenced this issue Jul 2, 2020
Fixes #39982.

Change-Id: I4e7b52c34bf8df63f2063dc2504a8125ca7585a0
Reviewed-on: https://go-review.googlesource.com/c/go/+/240737
Reviewed-by: Robert Griesemer <gri@golang.org>
@griesemer
Copy link
Contributor

Fixed on dev.go2go.

@arl
Copy link
Contributor Author

arl commented Jul 2, 2020

I just tested with 85ed317 and now both listing (top and 2nd comments of this issue) give the same error:

$ go tool go2go run issue39982.go2
# command-line-arguments
./issue39982.go2:25: instantiate୦୦Element୦୮1main୮aentry୮8int୮9 redeclared in this block
        previous declaration at ./issue39982.go2:29
/home/aurelien/dev/go/bin/go [run issue39982.go] failed: exit status 2

Let me know whether you prefer a new issue for this.

@griesemer
Copy link
Contributor

@arl, thanks for this. I admit I only tested type-checking for this case. Reopening.

@griesemer griesemer reopened this Jul 2, 2020
@griesemer
Copy link
Contributor

@ianlancetaylor Not sure if this is still a type-checker issue or a translator issue at this point.

@ianlancetaylor
Copy link
Contributor

Thanks. Translator is fixed on dev.go2go branch.

@gopherbot
Copy link

Change https://golang.org/cl/240899 mentions this issue: [dev.go2go] go/go2go: resolve pointers in sameType

gopherbot pushed a commit that referenced this issue Jul 3, 2020
Fixes #39982

Change-Id: Ieb33381716662b44464104e0073a52ab5c19b7c6
Reviewed-on: https://go-review.googlesource.com/c/go/+/240899
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Jul 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants