Skip to content

go.tools/go/types: inferred type for value,ok-mode expressions includes assignability conversions #7060

@adonovan

Description

@adonovan
Both of these functions contain expressions (m[1] and <-ch) that appear in a
"value,ok" context.

func f() (i interface{}, ok bool) {
       m := map[int]string{1: "hi"}
       i, ok = m[1]
       return
}

func g() (i interface{}, ok bool) {
       ch := make(chan string, 1)
       ch <- "hi"
       i, ok = <- ch
       return
}

I would expect the inferred types for both to be (string, bool), but in fact both are
(interface{}, bool).  i.e. the type checker has incorporated the type conversion to the
assignment's LHS into the type that is inferred.  This is not ideal because a compiler
(for example) would assume that no conversion is necessary since the LHS and RHS types
are identical.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions