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: conversions involving type parameters don't work #47150

Closed
griesemer opened this issue Jul 12, 2021 · 9 comments
Closed

go/types, types2: conversions involving type parameters don't work #47150

griesemer opened this issue Jul 12, 2021 · 9 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@griesemer
Copy link
Contributor

Reminder issue that conversions involving type parameters are implemented ad-hoc and don't work as expected.

For instance (https://go2goplay.golang.org/p/8MHhIOoWA29):

package p

type Int interface{ type int }
type String interface{ type string }
type IntOrString interface {
	type int, string
}

func Convert1[Dst Int, Src Int](x Src) Dst            { return Dst(x) }
func Convert2[Dst String, Src Int](x Src) Dst         { return Dst(x) }
func Convert3[Dst String, Src String](x Src) Dst      { return Dst(x) }
func Convert4[Dst IntOrString, Src Int](x Src) Dst    { return Dst(x) }
func Convert5[Dst String, Src IntOrString](x Src) Dst { return Dst(x) }

(test case by @mdempsky)

See also the section on type conversions in the generics proposal.

cc: @findleyr

@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. release-blocker labels Jul 12, 2021
@griesemer griesemer added this to the Go1.18 milestone Jul 12, 2021
@griesemer griesemer self-assigned this Jul 12, 2021
@toothrot
Copy link
Contributor

Checking in on this issue as it's labeled a release blocker for Go 1.18. Is there any update?

@griesemer
Copy link
Contributor Author

This will be worked on soon.

@griesemer
Copy link
Contributor Author

Updated test cases.

package p

func convert1[Dst ~int, Src ~int](x Src) Dst            { return Dst(x) }
func convert2[Dst ~string, Src ~int](x Src) Dst         { return Dst(x) }
func convert3[Dst ~string, Src ~string](x Src) Dst      { return Dst(x) }
func convert4[Dst ~int|~string, Src ~int](x Src) Dst    { return Dst(x) }
func convert5[Dst ~string, Src ~int|~string](x Src) Dst { return Dst(x) }

@griesemer
Copy link
Contributor Author

@ianlancetaylor I am certainly expecting convert1 and convert3 to work, but I'm not sure that we need to support string(int) conversions that are generic, specifically in light of #3939. Comments?

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Oct 13, 2021

Seems fine to me. But presumably these will work:

func convert6[Dst ~string, Src ~byte](x Src) Dst         { return Dst(x) }
func convert7[Dst ~string, Src ~rune](x Src) Dst         { return Dst(x) }

@griesemer
Copy link
Contributor Author

griesemer commented Oct 13, 2021

Yes, those will work. Should have a CL with all expected conversions soon.

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/356010 mentions this issue: cmd/compile/internal/types2: implement generic conversions

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/357333 mentions this issue: cmd/compile/internal/types2: use correct types when checking generic conversions

gopherbot pushed a commit that referenced this issue Oct 20, 2021
…conversions

Iterate through the actual, possibly defined types of constraints
when type-checking generic conversions, not the underlying types.

For #47150.

Change-Id: Ia7af313bf46d6f6b0ad5292ff793b030b8e2d3d8
Reviewed-on: https://go-review.googlesource.com/c/go/+/357333
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/357249 mentions this issue: cmd/compile/internal/types: more detailed error messages for generic conversions

gopherbot pushed a commit that referenced this issue Oct 22, 2021
… conversions

- slightly refactor convertibleTo and convertibleToImpl
- provide ability to return a conversion failure cause
- add detailed cause for generic conversions

For #47150.

Change-Id: Ie97d89be0234414ef4df22a6920e18acc944a102
Reviewed-on: https://go-review.googlesource.com/c/go/+/357249
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
@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 NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

4 participants