Skip to content

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

@phemmer

Description

@phemmer

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.

Metadata

Metadata

Labels

FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.genericsIssue is related to generics

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions