Skip to content

Commit

Permalink
internal/gcimporter: use Alias.Rhs, not unsafe hack
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
adonovan committed Apr 29, 2024
1 parent 0b45163 commit 77f691b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
7 changes: 3 additions & 4 deletions internal/aliases/aliases_go121.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (
// 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 }
Expand Down
11 changes: 11 additions & 0 deletions internal/aliases/aliases_go122.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import (
// 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) }

Expand Down
19 changes: 1 addition & 18 deletions internal/gcimporter/iexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"sort"
"strconv"
"strings"
"unsafe"

"golang.org/x/tools/go/types/objectpath"
"golang.org/x/tools/internal/aliases"
Expand Down Expand Up @@ -529,7 +528,7 @@ func (p *iexporter) doDecl(obj types.Object) {
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
Expand Down Expand Up @@ -1331,19 +1330,3 @@ func (e internalError) Error() string { return "gcimporter: " + string(e) }
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
}

0 comments on commit 77f691b

Please sign in to comment.