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

affected/package: compiler #51608

Closed
xiazemin opened this issue Mar 11, 2022 · 9 comments
Closed

affected/package: compiler #51608

xiazemin opened this issue Mar 11, 2022 · 9 comments

Comments

@xiazemin
Copy link

xiazemin commented Mar 11, 2022

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

$ go version
go version go1.18beta2 darwin/arm64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/xiazemin/Library/Caches/go-build"
GOENV="/Users/xiazemin/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/xiazemin/.gvm/pkgsets/go1.18beta2/global/pkg/mod"
GONOPROXY="git.shimo.im"
GONOSUMDB="git.shimo.im"
GOOS="darwin"
GOPATH="/Users/xiazemin/.gvm/pkgsets/go1.18beta2/global"
GOPRIVATE="git.shimo.im"
GOPROXY="https://goproxy.cn"
GOROOT="/Users/xiazemin/.gvm/gos/go1.18beta2"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/xiazemin/.gvm/gos/go1.18beta2/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.18beta2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/xiazemin/source/template_learn/generic/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/hb/1q06vpss7_d1fw3ymxb1ztbr0000gn/T/go-build1495064095=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

type Student struct {
	Name string
}
type Teacher struct {
	Name string
}

func ShowStudent[T Student](s T) {
	fmt.Println(s.Name)
}

func Show[T Student | Teacher](s T) {
	fmt.Println(s.Name)
}

var st Student
st.Name = "hhh"
Show[Student](st)  //this can work

// ShowStudent[Student](st)
//./gen.go:132:14: Student does not implement Student (Student is not an interface)

What did you expect to see?

the two functions can work as the same

What did you see instead?

Show[Student](st) works fine
but ShowStudent[Student](st) not work
./gen.go:132:14: Student does not implement Student (Student is not an interface)

@ianlancetaylor
Copy link
Member

I can't recreate the problem on tip. Perhaps it has been fixed, or perhaps I don't understand your description.

Please show a complete failing example on the Go playground at https://go.dev/play. Click on "Go release" and change it to "Go dev branch" to get the current sources. Thanks.

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 11, 2022
@xiazemin
Copy link
Author

xiazemin commented Mar 11, 2022

the version of Go dev branch is. devel go1.19-5a040c5a36 Thu Mar 10 09:12:04 2022 +0000
my test version is go1.18beta2 darwin/arm64
so I have this problem

in go 1.18

type Student struct {
	Name string
}
func ShowStudent[T Student](s T) {
	fmt.Println(s.Name)
}

works fine.

but in go1.19
there is a compile error
./prog.go:18:16: s.Name undefined (type T has no field or method Name)

my example is

// You can edit this code!
// Click here and start typing.
package main

import (
	"fmt"
	"runtime"
)

type Student struct {
	Name string
}
type Teacher struct {
	Name string
}

func ShowStudent[T Student](s T) {
	fmt.Println(s.Name)
}

func Show[T Student | Teacher](s T) {
	fmt.Println(s.Name)
}

func main() {
	fmt.Println(runtime.Version())
	var st Student
	st.Name = "hhh"
	Show[Student](st) //this can work

	ShowStudent[Student](st)
}

@ianlancetaylor
Copy link
Member

./prog.go:18:16: s.Name undefined (type T has no field or method Name)

This was a late change we made to the language spec and compiler for 1.18. See #51576. s.Name is no longer permitted even if all types in the type set have a Name field. Sorry.

@xiazemin
Copy link
Author

will you fix the problem in go1.18?

func Show[T Student](s T)
func Show[T Student | Teacher](s T)

when I call it with

Show[Student](st)

only the second function work ,it is confusing

@ianlancetaylor
Copy link
Member

I'm sorry, I don't understand exactly what you are saying.

The test case https://go.dev/play/p/Ti1vlIRR8HO?v=gotip, which is based on your test case, works for me using the future Go 1.18.

Do you have a different test case that fails?

@xiazemin
Copy link
Author

yes, It works on go1.19-5a040c5a36
but on go1.18beta2 it faild
image

@davecheney
Copy link
Contributor

@xiazemin you must not post screenshots. Please use links to go.dev, or if that is unavailable to you, text. Thank you in advance for your understanding.

@xiazemin
Copy link
Author

@xiazemin
Copy link
Author

it has been fixed in go1.18rc1

@mengzhuo mengzhuo removed the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 14, 2022
@golang golang locked and limited conversation to collaborators Mar 14, 2023
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

5 participants