internal/gcimporter: use Alias.Rhs, not unsafe hack
Unfortunately we need an aliases.Rhs shim to handle the
three cases (before, at, after go1.22).
(Alias.Rhs was recently added in CL CL 581615.)
Updates golang/go#66559
Change-Id: I25a35c14f3ef5ddb77712afcce17f960dd181b5c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/581635
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/internal/aliases/aliases_go121.go b/internal/aliases/aliases_go121.go
index f7e1732..c027b9f 100644
--- a/internal/aliases/aliases_go121.go
+++ b/internal/aliases/aliases_go121.go
@@ -15,11 +15,10 @@
// It will never be created by go/types.
type Alias struct{}
-func (*Alias) String() string { panic("unreachable") }
-
+func (*Alias) String() string { panic("unreachable") }
func (*Alias) Underlying() types.Type { panic("unreachable") }
-
-func (*Alias) Obj() *types.TypeName { panic("unreachable") }
+func (*Alias) Obj() *types.TypeName { panic("unreachable") }
+func Rhs(alias *Alias) types.Type { panic("unreachable") }
// Unalias returns the type t for go <=1.21.
func Unalias(t types.Type) types.Type { return t }
diff --git a/internal/aliases/aliases_go122.go b/internal/aliases/aliases_go122.go
index e767676..b329954 100644
--- a/internal/aliases/aliases_go122.go
+++ b/internal/aliases/aliases_go122.go
@@ -17,6 +17,17 @@
// Alias is an alias of types.Alias.
type Alias = types.Alias
+// Rhs returns the type on the right-hand side of the alias declaration.
+func Rhs(alias *Alias) types.Type {
+ if alias, ok := any(alias).(interface{ Rhs() types.Type }); ok {
+ return alias.Rhs() // go1.23+
+ }
+
+ // go1.22's Alias didn't have the Rhs method,
+ // so Unalias is the best we can do.
+ return Unalias(alias)
+}
+
// Unalias is a wrapper of types.Unalias.
func Unalias(t types.Type) types.Type { return types.Unalias(t) }
diff --git a/internal/gcimporter/iexport.go b/internal/gcimporter/iexport.go
index 683bd73..deeb67f 100644
--- a/internal/gcimporter/iexport.go
+++ b/internal/gcimporter/iexport.go
@@ -21,7 +21,6 @@
"sort"
"strconv"
"strings"
- "unsafe"
"golang.org/x/tools/go/types/objectpath"
"golang.org/x/tools/internal/aliases"
@@ -529,7 +528,7 @@
if alias, ok := t.(*aliases.Alias); ok {
// Preserve materialized aliases,
// even of non-exported types.
- t = aliasRHS(alias)
+ t = aliases.Rhs(alias)
}
w.typ(t, obj.Pkg())
break
@@ -1331,19 +1330,3 @@
func internalErrorf(format string, args ...interface{}) error {
return internalError(fmt.Sprintf(format, args...))
}
-
-// aliasRHS removes exactly one Alias constructor.
-func aliasRHS(alias *aliases.Alias) types.Type {
- // TODO(adonovan): if proposal #66559 is accepted, this will
- // become Alias.RHS(alias). In the meantime, we must punch
- // through the drywall.
- type go123Alias struct {
- _ *types.TypeName
- _ *types.TypeParamList
- RHS types.Type
- _ types.Type
- }
- var raw *go123Alias
- *(**aliases.Alias)(unsafe.Pointer(&raw)) = alias
- return raw.RHS
-}