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

Closed
zllangct opened this issue May 29, 2021 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@zllangct
Copy link

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

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 the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 29, 2021
@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

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

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

@findleyr findleyr self-assigned this Jun 22, 2021
@griesemer
Copy link
Contributor

The compiler now accepts both the original and simplified code sample; so this seems to be working now.

@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants