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: confusion about use a specific type for the receiver of a parameterized type in generic struct method #60017

Closed
almas1992 opened this issue May 6, 2023 · 4 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@almas1992
Copy link

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

$ go version
go version go1.20.4 darwin/arm64

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="arm64"
GOBIN=""
GOCACHE="/Users/xxx/Library/Caches/go-build"
GOENV="/Users/xxx/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/xxx/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/xxx/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/opt/homebrew/Cellar/go/1.20.4/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.20.4/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
AR="ar"
CC="cc"
CXX="c++"
CGO_ENABLED="1"
GOMOD=""
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/zp/b8twgj314bsg2t1wfz_9yszh0000gn/T/go-build2384698175=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

package main

import (
	"fmt"
)

type MySlice[T int | float32] []T

func (s MySlice[string]) Sum() string {
	var sum string
	for _, value := range s {
		sum += value
	}
	return sum
}

func main() {
	var s MySlice[float32] = []float32{1.1, 2.2, 3.3, 4.4, 5.5}
	fmt.Printf("%T %v\n", s.Sum(), s.Sum())

}

What did you expect to see?

According to the https://go.dev/doc/faq#types_in_method_declaration I should see compilation errors

What did you see instead?

float32 16.5

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label May 6, 2023
@randall77
Copy link
Contributor

Your method declaration is redefining the string token to be the type parameter. It is not instantiating with the string type.

Dup of #48123

@ianlancetaylor
Copy link
Contributor

@almas1992
Copy link
Author

https://go.dev/doc/faq#types_in_method_declaration

As I already said, according to this linked article description, the compilation should prompt an error. Then this URL I have also written in the issue.

@griesemer
Copy link
Contributor

No, there should not be an error, even according to the linked article. Note that in

func (s MySlice[string]) Sum() string { ...

MySlice is a a slice of strings, but here the name string does not mean the predeclared type string. It is the name (which happens to be string) of the type parameter declared through MySlice[string]. The important point here is that this receiver is a declaration of a local (i.e., for this method) name for the type parameter T in the struct.

As has been pointed out before, this is working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

5 participants