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

cmd/compile: fail to infer type with only one valid solution #50212

Closed
Ludonope opened this issue Dec 16, 2021 · 2 comments
Closed

cmd/compile: fail to infer type with only one valid solution #50212

Ludonope opened this issue Dec 16, 2021 · 2 comments
Labels
NeedsInvestigation
Milestone

Comments

@Ludonope
Copy link

@Ludonope Ludonope commented Dec 16, 2021

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

$ go version
go version go1.18beta1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

I'm trying to make some sort of Nullable constraint which would indicate if that type can be set to nil or not.

Right now I'm only trying to detect pointers and slices.

One way to detect a pointer type (which works) is the following:

type Ptr[T any] interface {
	~*T
}

func SetNil[U any, T Ptr[U]](val T) {
	val = nil
}

func main() {
    a := 0
    SetNil(&a)
}

It can also be done with slice type:

type Slice[T any] interface {
	~[]T
}

func SetNil[U any, T Slice[U]](val T) {
	val = nil
}

func main() {
    SetNil([]int{})
}

However if I try to combine those into a Nullable constraint the type inference breaks for U.

https://go.dev/play/p/SbBJnOUeFVP?v=gotip

Providing the types U and T explicitly works tho.

What did you expect to see?

I'm expecting the compiler to properly infer Uand T types. As there are multiple options for T but only one of them is valid, it should be able to try to deduce U just like it does when using the Ptr[T] or Slice[T] constraint.

What did you see instead?

./prog.go:20:8: cannot infer U (prog.go:15:13)

Go build failed.

You should be able to reproduce this with the go.dev/play link provided above.

@cherrymui cherrymui changed the title affected/package: generics Fail to infer type with only one valid solution cmd/compile: fail to infer type with only one valid solution Dec 16, 2021
@cherrymui cherrymui added the NeedsInvestigation label Dec 16, 2021
@cherrymui cherrymui added this to the Backlog milestone Dec 16, 2021
@cherrymui
Copy link
Member

@cherrymui cherrymui commented Dec 16, 2021

cc @griesemer

@griesemer
Copy link
Contributor

@griesemer griesemer commented Dec 16, 2021

Constraint type inference requires that there's a structural type present in the constraint (the inference part is not yet in the spec, but the generics proposal describes a much). Here we have two types in the constraint and they are different, so there's no structural type.

We're not going to change that for 1.18. Feel free to send out a detailed proposal. Thanks.

Closing as working as intended.

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

3 participants