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: recursive call of generic function with concrete type argument fails #48638

Closed
griesemer opened this issue Sep 27, 2021 · 3 comments
Assignees
Labels
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 27, 2021

package p
func g[P any](x P) {
        g[int](x)
}

does not type-check:

cannot use x (variable of type parameter P constrained by interface{}) as int value in argument to g[int]

[edited]: This looks like a correct error message after all: If g is called as g[string]("foo"), the type of x would be string and the local call g[int](x) would be incorrect.

@griesemer griesemer added this to the Go1.18 milestone Sep 27, 2021
@griesemer griesemer self-assigned this Sep 27, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Sep 27, 2021

Change https://golang.org/cl/352311 mentions this issue: cmd/compile/internal/types2: use correct argument types for recursive generic functions

Loading

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Sep 27, 2021

package p
func g[P any](x P) {
        g[int](x)
}

Is this code permitted at all? It is a recursive reference from g to itself with a different set of type arguments.

Loading

@griesemer
Copy link
Contributor Author

@griesemer griesemer commented Sep 27, 2021

Agreed that this code should not be permitted because the argument x in the local call of g is of type P (which we don't know anything about) but that call requires the argument to be of type int (per the instantiation of g). In the first recursive call, x will be of type int but that's "too late".

In short, type-checking this function should fail, and thus this is not an issue.

Loading

@griesemer griesemer closed this Sep 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants