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: ambiguous selector with generic interface & embedded types #53419

Closed
phemmer opened this issue Jun 17, 2022 · 12 comments
Closed

cmd/compile: ambiguous selector with generic interface & embedded types #53419

phemmer opened this issue Jun 17, 2022 · 12 comments
Assignees
Labels
FrozenDueToAge generics Issue is related to generics NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@phemmer
Copy link

phemmer commented Jun 17, 2022

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

$ go version
go version go1.18.3 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
GOARCH="amd64"
GOOS="linux"

What did you do?

package main

import "fmt"

type MyString string
type MyByteSlice []byte
type Both struct {
	MyString
	MyByteSlice
}

func (s1 MyString) Concat(s2 MyString) MyString            { return s1 + s2 }
func (bs1 MyByteSlice) Concat(bs2 MyByteSlice) MyByteSlice { return append(bs1, bs2...) }
func (b1 Both) Concat(b2 Both) Both {
	return Both{
		b1.MyString.Concat(b2.MyString),
		b1.MyByteSlice.Concat(b2.MyByteSlice),
	}
}

type Concatable[T any] interface {
	Concat(T) T
}

func Repeat[T Concatable[T]](c T) T {
	return c.Concat(c)
}

func main() {
	s := MyString("foo")
	bs := MyByteSlice("foo")
	b := Both{MyString("foo"), MyByteSlice("foo")}
	fmt.Println(Repeat(s))
	fmt.Println(Repeat(bs))
	fmt.Println(Repeat(b))
}

https://go.dev/play/p/Jyws-pz9k5X

What did you expect to see?

successful compilation

What did you see instead?

./prog.go:26:10: ambiguous selector c.Concat

Go build failed.

The error is with the Both type (as if I remove it, the issue goes away). However on Both, Concat is not ambiguous. Both has it's own Concat method, and should not be trying to fall through to either of the embedded types.

@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 17, 2022
@thanm
Copy link
Contributor

thanm commented Jun 17, 2022

@griesemer

@griesemer griesemer added this to the Go1.20 milestone Jun 17, 2022
@griesemer griesemer added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 17, 2022
@griesemer
Copy link
Contributor

Simplified reproducer:

package main

type T1 struct{}
type T2 struct{}
type Both struct {
	T1
	T2
}

func (T1) m()   {}
func (T2) m()   {}
func (Both) m() {}

func f[T interface{ m() }](c T) {
	c.m()
}

func main() {
	var b Both
	// b.m()         // <<< this (without the f(b) below) is ok
	f(b)
}

The type checker (types2) doesn't have a problem with this, the error appears to arrive later, in the noder or after instantiation in the compiler (have not yet investigated).

@mdempsky Can you confirm that this problem is present/absent with unified IR?

@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 17, 2022
@mdempsky
Copy link
Member

@mdempsky Can you confirm that this problem is present/absent with unified IR?

Yeah, the test program appears to run correctly with GOEXPERIMENT=unified go run.

@griesemer griesemer added the generics Issue is related to generics label Jun 17, 2022
@griesemer
Copy link
Contributor

Thanks, @mdempsky .
Unless we hear otherwise from users, we can probably just wait for the unified IR to fix this in 1.20.
Assigning to you and I for now so it's on the radar.

@gopherbot
Copy link

Change https://go.dev/cl/413014 mentions this issue: test: add regress test for #53419

gopherbot pushed a commit that referenced this issue Jun 17, 2022
This currently works with GOEXPERIMENT=unified. Add a regress test to
make sure it stays that way.

Updates #53419.

Change-Id: I2ea1f9039c59807fbd497d69a0420771f8d6d035
Reviewed-on: https://go-review.googlesource.com/c/go/+/413014
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
@gopherbot
Copy link

Change https://go.dev/cl/413977 mentions this issue: [dev.unified] all: merge master (5a1c5b8) into dev.unified

gopherbot pushed a commit that referenced this issue Jun 24, 2022
Conflicts:

- test/run.go

  Textual conflict adding to the known failures list for the nounified
  frontend.

Merge List:

+ 2022-06-24 5a1c5b8 cmd/go: add per-package indexing for modules outside mod cache
+ 2022-06-24 b9c4d94 cmd/go/internal/list: update help info with Deprecated field
+ 2022-06-24 73475ef go/types, types2: print qualified object names in cycle errors
+ 2022-06-24 3e58ef6 go/types, types2: better errors for == when type sets are empty
+ 2022-06-24 d38f1d1 doc/go1.19: Linux race detector now requires glibc 2.17
+ 2022-06-23 de5329f debug/dwarf: handle malformed line table with bad program offset
+ 2022-06-23 15605ca embed: document additional file name restrictions
+ 2022-06-22 2e773a3 test: add test that causes gofrontend crash
+ 2022-06-22 ff17b7d cmd/compile: don't use dictionary convert to shaped empty interface
+ 2022-06-22 2a3b467 cmd/go: make module .zip files group/world readable
+ 2022-06-22 bdab4cf cmd/go, cmd/link: support failure to create _cgo_import.go
+ 2022-06-22 aca37d1 cmd/go: avoid indexing modules in GOROOT
+ 2022-06-22 111cdb5 all: update to current golang.org/x/sys revision
+ 2022-06-22 4045b1b cmd/compile: fix assert condition in generic method call
+ 2022-06-22 6bad7e8 compress/gzip: always close bodyReader in Example_compressingReader
+ 2022-06-22 606c6c3 encoding/xml: check nil pointer in DecodeElement
+ 2022-06-22 f571518 cmd/cgo: dont override declared struct type
+ 2022-06-22 92c9b81 net: don't set netGo = true on Windows with no cgo
+ 2022-06-22 be0b2a3 cmd/trace: add basic documentation to main page
+ 2022-06-22 b004c73 go/types, types2: fix parameter order dependence in type inference
+ 2022-06-21 f2c7e78 spec: document operations which accept []byte|string constrained types
+ 2022-06-21 ab422f2 runtime/trace: ignore fallback stacks in test
+ 2022-06-21 66685fb doc/go1.19: use correct link to sync/atomic docs
+ 2022-06-21 4b236b4 runtime: convert flaky semaphore linearity test into benchmark
+ 2022-06-21 530511b cmd/go/internal/modindex: avoid walking modules when not needed
+ 2022-06-21 c2d373d cmd/compile: allow 128-bit values to be spilled
+ 2022-06-21 19ed442 test: add regress test for #53477
+ 2022-06-20 3fcbfb0 doc/go1.19: fix HTML validation issues
+ 2022-06-18 527ace0 cmd/compile: skip substituting closures in unsafe builtins arguments
+ 2022-06-17 ec58e3f test: add regress test for #53419
+ 2022-06-17 103cc66 cmd/go/internal/modfetch: prevent duplicate hashes in go.sum
+ 2022-06-17 d42a488 sync: add more notes about Cond behavior
+ 2022-06-17 9e2f289 cmd/go/internal/work: log clearer detail for subprocess errors in (*Builder).toolID
+ 2022-06-17 dd2d00f net: fix flaky *TimeoutMustNotReturn tests
+ 2022-06-17 6c25ba6 go/token: delete unused File.set field
+ 2022-06-16 9068c68 cmd/dist: add package . to 'go test' commands
+ 2022-06-16 7bad615 runtime: write much more direct test for semaphore waiter scalability
+ 2022-06-16 f38a580 cmd/go: add more tracing

Change-Id: I912c5879165e03f4d7f8ac3ee9241d50fc92a419
@gopherbot
Copy link

Change https://go.dev/cl/414836 mentions this issue: cmd/compile: only check implicit dots for method call enabled by a type bound

@cuonglm
Copy link
Member

cuonglm commented Jun 28, 2022

@mdempsky @griesemer I sent CL 414836 to fix this issue, is it worth to make it fixed for go1.19 or just wait for unified IR in go1.20 like you said above?

@griesemer
Copy link
Contributor

@cuonglm The fix seems simple enough, but we're also close to RC1. I will ping the compiler/runtime team to chime in.

@randall77
Copy link
Contributor

@gopherbot please open a backport issue for 1.18.

@gopherbot
Copy link

Backport issue(s) opened: #53723 (for 1.18).

Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://go.dev/wiki/MinorReleases.

@gopherbot
Copy link

Change https://go.dev/cl/422274 mentions this issue: cmd/compile: fix ICE when checking implicit dot for method call

jproberts pushed a commit to jproberts/go that referenced this issue Aug 10, 2022
…pe bound

Fixes golang#53419

Change-Id: Ibad64f5c4af2112deeb0a9ecc9c589b17594bd05
Reviewed-on: https://go-review.googlesource.com/c/go/+/414836
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
gopherbot pushed a commit that referenced this issue Aug 15, 2022
CL 414836 limited the check for implicit dot for method call enabled by
a type bound. However, the checking condition for ODOTMETH only is not
right. For example, for promoted method, we have a OXDOT node instead,
and we still have to check for implicit dot in this case.

However, if the base type and embedded types have the same method name,
e.g in issue #53419, typecheck.AddImplicitDots will be confused and
result in an ambigus selector.

To fix this, we ensure methods for the base type are computed, then only
do the implicit dot check if we can find a matched method.

Fixes #54348

Change-Id: Iefe84ff330830afe35c5daffd499824db108da23
Reviewed-on: https://go-review.googlesource.com/c/go/+/422274
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
@golang golang locked and limited conversation to collaborators Aug 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge generics Issue is related to generics NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

8 participants