Skip to content

Commit 2db563b

Browse files
committed
internal/gcimporter: copy over ureader changes
Copy over the ureader.go changes from GOROOT's go/internal/gcimporter. Adds a test that goes through gc export data. Updates golang/go#68778 Change-Id: Ie4b91dfdb1ab9f952631a34c3691dc84be8831a7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/609317 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 09886e0 commit 2db563b

File tree

6 files changed

+131
-10
lines changed

6 files changed

+131
-10
lines changed

internal/gcimporter/gcimporter_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,67 @@ func TestIssue58296(t *testing.T) {
956956
}
957957
}
958958

959+
func TestIssueAliases(t *testing.T) {
960+
// This package only handles gc export data.
961+
testenv.NeedsGo1Point(t, 24)
962+
needsCompiler(t, "gc")
963+
testenv.NeedsGoBuild(t) // to find stdlib export data in the build cache
964+
testenv.NeedsGoExperiment(t, "aliastypeparams")
965+
966+
t.Setenv("GODEBUG", fmt.Sprintf("gotypesalias=%d", 1))
967+
968+
tmpdir := mktmpdir(t)
969+
defer os.RemoveAll(tmpdir)
970+
testoutdir := filepath.Join(tmpdir, "testdata")
971+
972+
apkg := filepath.Join(testoutdir, "a")
973+
bpkg := filepath.Join(testoutdir, "b")
974+
cpkg := filepath.Join(testoutdir, "c")
975+
976+
// compile a, b and c into gc export data.
977+
srcdir := filepath.Join("testdata", "aliases")
978+
compilePkg(t, filepath.Join(srcdir, "a"), "a.go", testoutdir, nil, apkg)
979+
compilePkg(t, filepath.Join(srcdir, "b"), "b.go", testoutdir, map[string]string{apkg: filepath.Join(testoutdir, "a.o")}, bpkg)
980+
compilePkg(t, filepath.Join(srcdir, "c"), "c.go", testoutdir,
981+
map[string]string{apkg: filepath.Join(testoutdir, "a.o"), bpkg: filepath.Join(testoutdir, "b.o")},
982+
cpkg,
983+
)
984+
985+
// import c from gc export data using a and b.
986+
pkg, err := Import(map[string]*types.Package{
987+
apkg: types.NewPackage(apkg, "a"),
988+
bpkg: types.NewPackage(bpkg, "b"),
989+
}, "./c", testoutdir, nil)
990+
if err != nil {
991+
t.Fatal(err)
992+
}
993+
994+
// Check c's objects and types.
995+
var objs []string
996+
for _, imp := range pkg.Scope().Names() {
997+
obj := pkg.Scope().Lookup(imp)
998+
s := fmt.Sprintf("%s : %s", obj.Name(), obj.Type())
999+
s = strings.ReplaceAll(s, testoutdir, "testdata")
1000+
objs = append(objs, s)
1001+
}
1002+
sort.Strings(objs)
1003+
1004+
want := strings.Join([]string{
1005+
"S : struct{F int}",
1006+
"T : struct{F int}",
1007+
"U : testdata/a.A[string]",
1008+
"V : testdata/a.A[int]",
1009+
"W : testdata/b.B[string]",
1010+
"X : testdata/b.B[int]",
1011+
"Y : testdata/c.c[string]",
1012+
"Z : testdata/c.c[int]",
1013+
"c : testdata/c.c",
1014+
}, ",")
1015+
if got := strings.Join(objs, ","); got != want {
1016+
t.Errorf("got imports %v for package c. wanted %v", objs, want)
1017+
}
1018+
}
1019+
9591020
// apkg returns the package "a" prefixed by (as a package) testoutdir
9601021
func apkg(testoutdir string) string {
9611022
apkg := testoutdir + "/a"

internal/gcimporter/iexport_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,6 @@ type Chained = C[Named] // B[Named, A[Named]] = B[Named, *Named] = []*Named
517517
// This means that it can be loaded by go/importer or go/types.
518518
// This step is not supported, but it does give test coverage for stdlib.
519519
"goroot": func(t *testing.T) *types.Package {
520-
t.Skip("Fix bug in src/internal/gcimporter.IImportData for aliasType then reenable")
521-
522520
// Write indexed export data file contents.
523521
//
524522
// TODO(taking): Slightly unclear to what extent this step should be supported by go/importer.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
type A[T any] = *T
8+
9+
type B = struct{ F int }
10+
11+
func F() B {
12+
type a[T any] = struct{ F T }
13+
return a[int]{}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package b
6+
7+
import "./a"
8+
9+
type B[S any] = struct {
10+
F a.A[[]S]
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package c
6+
7+
import (
8+
"./a"
9+
"./b"
10+
)
11+
12+
type c[V any] = struct {
13+
G b.B[[3]V]
14+
}
15+
16+
var S struct{ F int } = a.B{}
17+
var T struct{ F int } = a.F()
18+
19+
var U a.A[string] = (*string)(nil)
20+
var V a.A[int] = (*int)(nil)
21+
22+
var W b.B[string] = struct{ F *[]string }{}
23+
var X b.B[int] = struct{ F *[]int }{}
24+
25+
var Y c[string] = struct{ G struct{ F *[][3]string } }{}
26+
var Z c[int] = struct{ G struct{ F *[][3]int } }{}

internal/gcimporter/ureader_yes.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ func (pr *pkgReader) later(fn func()) {
5252

5353
// See cmd/compile/internal/noder.derivedInfo.
5454
type derivedInfo struct {
55-
idx pkgbits.Index
56-
needed bool
55+
idx pkgbits.Index
5756
}
5857

5958
// See cmd/compile/internal/noder.typeInfo.
@@ -110,13 +109,17 @@ func readUnifiedPackage(fset *token.FileSet, ctxt *types.Context, imports map[st
110109

111110
r := pr.newReader(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic)
112111
pkg := r.pkg()
113-
r.Bool() // has init
112+
if r.Version().Has(pkgbits.HasInit) {
113+
r.Bool()
114+
}
114115

115116
for i, n := 0, r.Len(); i < n; i++ {
116117
// As if r.obj(), but avoiding the Scope.Lookup call,
117118
// to avoid eager loading of imports.
118119
r.Sync(pkgbits.SyncObject)
119-
assert(!r.Bool())
120+
if r.Version().Has(pkgbits.DerivedFuncInstance) {
121+
assert(!r.Bool())
122+
}
120123
r.p.objIdx(r.Reloc(pkgbits.RelocObj))
121124
assert(r.Len() == 0)
122125
}
@@ -165,7 +168,7 @@ type readerDict struct {
165168
// tparams is a slice of the constructed TypeParams for the element.
166169
tparams []*types.TypeParam
167170

168-
// devived is a slice of types derived from tparams, which may be
171+
// derived is a slice of types derived from tparams, which may be
169172
// instantiated while reading the current element.
170173
derived []derivedInfo
171174
derivedTypes []types.Type // lazily instantiated from derived
@@ -471,7 +474,9 @@ func (r *reader) param() *types.Var {
471474
func (r *reader) obj() (types.Object, []types.Type) {
472475
r.Sync(pkgbits.SyncObject)
473476

474-
assert(!r.Bool())
477+
if r.Version().Has(pkgbits.DerivedFuncInstance) {
478+
assert(!r.Bool())
479+
}
475480

476481
pkg, name := r.p.objIdx(r.Reloc(pkgbits.RelocObj))
477482
obj := pkgScope(pkg).Lookup(name)
@@ -525,8 +530,11 @@ func (pr *pkgReader) objIdx(idx pkgbits.Index) (*types.Package, string) {
525530

526531
case pkgbits.ObjAlias:
527532
pos := r.pos()
533+
var tparams []*types.TypeParam
534+
if r.Version().Has(pkgbits.AliasTypeParamNames) {
535+
tparams = r.typeParamNames()
536+
}
528537
typ := r.typ()
529-
var tparams []*types.TypeParam // TODO(#68778): read type params once pkgbits.V2 is available.
530538
declare(aliases.NewAlias(r.p.aliases, pos, objPkg, objName, typ, tparams))
531539

532540
case pkgbits.ObjConst:
@@ -633,7 +641,10 @@ func (pr *pkgReader) objDictIdx(idx pkgbits.Index) *readerDict {
633641
dict.derived = make([]derivedInfo, r.Len())
634642
dict.derivedTypes = make([]types.Type, len(dict.derived))
635643
for i := range dict.derived {
636-
dict.derived[i] = derivedInfo{r.Reloc(pkgbits.RelocType), r.Bool()}
644+
dict.derived[i] = derivedInfo{idx: r.Reloc(pkgbits.RelocType)}
645+
if r.Version().Has(pkgbits.DerivedInfoNeeded) {
646+
assert(!r.Bool())
647+
}
637648
}
638649

639650
pr.retireReader(r)

0 commit comments

Comments
 (0)