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: dev.typeparams branch: cannot use T₈ value as type IBase in assignment: T₈ does not implement IBase (missing GetName method) #46442

Open
zllangct opened this issue May 29, 2021 · 3 comments
Assignees
Milestone

Comments

@zllangct
Copy link

@zllangct zllangct commented May 29, 2021

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

$ go version
go version devel go1.17-22f5ece3b13 Thu May 27 23:54:29 2021 +0000 windows/amd64

What did you do?

type IBase interface {
	GetName() string
}

type Item struct {
	Name  string
}

func (p Item) GetName() string {
	return p.Name
}

var funcMap = map[reflect.Type]func(interface{})IBase{}

func AddCallback[T IBase](in T) {
	funcMap[reflect.TypeOf(in)] = func(src interface{}) IBase {
		return src.(T)
	}
}

func main() {
	item := Item{
		Name : "hello",
	}

	AddCallback[Item](item)
}

What did you expect to see?

What did you see instead?

# go_generic_test
.\main.go:119:3: cannot use T₈ value as type IBase in assignment:
	T₈ does not implement IBase (missing GetName method)
@zllangct zllangct changed the title dev.typeparams dev.typeparams cannot use T₈ value as type IBase in assignment: T₈ does not implement IBase (missing GetName method) May 29, 2021
@ianlancetaylor ianlancetaylor changed the title dev.typeparams cannot use T₈ value as type IBase in assignment: T₈ does not implement IBase (missing GetName method) go/types: dev.typeparams branch: cannot use T₈ value as type IBase in assignment: T₈ does not implement IBase (missing GetName method) May 29, 2021
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 29, 2021

CC @griesemer @findleyr

Note that the dev.typeparams branch is under very active development. It is not expected to be stable or to work at all. It's not particularly helpful to file bugs against it. Thanks.

That said I'll keep this one open since you've already filed it.

@ianlancetaylor ianlancetaylor added this to the Unreleased milestone May 29, 2021
@griesemer griesemer self-assigned this Jun 3, 2021
@griesemer griesemer modified the milestones: Unreleased, Go1.18 Jun 3, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Jun 3, 2021

This is a good bug report, thanks.

Here's a simplified version:

package p

type C interface {
	m()
}

func _[T C]() {
	_ = func(x interface{}) C {
		return x.(T)
	}
}

reporting:

$ go tool compile -G=3 bug.go2
bug.go2:9:3: cannot use T₁ value as type C in assignment:
	T₁ does not implement C (missing m method)

But the same code does work on the go2go playground, as expected. So this is a a regression of sorts.

@griesemer
Copy link
Contributor

@griesemer griesemer commented Jun 3, 2021

As an aside, the error message also shouldn't print the (internally used) subscripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants