Skip to content

Commit b31dda8

Browse files
committed
cmd/compile: handle any as alias like byte and rune
`types.Types[types.TINTER]` is already used for `interface{}`, so we can conveniently just extend the existing logic that substitutes `byte` and `rune` with `uint8` and `int32` to also substitute `any`. Fixes #49665. Change-Id: I1ab1954699934150aab899b35037d5611c8ca47e Reviewed-on: https://go-review.googlesource.com/c/go/+/365354 Trust: Matthew Dempsky <[email protected]> Trust: Dan Scales <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Dan Scales <[email protected]>
1 parent 5e774b0 commit b31dda8

File tree

7 files changed

+46
-12
lines changed

7 files changed

+46
-12
lines changed

src/cmd/compile/internal/reflectdata/reflect.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,12 @@ func hashMightPanic(t *types.Type) bool {
924924
}
925925
}
926926

927-
// formalType replaces byte and rune aliases with real types.
927+
// formalType replaces predeclared aliases with real types.
928928
// They've been separate internally to make error messages
929929
// better, but we have to merge them in the reflect tables.
930930
func formalType(t *types.Type) *types.Type {
931-
if t == types.ByteType || t == types.RuneType {
931+
switch t {
932+
case types.AnyType, types.ByteType, types.RuneType:
932933
return types.Types[t.Kind()]
933934
}
934935
return t

src/cmd/compile/internal/types/fmt.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
328328
return
329329
}
330330

331-
if t == ByteType || t == RuneType {
332-
// in %-T mode collapse rune and byte with their originals.
331+
if t == AnyType || t == ByteType || t == RuneType {
332+
// in %-T mode collapse predeclared aliases with their originals.
333333
switch mode {
334334
case fmtTypeIDName, fmtTypeID:
335335
t = Types[t.Kind()]

src/cmd/compile/internal/types/identity.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func identical(t1, t2 *Type, flags int, assumedEqual map[typePair]struct{}) bool
5858
return (t1 == Types[TUINT8] || t1 == ByteType) && (t2 == Types[TUINT8] || t2 == ByteType)
5959
case TINT32:
6060
return (t1 == Types[TINT32] || t1 == RuneType) && (t2 == Types[TINT32] || t2 == RuneType)
61+
case TINTER:
62+
return (t1 == Types[TINTER] || t1 == AnyType) && (t2 == Types[TINTER] || t2 == AnyType)
6163
default:
6264
return false
6365
}

src/cmd/compile/internal/types/type.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,23 @@ const (
106106
// It also stores pointers to several special types:
107107
// - Types[TANY] is the placeholder "any" type recognized by SubstArgTypes.
108108
// - Types[TBLANK] represents the blank variable's type.
109+
// - Types[TINTER] is the canonical "interface{}" type.
109110
// - Types[TNIL] represents the predeclared "nil" value's type.
110111
// - Types[TUNSAFEPTR] is package unsafe's Pointer type.
111112
var Types [NTYPE]*Type
112113

113114
var (
114-
// Predeclared alias types. Kept separate for better error messages.
115+
// Predeclared alias types. These are actually created as distinct
116+
// defined types for better error messages, but are then specially
117+
// treated as identical to their respective underlying types.
118+
AnyType *Type
115119
ByteType *Type
116120
RuneType *Type
117121

118122
// Predeclared error interface type.
119123
ErrorType *Type
120124
// Predeclared comparable interface type.
121125
ComparableType *Type
122-
// Predeclared any interface type.
123-
AnyType *Type
124126

125127
// Types to represent untyped string and boolean constants.
126128
UntypedString = newType(TSTRING)
@@ -1207,6 +1209,11 @@ func (t *Type) cmp(x *Type) Cmp {
12071209
if (t == Types[RuneType.kind] || t == RuneType) && (x == Types[RuneType.kind] || x == RuneType) {
12081210
return CMPeq
12091211
}
1212+
1213+
case TINTER:
1214+
if (t == Types[AnyType.kind] || t == AnyType) && (x == Types[AnyType.kind] || x == AnyType) {
1215+
return CMPeq
1216+
}
12101217
}
12111218
}
12121219

src/cmd/compile/internal/types/universe.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func InitTypes(defTypeName func(sym *Sym, typ *Type) Object) {
5959

6060
Types[TANY] = newType(TANY)
6161
Types[TINTER] = NewInterface(LocalPkg, nil, false)
62+
CheckSize(Types[TINTER])
6263

6364
defBasic := func(kind Kind, pkg *Pkg, name string) *Type {
6465
typ := newType(kind)
@@ -108,11 +109,14 @@ func InitTypes(defTypeName func(sym *Sym, typ *Type) Object) {
108109
ResumeCheckSize()
109110

110111
// any type (interface)
111-
if base.Flag.G > 0 {
112-
DeferCheckSize()
113-
AnyType = defBasic(TFORW, BuiltinPkg, "any")
114-
AnyType.SetUnderlying(NewInterface(NoPkg, []*Field{}, false))
115-
ResumeCheckSize()
112+
DeferCheckSize()
113+
AnyType = defBasic(TFORW, BuiltinPkg, "any")
114+
AnyType.SetUnderlying(NewInterface(NoPkg, []*Field{}, false))
115+
ResumeCheckSize()
116+
117+
if base.Flag.G == 0 {
118+
ComparableType.Sym().Def = nil
119+
AnyType.Sym().Def = nil
116120
}
117121

118122
Types[TUNSAFEPTR] = defBasic(TUNSAFEPTR, UnsafePkg, "Pointer")

test/fixedbugs/issue49665.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// run
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import "fmt"
10+
11+
var x any
12+
var y interface{}
13+
14+
var _ = &x == &y // assert x and y have identical types
15+
16+
func main() {
17+
fmt.Printf("%T\n%T\n", &x, &y)
18+
}

test/fixedbugs/issue49665.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*interface {}
2+
*interface {}

0 commit comments

Comments
 (0)