Skip to content

Commit 660e7d6

Browse files
committed
go/internal/gcimporter: indexed format imports for type parameters aliases
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 0320616 commit 660e7d6

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

src/cmd/compile/internal/importer/iimport.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,14 @@ func (r *importReader) obj(name string) {
321321
pos := r.pos()
322322

323323
switch tag {
324-
case 'A':
325-
typ := r.typ()
326-
327-
r.declare(types2.NewTypeName(pos, r.currPkg, name, typ))
324+
case 'A', 'B':
325+
var tparams []*types2.TypeParam
326+
if tag == 'B' {
327+
tparams = r.tparamList()
328+
}
329+
rhs := r.typ()
330+
const enabled = true // This is now always enabled within the compiler.
331+
r.declare(newAliasTypeName(enabled, pos, r.currPkg, name, rhs, tparams))
328332

329333
case 'C':
330334
typ, val := r.value()

src/cmd/compile/internal/importer/ureader.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,9 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types2.Package, string) {
410410

411411
case pkgbits.ObjAlias:
412412
pos := r.pos()
413+
var tparams []*types2.TypeParam // TODO(#68778): Read tparams for unified IR.
413414
typ := r.typ()
414-
return newAliasTypeName(pr.enableAlias, pos, objPkg, objName, typ)
415+
return newAliasTypeName(pr.enableAlias, pos, objPkg, objName, typ, tparams)
415416

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

539540
// newAliasTypeName returns a new TypeName, with a materialized *types2.Alias if supported.
540-
func newAliasTypeName(aliases bool, pos syntax.Pos, pkg *types2.Package, name string, rhs types2.Type) *types2.TypeName {
541+
func newAliasTypeName(aliases bool, pos syntax.Pos, pkg *types2.Package, name string, rhs types2.Type, tparams []*types2.TypeParam) *types2.TypeName {
541542
// Copied from x/tools/internal/aliases.NewAlias via
542543
// GOROOT/src/go/internal/gcimporter/ureader.go.
543544
if aliases {
544545
tname := types2.NewTypeName(pos, pkg, name, nil)
545-
_ = types2.NewAlias(tname, rhs) // form TypeName -> Alias cycle
546+
a := types2.NewAlias(tname, rhs) // form TypeName -> Alias cycle
547+
a.SetTypeParams(tparams)
546548
return tname
547549
}
550+
assert(len(tparams) == 0)
548551
return types2.NewTypeName(pos, pkg, name, rhs)
549552
}

src/cmd/compile/internal/typecheck/iexport.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@
9090
// }
9191
//
9292
// type Alias struct {
93-
// Tag byte // 'A'
93+
// Tag byte // 'A' or 'B'
9494
// Pos Pos
95+
// TypeParams []typeOff // only present if Tag == 'B'
9596
// Type typeOff
9697
// }
9798
//

src/go/internal/gcimporter/iimport.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,13 @@ func (r *importReader) obj(name string) {
333333
pos := r.pos()
334334

335335
switch tag {
336-
case 'A':
337-
typ := r.typ()
338-
339-
r.declare(types.NewTypeName(pos, r.currPkg, name, typ))
336+
case 'A', 'B':
337+
var tparams []*types.TypeParam
338+
if tag == 'B' {
339+
tparams = r.tparamList()
340+
}
341+
rhs := r.typ()
342+
r.declare(newAliasTypeName(pos, r.currPkg, name, rhs, tparams))
340343

341344
case 'C':
342345
typ, val := r.value()

src/go/internal/gcimporter/ureader.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,9 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {
482482

483483
case pkgbits.ObjAlias:
484484
pos := r.pos()
485+
var tparams []*types.TypeParam // TODO(#68778): Read tparams for unified IR.
485486
typ := r.typ()
486-
declare(newAliasTypeName(pos, objPkg, objName, typ))
487+
declare(newAliasTypeName(pos, objPkg, objName, typ, tparams))
487488

488489
case pkgbits.ObjConst:
489490
pos := r.pos()
@@ -661,14 +662,16 @@ func pkgScope(pkg *types.Package) *types.Scope {
661662
}
662663

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

0 commit comments

Comments
 (0)