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: make constraint type inference more flexible for some channel types (?) #45920

Closed
griesemer opened this issue May 3, 2021 · 6 comments
Labels
NeedsInvestigation
Milestone

Comments

@griesemer
Copy link
Contributor

griesemer commented May 3, 2021

Constraint type inference works as expected for

package p

func Recv[T any, C interface{ type chan T }](ch C)

func _(ch chan int) {
	Recv(ch) // T is inferred as int
}

but fails for

package p

func Recv[T any, C interface{ type chan T, <-chan T }](ch C)

func _(ch chan int) {
	Recv(ch) // inference fails
}

per the current proposal specification (constraint type inference expects exactly one type in the type list/set of the constraint for which inference is used).

This seems like it might be a common case; perhaps we need to special case channels that are the same but for their directions.

Reported by @rogpeppe in #43671.

@griesemer griesemer added the NeedsDecision label May 3, 2021
@griesemer griesemer added this to the Go1.18 milestone May 3, 2021
@griesemer griesemer self-assigned this May 3, 2021
@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented May 3, 2021

I think that what is most important for now is that the inference rules be as simple as possible. Constraint type inference is already quite complicated. I don't think that we should make it more complicated. There will always be cases where a person can easily see what the answer is even if the inference algorithm can't.

@rogpeppe
Copy link
Contributor

rogpeppe commented May 6, 2021

FWIW here's another case that came up while discussing possible future directions for encoding/json: https://go2goplay.golang.org/p/27Vkw9dnhNZ

It would be kinda nice to have constraint type inference work there.

@griesemer
Copy link
Contributor Author

griesemer commented May 6, 2021

The good thing about type inference is that we can continue to fine-tune it (and make it more powerful) without affecting existing code (i.e. w/o affecting backward-compatibility).

@griesemer
Copy link
Contributor Author

griesemer commented Oct 29, 2021

For this to work we need to relax the structural type of constraints to ignore channel directions. This may have some unexpected consequences. Will experiment. If the relaxation is all that's needed, we can address this. Otherwise we will stick to the existing rules.

@griesemer
Copy link
Contributor Author

griesemer commented Nov 1, 2021

Removing release-blocker again. We will be ok with or without this.

@griesemer griesemer added NeedsInvestigation and removed release-blocker labels Nov 1, 2021
@gopherbot gopherbot removed the NeedsDecision label Nov 1, 2021
@gopherbot
Copy link

gopherbot commented Nov 10, 2021

Change https://golang.org/cl/363075 mentions this issue: cmd/compile/internal/types2: slightly relax notion of structural type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation
Projects
None yet
Development

No branches or pull requests

4 participants