mirror of
https://github.com/golang/go
synced 2024-11-11 19:51:37 -07:00
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 <mdempsky@google.com> Trust: Dan Scales <danscales@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dan Scales <danscales@google.com>
This commit is contained in:
parent
5e774b0f5c
commit
b31dda8a2a
@ -924,11 +924,12 @@ func hashMightPanic(t *types.Type) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// formalType replaces byte and rune aliases with real types.
|
||||
// formalType replaces predeclared aliases with real types.
|
||||
// They've been separate internally to make error messages
|
||||
// better, but we have to merge them in the reflect tables.
|
||||
func formalType(t *types.Type) *types.Type {
|
||||
if t == types.ByteType || t == types.RuneType {
|
||||
switch t {
|
||||
case types.AnyType, types.ByteType, types.RuneType:
|
||||
return types.Types[t.Kind()]
|
||||
}
|
||||
return t
|
||||
|
@ -328,8 +328,8 @@ func tconv2(b *bytes.Buffer, t *Type, verb rune, mode fmtMode, visited map[*Type
|
||||
return
|
||||
}
|
||||
|
||||
if t == ByteType || t == RuneType {
|
||||
// in %-T mode collapse rune and byte with their originals.
|
||||
if t == AnyType || t == ByteType || t == RuneType {
|
||||
// in %-T mode collapse predeclared aliases with their originals.
|
||||
switch mode {
|
||||
case fmtTypeIDName, fmtTypeID:
|
||||
t = Types[t.Kind()]
|
||||
|
@ -58,6 +58,8 @@ func identical(t1, t2 *Type, flags int, assumedEqual map[typePair]struct{}) bool
|
||||
return (t1 == Types[TUINT8] || t1 == ByteType) && (t2 == Types[TUINT8] || t2 == ByteType)
|
||||
case TINT32:
|
||||
return (t1 == Types[TINT32] || t1 == RuneType) && (t2 == Types[TINT32] || t2 == RuneType)
|
||||
case TINTER:
|
||||
return (t1 == Types[TINTER] || t1 == AnyType) && (t2 == Types[TINTER] || t2 == AnyType)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
@ -106,12 +106,16 @@ const (
|
||||
// It also stores pointers to several special types:
|
||||
// - Types[TANY] is the placeholder "any" type recognized by SubstArgTypes.
|
||||
// - Types[TBLANK] represents the blank variable's type.
|
||||
// - Types[TINTER] is the canonical "interface{}" type.
|
||||
// - Types[TNIL] represents the predeclared "nil" value's type.
|
||||
// - Types[TUNSAFEPTR] is package unsafe's Pointer type.
|
||||
var Types [NTYPE]*Type
|
||||
|
||||
var (
|
||||
// Predeclared alias types. Kept separate for better error messages.
|
||||
// Predeclared alias types. These are actually created as distinct
|
||||
// defined types for better error messages, but are then specially
|
||||
// treated as identical to their respective underlying types.
|
||||
AnyType *Type
|
||||
ByteType *Type
|
||||
RuneType *Type
|
||||
|
||||
@ -119,8 +123,6 @@ var (
|
||||
ErrorType *Type
|
||||
// Predeclared comparable interface type.
|
||||
ComparableType *Type
|
||||
// Predeclared any interface type.
|
||||
AnyType *Type
|
||||
|
||||
// Types to represent untyped string and boolean constants.
|
||||
UntypedString = newType(TSTRING)
|
||||
@ -1207,6 +1209,11 @@ func (t *Type) cmp(x *Type) Cmp {
|
||||
if (t == Types[RuneType.kind] || t == RuneType) && (x == Types[RuneType.kind] || x == RuneType) {
|
||||
return CMPeq
|
||||
}
|
||||
|
||||
case TINTER:
|
||||
if (t == Types[AnyType.kind] || t == AnyType) && (x == Types[AnyType.kind] || x == AnyType) {
|
||||
return CMPeq
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,6 +59,7 @@ func InitTypes(defTypeName func(sym *Sym, typ *Type) Object) {
|
||||
|
||||
Types[TANY] = newType(TANY)
|
||||
Types[TINTER] = NewInterface(LocalPkg, nil, false)
|
||||
CheckSize(Types[TINTER])
|
||||
|
||||
defBasic := func(kind Kind, pkg *Pkg, name string) *Type {
|
||||
typ := newType(kind)
|
||||
@ -108,11 +109,14 @@ func InitTypes(defTypeName func(sym *Sym, typ *Type) Object) {
|
||||
ResumeCheckSize()
|
||||
|
||||
// any type (interface)
|
||||
if base.Flag.G > 0 {
|
||||
DeferCheckSize()
|
||||
AnyType = defBasic(TFORW, BuiltinPkg, "any")
|
||||
AnyType.SetUnderlying(NewInterface(NoPkg, []*Field{}, false))
|
||||
ResumeCheckSize()
|
||||
|
||||
if base.Flag.G == 0 {
|
||||
ComparableType.Sym().Def = nil
|
||||
AnyType.Sym().Def = nil
|
||||
}
|
||||
|
||||
Types[TUNSAFEPTR] = defBasic(TUNSAFEPTR, UnsafePkg, "Pointer")
|
||||
|
18
test/fixedbugs/issue49665.go
Normal file
18
test/fixedbugs/issue49665.go
Normal file
@ -0,0 +1,18 @@
|
||||
// run
|
||||
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
var x any
|
||||
var y interface{}
|
||||
|
||||
var _ = &x == &y // assert x and y have identical types
|
||||
|
||||
func main() {
|
||||
fmt.Printf("%T\n%T\n", &x, &y)
|
||||
}
|
2
test/fixedbugs/issue49665.out
Normal file
2
test/fixedbugs/issue49665.out
Normal file
@ -0,0 +1,2 @@
|
||||
*interface {}
|
||||
*interface {}
|
Loading…
Reference in New Issue
Block a user