Skip to content

Commit

Permalink
go/internal/gcimporter: indexed format imports for type parameters al…
Browse files Browse the repository at this point in the history
…iases

Add support for importing a new 'B' tag for type parameters aliases
in the indexed data format.

Updates #68778

Change-Id: I3bd82870d4c4619a3771de30baf6d54f6ee5959e
Reviewed-on: https://go-review.googlesource.com/c/go/+/604635
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
  • Loading branch information
timothy-king committed Aug 16, 2024
1 parent 0320616 commit 660e7d6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
12 changes: 8 additions & 4 deletions src/cmd/compile/internal/importer/iimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@ func (r *importReader) obj(name string) {
pos := r.pos()

switch tag {
case 'A':
typ := r.typ()

r.declare(types2.NewTypeName(pos, r.currPkg, name, typ))
case 'A', 'B':
var tparams []*types2.TypeParam
if tag == 'B' {
tparams = r.tparamList()
}
rhs := r.typ()
const enabled = true // This is now always enabled within the compiler.
r.declare(newAliasTypeName(enabled, pos, r.currPkg, name, rhs, tparams))

case 'C':
typ, val := r.value()
Expand Down
9 changes: 6 additions & 3 deletions src/cmd/compile/internal/importer/ureader.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,9 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types2.Package, string) {

case pkgbits.ObjAlias:
pos := r.pos()
var tparams []*types2.TypeParam // TODO(#68778): Read tparams for unified IR.
typ := r.typ()
return newAliasTypeName(pr.enableAlias, pos, objPkg, objName, typ)
return newAliasTypeName(pr.enableAlias, pos, objPkg, objName, typ, tparams)

case pkgbits.ObjConst:
pos := r.pos()
Expand Down Expand Up @@ -537,13 +538,15 @@ func (r *reader) ident(marker pkgbits.SyncMarker) (*types2.Package, string) {
}

// newAliasTypeName returns a new TypeName, with a materialized *types2.Alias if supported.
func newAliasTypeName(aliases bool, pos syntax.Pos, pkg *types2.Package, name string, rhs types2.Type) *types2.TypeName {
func newAliasTypeName(aliases bool, pos syntax.Pos, pkg *types2.Package, name string, rhs types2.Type, tparams []*types2.TypeParam) *types2.TypeName {
// Copied from x/tools/internal/aliases.NewAlias via
// GOROOT/src/go/internal/gcimporter/ureader.go.
if aliases {
tname := types2.NewTypeName(pos, pkg, name, nil)
_ = types2.NewAlias(tname, rhs) // form TypeName -> Alias cycle
a := types2.NewAlias(tname, rhs) // form TypeName -> Alias cycle
a.SetTypeParams(tparams)
return tname
}
assert(len(tparams) == 0)
return types2.NewTypeName(pos, pkg, name, rhs)
}
3 changes: 2 additions & 1 deletion src/cmd/compile/internal/typecheck/iexport.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@
// }
//
// type Alias struct {
// Tag byte // 'A'
// Tag byte // 'A' or 'B'
// Pos Pos
// TypeParams []typeOff // only present if Tag == 'B'
// Type typeOff
// }
//
Expand Down
11 changes: 7 additions & 4 deletions src/go/internal/gcimporter/iimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,13 @@ func (r *importReader) obj(name string) {
pos := r.pos()

switch tag {
case 'A':
typ := r.typ()

r.declare(types.NewTypeName(pos, r.currPkg, name, typ))
case 'A', 'B':
var tparams []*types.TypeParam
if tag == 'B' {
tparams = r.tparamList()
}
rhs := r.typ()
r.declare(newAliasTypeName(pos, r.currPkg, name, rhs, tparams))

case 'C':
typ, val := r.value()
Expand Down
9 changes: 6 additions & 3 deletions src/go/internal/gcimporter/ureader.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,9 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {

case pkgbits.ObjAlias:
pos := r.pos()
var tparams []*types.TypeParam // TODO(#68778): Read tparams for unified IR.
typ := r.typ()
declare(newAliasTypeName(pos, objPkg, objName, typ))
declare(newAliasTypeName(pos, objPkg, objName, typ, tparams))

case pkgbits.ObjConst:
pos := r.pos()
Expand Down Expand Up @@ -661,14 +662,16 @@ func pkgScope(pkg *types.Package) *types.Scope {
}

// newAliasTypeName returns a new TypeName, with a materialized *types.Alias if supported.
func newAliasTypeName(pos token.Pos, pkg *types.Package, name string, rhs types.Type) *types.TypeName {
func newAliasTypeName(pos token.Pos, pkg *types.Package, name string, rhs types.Type, tparams []*types.TypeParam) *types.TypeName {
// When GODEBUG=gotypesalias=1 or unset, the Type() of the return value is a
// *types.Alias. Copied from x/tools/internal/aliases.NewAlias.
switch godebug.New("gotypesalias").Value() {
case "", "1":
tname := types.NewTypeName(pos, pkg, name, nil)
_ = types.NewAlias(tname, rhs) // form TypeName -> Alias cycle
a := types.NewAlias(tname, rhs) // form TypeName -> Alias cycle
a.SetTypeParams(tparams)
return tname
}
assert(len(tparams) == 0)
return types.NewTypeName(pos, pkg, name, rhs)
}

0 comments on commit 660e7d6

Please sign in to comment.