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: better error message for failing inference for arguments using type parameters #49800

Closed
mpx opened this issue Nov 26, 2021 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mpx
Copy link
Contributor

mpx commented Nov 26, 2021

Using compiler from tip:

go version devel +45bae64015 Thu Nov 25 04:02:39 2021 +0000 linux/amd64

Calling a generic function from another using the same type parameters causes inference to fail. The compiler points to the function being called, not the location of the call:

package main

import (
    "fmt"
    "sort"
)

func keys[V any, M ~map[string]V](m M) []string {
    out := make([]string, 0, len(m))
    for k := range m {
        out = append(out, k)
    }
    return out
}

func sortKeys[V any, M ~map[string]V](m M) []string {
    // Fails:
    ks := keys(m)
    // Works:
    //ks := keys[V, M](m)
    sort.Strings(ks)
    return ks
}

func main() {
    m := map[string]int{"foo": 1, "bar": 2}
    fmt.Println(keys(m))
}

Returns:

./keys.go:8:18: M does not match map[string]V

Ideally type checking should be able to infer the type parameters since they are identical, but I'm not sure if this is intended for Go 1.18.

Alternatively, there should to be a clearer error message pointing to the call location, not the callee.

I couldn't see a similar open bug, but this is complicated enough (for me) I might have missed it.

Cc @griesemer @findleyr

@hanchaoqun
Copy link
Contributor

hanchaoqun commented Nov 26, 2021

Seems duplicate with #49575, and the fix CL is https://go-review.googlesource.com/c/go/+/362776

@mpx
Copy link
Contributor Author

mpx commented Nov 26, 2021

Thanks, yes, appears to be a duplicate of #49575, but changing the inference rules is pushed to Go 1.19.

I think the error message should be fixed in Go 1.18 - it needs to point to the call site and should have a clearer description.

@griesemer griesemer self-assigned this Nov 26, 2021
@griesemer griesemer added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 26, 2021
@griesemer griesemer added this to the Go1.18 milestone Nov 26, 2021
@griesemer griesemer changed the title go/types, types2: inference fails for arguments using type parameters go/types, types2: better error message for failing inference for arguments using type parameters Nov 26, 2021
@hanchaoqun
Copy link
Contributor

Thanks, yes, appears to be a duplicate of #49575, but changing the inference rules is pushed to Go 1.19.

I think the error message should be fixed in Go 1.18 - it needs to point to the call site and should have a clearer description.

@mpx Maybe this CL https://golang.org/cl/362634 can fix the error report on the wrong line.

@gopherbot
Copy link

Change https://golang.org/cl/362634 mentions this issue: cmd/compile: fix type error reported on the wrong line

@golang golang locked and limited conversation to collaborators Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants