From 4ed358b57efdad9ed710be7f4fc51495a7620ce2 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Fri, 19 Apr 2024 14:51:18 +0000 Subject: go/types, types: represent any using Alias When GODEBUG=gotypesalias=1 is set, use an actual Alias type to represent any, rather than a legacy alias representation. This makes any consistent with other interface aliases, and will eventually make obsolete the various workarounds for formatting any as 'any' rather than 'interface{}'. Since any is a global in the Universe scope, we must hijack Scope.Lookup to select the correct representation. Of course, this also means that we can't support type checking concurrently while mutating gotypesalias (or, in the case of types2, Config.EnableAlias). Some care is taken to ensure that the type checker panics in the event of this type of misuse. For now, we must still support the legacy representation of any, and the existing workarounds that look for a distinguished any pointer. This is done by ensuring that both representations have the same underlying pointer, and by updating workarounds to consider Underlying. Fixes golang/go#66921 Change-Id: I81db7e8e15317b7a6ed3b406545db15a2fc42f57 Reviewed-on: https://go-review.googlesource.com/c/go/+/580355 Reviewed-by: Alan Donovan LUCI-TryBot-Result: Go LUCI --- src/cmd/compile/internal/noder/writer.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/cmd/compile/internal/noder') diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go index 785176b3b5..453b08dbf9 100644 --- a/src/cmd/compile/internal/noder/writer.go +++ b/src/cmd/compile/internal/noder/writer.go @@ -569,7 +569,10 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo { case *types2.Interface: // Handle "any" as reference to its TypeName. - if typ == anyTypeName.Type() { + // The underlying "any" interface is canonical, so this logic handles both + // GODEBUG=gotypesalias=1 (when any is represented as a types2.Alias), and + // gotypesalias=0. + if types2.Unalias(typ) == types2.Unalias(anyTypeName.Type()) { w.Code(pkgbits.TypeNamed) w.obj(anyTypeName, nil) break -- cgit v1.3-5-g9baa