Skip to content

Commit 77f691b

Browse files
committed
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 <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 0b45163 commit 77f691b

File tree

3 files changed

+15
-22
lines changed

3 files changed

+15
-22
lines changed

internal/aliases/aliases_go121.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ import (
1515
// It will never be created by go/types.
1616
type Alias struct{}
1717

18-
func (*Alias) String() string { panic("unreachable") }
19-
18+
func (*Alias) String() string { panic("unreachable") }
2019
func (*Alias) Underlying() types.Type { panic("unreachable") }
21-
22-
func (*Alias) Obj() *types.TypeName { panic("unreachable") }
20+
func (*Alias) Obj() *types.TypeName { panic("unreachable") }
21+
func Rhs(alias *Alias) types.Type { panic("unreachable") }
2322

2423
// Unalias returns the type t for go <=1.21.
2524
func Unalias(t types.Type) types.Type { return t }

internal/aliases/aliases_go122.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ import (
1717
// Alias is an alias of types.Alias.
1818
type Alias = types.Alias
1919

20+
// Rhs returns the type on the right-hand side of the alias declaration.
21+
func Rhs(alias *Alias) types.Type {
22+
if alias, ok := any(alias).(interface{ Rhs() types.Type }); ok {
23+
return alias.Rhs() // go1.23+
24+
}
25+
26+
// go1.22's Alias didn't have the Rhs method,
27+
// so Unalias is the best we can do.
28+
return Unalias(alias)
29+
}
30+
2031
// Unalias is a wrapper of types.Unalias.
2132
func Unalias(t types.Type) types.Type { return types.Unalias(t) }
2233

internal/gcimporter/iexport.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"sort"
2222
"strconv"
2323
"strings"
24-
"unsafe"
2524

2625
"golang.org/x/tools/go/types/objectpath"
2726
"golang.org/x/tools/internal/aliases"
@@ -529,7 +528,7 @@ func (p *iexporter) doDecl(obj types.Object) {
529528
if alias, ok := t.(*aliases.Alias); ok {
530529
// Preserve materialized aliases,
531530
// even of non-exported types.
532-
t = aliasRHS(alias)
531+
t = aliases.Rhs(alias)
533532
}
534533
w.typ(t, obj.Pkg())
535534
break
@@ -1331,19 +1330,3 @@ func (e internalError) Error() string { return "gcimporter: " + string(e) }
13311330
func internalErrorf(format string, args ...interface{}) error {
13321331
return internalError(fmt.Sprintf(format, args...))
13331332
}
1334-
1335-
// aliasRHS removes exactly one Alias constructor.
1336-
func aliasRHS(alias *aliases.Alias) types.Type {
1337-
// TODO(adonovan): if proposal #66559 is accepted, this will
1338-
// become Alias.RHS(alias). In the meantime, we must punch
1339-
// through the drywall.
1340-
type go123Alias struct {
1341-
_ *types.TypeName
1342-
_ *types.TypeParamList
1343-
RHS types.Type
1344-
_ types.Type
1345-
}
1346-
var raw *go123Alias
1347-
*(**aliases.Alias)(unsafe.Pointer(&raw)) = alias
1348-
return raw.RHS
1349-
}

0 commit comments

Comments
 (0)