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: type inference fails when a prefix of type arguments is provided #44799

Closed
griesemer opened this issue Mar 5, 2021 · 5 comments
Closed
Assignees
Milestone

Comments

@griesemer
Copy link
Contributor

@griesemer griesemer commented Mar 5, 2021

Given program

package main

func Map[F, T any](s []F, f func(F) T) []T { return nil }

func Reduce[Elem1, Elem2 any](s []Elem1, initializer Elem2, f func(Elem2, Elem1) Elem2) Elem2 { var x Elem2; return x }

func main() {
	var s []int
	var f1 func(int) float64
	var f2 func(float64, int) float64
	//_ = Map[int](s, f1)       // <<< this should work, too
	_ = Map(s, f1)
	//_ = Reduce[int](s, 0, f2) // <<< this should work, too
	_ = Reduce(s, 0, f2)
}

Type inference fails for the commented out statements. It should work.

cc: @findleyr

@griesemer griesemer added this to the Go1.17 milestone Mar 5, 2021
@griesemer griesemer self-assigned this Mar 5, 2021
@findleyr
Copy link
Contributor

@findleyr findleyr commented Mar 5, 2021

The problem is that we don't continue with normal inference if constraint type inference fails. I suspect this bug that was introduced when we extracted logic out of Checker.call into Checker.funcInst -- now when checking a call we check the call.Fun independently.

I'll try to fix.

Loading

@findleyr findleyr self-assigned this Mar 5, 2021
@griesemer
Copy link
Contributor Author

@griesemer griesemer commented Mar 5, 2021

The order needs to be:

  • start with whatever type arguments are provided
  • use function type inference (using the actual function arguments) to infer more type arguments (if we have an actual call_
  • use constraint type inference if we still have missing type arguments

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 8, 2021

Change https://golang.org/cl/299829 mentions this issue: go/types: try function type inference before constraint type inference

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Apr 8, 2021

Change https://golang.org/cl/308372 mentions this issue: cmd/compile/internal/types2: use combined type and ordinary args for type inference

Loading

@gopherbot gopherbot closed this in 3f4977b Apr 10, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Apr 11, 2021

Change https://golang.org/cl/308973 mentions this issue: cmd/compile/internal/types: add example test for type inference

Loading

gopherbot pushed a commit that referenced this issue Apr 13, 2021
Follow-up on https://golang.org/cl/308372.

For #44799.

Change-Id: I27d149241d465ee31e9c21d5f0935f31efcb5b60
Reviewed-on: https://go-review.googlesource.com/c/go/+/308973
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants