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

go/types, types2: self-contradictory error message when separating the definition and implementation of an interface with anonymous structs in a method signature #54258

Open
guamoko995 opened this issue Aug 4, 2022 · 3 comments
Labels
compiler/runtime NeedsInvestigation
Milestone

Comments

@guamoko995
Copy link

@guamoko995 guamoko995 commented Aug 4, 2022

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

$ go version
go version go1.19 linux/amd64

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="amd64"
GOBIN=""
GOCACHE="/home/guamoko95/.cache/go-build"
GOENV="/home/guamoko95/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/guamoko95/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/guamoko95/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/guamoko95/go/src/play/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3021280297=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import "play/implementation"

// interface declaration with anonymous non-empty structure in signature

type AnonNonEmptyStructGeter interface {
	AnonNonEmptyStructGet() struct{ string }
}

func main() {
	var w AnonNonEmptyStructGeter
	w = implementation.ImpAnonNonEmptyStructGeter{}
}

play/implementation:

package implementation

// interface implementation with anonymous non-empty struct in signature
type ImpAnonNonEmptyStructGeter struct{}

func (ImpAnonNonEmptyStructGeter) AnonNonEmptyStructGet() struct{ string } {
	return struct{ string }{""}
}

What did you expect to see?

successful compilation or compilation error message indicating a problem. For example: "anonymous types in different packages"

What did you see instead?

self-contradictory error message:

# command-line-arguments
./main.go:13:6: cannot use implementation.WithMetodWithAnonStruct{} (value of type implementation.WithMetodWithAnonStruct) 
as type WithAnonNonEmptyStructer in assignment:
        implementation.WithMetodWithAnonStruct does not implement WithAnonNonEmptyStructer (wrong type for WithAnonStruct method)
                have WithAnonStruct() struct{string}
                want WithAnonStruct() struct{string}
@guamoko995 guamoko995 changed the title affected/package: go/build go/build: self-contradictory error message Aug 4, 2022
@guamoko995 guamoko995 changed the title go/build: self-contradictory error message go/build: self-contradictory error message when separating the definition and implementation of an interface with anonymous types in a method signature. Aug 4, 2022
@guamoko995 guamoko995 changed the title go/build: self-contradictory error message when separating the definition and implementation of an interface with anonymous types in a method signature. go/build: self-contradictory error message when separating the definition and implementation of an interface with anonymous structs in a method signature. Aug 4, 2022
@dmitshur dmitshur added NeedsInvestigation compiler/runtime labels Aug 4, 2022
@dmitshur dmitshur added this to the Backlog milestone Aug 4, 2022
@dmitshur dmitshur changed the title go/build: self-contradictory error message when separating the definition and implementation of an interface with anonymous structs in a method signature. cmd/compile: self-contradictory error message when separating the definition and implementation of an interface with anonymous structs in a method signature Aug 4, 2022
@dmitshur
Copy link
Contributor

@dmitshur dmitshur commented Aug 4, 2022

CC @golang/compiler.

@mdempsky
Copy link
Member

@mdempsky mdempsky commented Aug 4, 2022

struct{string} denotes different types when it appears in different packages. So failing to type check is the correct behavior here.

Agreed the error message is unfortunate for not being able to disambiguate that though.

/cc @griesemer @findleyr

@mdempsky mdempsky changed the title cmd/compile: self-contradictory error message when separating the definition and implementation of an interface with anonymous structs in a method signature go/types, types2: self-contradictory error message when separating the definition and implementation of an interface with anonymous structs in a method signature Aug 4, 2022
@findleyr
Copy link
Contributor

@findleyr findleyr commented Aug 4, 2022

Ack, this is unfortunate and confusing. It would be good to clarify that the identity of the string field does not match here.

I am not sure how high a priority this is, but we do want to continue to improve error messages in the new type checker. (I have not yet checked whether the old type checker produced a better message here).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

4 participants