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: compile error while using generics #63981

Closed
qqxhb opened this issue Nov 7, 2023 · 3 comments
Closed

cmd/compile: compile error while using generics #63981

qqxhb opened this issue Nov 7, 2023 · 3 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@qqxhb
Copy link

qqxhb commented Nov 7, 2023

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

$ 1.20.10

Does this issue reproduce with the latest release?

no

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

go env Output
$ GO111MODULE="on"
GOARCH="amd64"
GOBIN=""

GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""

GOOS="darwin"

GOSUMDB="sum.golang.google.cn"
GOTMPDIR=""
GOVCS=""
GOVERSION="go1.20.10"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qg/7jl0rh4502v1w663txn71wnm0000gp/T/go-build2599608933=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Implement generic interfaces

type DO struct {
}

func (d *DO) Delete(models ...interface{}) (info interface, err error) {
	return nil, nil
}

type IGenericsDo[T IGenericsDo[T, E], E any] interface {
	Delete(...E) (info interface, err error)
}

func NewGenericsDo[T IGenericsDo[T, E], E any](realDo T) IGenericsDo[T, E] {
	return &genericsDo[T, E]{realDo: realDo}
}

type genericsDo[T IGenericsDo[T, E], E any] struct {
	realDo T
	DO
}

func (b *genericsDo[T, E]) Delete(models ...E) (result interface, err error) {
	return b.realDo.Delete(models...)
}

compile err: Cannot use '&genericsDo[T, E]{realDo: realDo}' (type *genericsDo[T, E]) as the type IGenericsDo[T, E] Type does not implement 'IGenericsDo[T, E]' need the method: Delete(...E) (info interface{}, err error) have the method: Delete(models ...interface{}) (info interface{}, err error)

What did you expect to see?

What did you see instead?

When I remove the inherited DO, it work.

type IGenericsDo[T IGenericsDo[T, E], E any] interface {
	Delete(...E) (info interface, err error)
}

func NewGenericsDo[T IGenericsDo[T, E], E any](realDo T) IGenericsDo[T, E] {
	return &genericsDo[T, E]{realDo: realDo}
}

type genericsDo[T IGenericsDo[T, E], E any] struct {
	realDo T
	//DO
}

func (b *genericsDo[T, E]) Delete(models ...E) (result interface, err error) {
	return b.realDo.Delete(models...)
}
@Jorropo
Copy link
Member

Jorropo commented Nov 7, 2023

Are you using gccgo ?
With dc74a3d 1.21 and 1.20.10 I get:

./prog.go:12:6: invalid recursive type: IGenericsDo refers to itself
./prog.go:26:18: b.realDo.Delete undefined (type T has no field or method Delete)

Which looks correct to me.

@heschi
Copy link
Contributor

heschi commented Nov 7, 2023

cc @golang/compiler

@heschi heschi added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 7, 2023
@heschi heschi added this to the Backlog milestone Nov 7, 2023
@heschi heschi added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Nov 7, 2023
@heschi heschi changed the title affected/package: generic cmd/compile: compile error while using generics Nov 7, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 7, 2023
@griesemer
Copy link
Contributor

  1. I assume you meant (info interface{}, err error) in the signature of Do.Delete? As written, (info interface, err error) is not valid syntax.

  2. After fixing the syntax, I agree with @Jorropo . This program is not correct.

I am going to close this. If you believe this is a mistake and there's a real issue here, please re-open with a small, syntactically correct reproducer. Thanks.

As an aside, gccgo's generics implementation is not sufficiently far along yet I believe to be useable in general (@ianlancetaylor , please correct me if I am mistaken).

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. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants