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: error using generics: "incompatible type: cannot use n (variable of type MyType[T]) as T value" #48543

Closed
neclepsio opened this issue Sep 22, 2021 · 2 comments

Comments

@neclepsio
Copy link

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

$ go version
go version devel go1.18-051df0d722 Wed Sep 22 03:45:00 2021 +0000 windows/amd64

Does this issue reproduce with the latest release?

N/A

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

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=[...]
set GOENV=[...]
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=[...]
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=[...]
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=[...]
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=[...]
set GOVCS=
set GOVERSION=devel go1.18-051df0d722 Wed Sep 22 03:45:00 2021 +0000
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=[...]
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=[...]/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

type MyType[T any] struct{}

func (m MyType[T]) F1(arg T) {}

func (m MyType[T]) F2() {
    n := MyType[T]{}
    m.F1(n)
}

What did you expect to see?

Successful compilation, since MyType[T] implements any.

What did you see instead?

incompatible type: cannot use n (variable of type MyType[T]) as T value

I'm not sure this is a bug, but I even if I read the design document, I don't really understand why I have this error. Even using https://golang.org/cl/350697 gave me no reason.

@danscales
Copy link
Contributor

This is not a bug. In method F2, the typechecker must be able to show that the the type of n (which is MyType[T]) is compatible with the required type of the argument of F1, which is T, for any possible value of T. This is not true generally, and I don't think there any value of T for which it is true specifically. To be concreate, consider T=int. MyType[int] is a defined type which is completely distinct from int, and so can not be used as an argument type where int is required.

Possibly your confusion is that the value of T, even though not constrained (because of the any), must be passed along the same when F2 calls F1. The value of T cannot change as you go from F2 to F1.

@neclepsio
Copy link
Author

Yes, that's clear now. Thank you.

@golang golang locked and limited conversation to collaborators Sep 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants