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: gotip cannot infer generic types when functions have no generic parameter #49297

Closed
sina-devel opened this issue Nov 2, 2021 · 4 comments
Labels
generics TypeInference

Comments

@sina-devel
Copy link

@sina-devel sina-devel commented Nov 2, 2021

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

$ gotip version
go version devel go1.18-62b29b0 Tue Nov 2 17:01:14 2021 +0000 linux/amd64

What did you do?

package main

func zero[T any]() T {
	var o T
	return o
}

func getInt(a int) {}

func zeroInt() int {
	return zero()
}

func main() {
	zeroInt()
	getInt(zero())
}

What did you expect to see?

Successful compilation

What did you see instead?

main.go:11:13: cannot infer T (main.go:3:11)
main.go:17:13: cannot infer T (main.go:3:11)
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 2, 2021

This is working as intended. Inference is based only on function arguments and constraints. See the description at https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#type-inference.

@sina-devel
Copy link
Author

@sina-devel sina-devel commented Nov 2, 2021

@ianlancetaylor Ok, and that means it is not possible to add it in future?

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 2, 2021

It is possible to add it in the future. But it would need a clear and precise design for how it works. It should be written as a language change proposal; see https://golang.org/s/proposal. It's not obvious to me how it should work; exactly what types should be inferred if I write zero() + zero() != zero(), and why?

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Apr 23, 2022

If I understand this correctly, it's basically the same as #50285, which is still open.

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

No branches or pull requests

2 participants