Skip to content

Commit

Permalink
[dev.typeparams] go/types: make predeclared "any" alias for interface{}
Browse files Browse the repository at this point in the history
This is a direct port of CL 285132 to go/types.

Change-Id: I35486d8ea1fa6c0c6a32ece199a6ccfd55d44d29
Reviewed-on: https://go-review.googlesource.com/c/go/+/291322
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
findleyr committed Feb 12, 2021
1 parent 58758e0 commit 0f43973
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/go/types/api_test.go
Expand Up @@ -325,14 +325,14 @@ func TestTypesInfo(t *testing.T) {
{broken + `x5; func _() { var x map[string][...]int; x = map[string][...]int{"": {1,2,3}} }`, `x`, `map[string][-1]int`},

// parameterized functions
{genericPkg + `p0; func f[T any](T); var _ = f(int)`, `f`, `func[T₁ any](T₁)`},
{genericPkg + `p0; func f[T any](T); var _ = f(int)`, `f`, `func[T₁ interface{}](T₁)`},
{genericPkg + `p1; func f[T any](T); var _ = f(int)`, `f(int)`, `func(int)`},
{genericPkg + `p2; func f[T any](T); func _() { f(42) }`, `f`, `func[T₁ any](T₁)`},
{genericPkg + `p2; func f[T any](T); func _() { f(42) }`, `f`, `func[T₁ interface{}](T₁)`},
{genericPkg + `p3; func f[T any](T); func _() { f(42) }`, `f(42)`, `()`},

// type parameters
{genericPkg + `t0; type t[] int; var _ t`, `t`, `generic_t0.t`}, // t[] is a syntax error that is ignored in this test in favor of t
{genericPkg + `t1; type t[P any] int; var _ t[int]`, `t`, `generic_t1.t[P₁ any]`},
{genericPkg + `t1; type t[P any] int; var _ t[int]`, `t`, `generic_t1.t[P₁ interface{}]`},
{genericPkg + `t2; type t[P interface{}] int; var _ t[int]`, `t`, `generic_t2.t[P₁ interface{}]`},
{genericPkg + `t3; type t[P, Q interface{}] int; var _ t[int, int]`, `t`, `generic_t3.t[P₁, Q₂ interface{}]`},

Expand Down
9 changes: 3 additions & 6 deletions src/go/types/universe.go
Expand Up @@ -24,7 +24,7 @@ var (
universeIota *Const
universeByte *Basic // uint8 alias, but has name "byte"
universeRune *Basic // int32 alias, but has name "rune"
universeAny *Named
universeAny *Interface
universeError *Named
)

Expand Down Expand Up @@ -82,10 +82,7 @@ func defPredeclaredTypes() {
// (Predeclared and entered into universe scope so we do all the
// usual checks; but removed again from scope later since it's
// only visible as constraint in a type parameter list.)
{
typ := &Named{underlying: &emptyInterface}
def(NewTypeName(token.NoPos, nil, "any", typ))
}
def(NewTypeName(token.NoPos, nil, "any", &emptyInterface))

// Error has a nil package in its qualified name since it is in no package
{
Expand Down Expand Up @@ -241,7 +238,7 @@ func init() {
universeIota = Universe.Lookup("iota").(*Const)
universeByte = Universe.Lookup("byte").(*TypeName).typ.(*Basic)
universeRune = Universe.Lookup("rune").(*TypeName).typ.(*Basic)
universeAny = Universe.Lookup("any").(*TypeName).typ.(*Named)
universeAny = Universe.Lookup("any").(*TypeName).typ.(*Interface)
universeError = Universe.Lookup("error").(*TypeName).typ.(*Named)

// "any" is only visible as constraint in a type parameter list
Expand Down

0 comments on commit 0f43973

Please sign in to comment.