Skip to content

Commit c3f2ffe

Browse files
Some cleanup
1 parent e57c7aa commit c3f2ffe

File tree

6 files changed

+64
-35
lines changed

6 files changed

+64
-35
lines changed

build/build.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,7 @@ type Session struct {
766766
// up to date with input sources and dependencies. In the -w ("watch") mode
767767
// must be cleared upon entering watching.
768768
UpToDateArchives map[string]*compiler.Archive
769-
770-
Watcher *fsnotify.Watcher
769+
Watcher *fsnotify.Watcher
771770
}
772771

773772
// NewSession creates a new GopherJS build session.
@@ -949,9 +948,7 @@ func (s *Session) getSortedSources() []*sources.Sources {
949948
for _, srcs := range s.sources {
950949
allSources = append(allSources, srcs)
951950
}
952-
sort.Slice(allSources, func(i, j int) bool {
953-
return allSources[i].ImportPath < allSources[j].ImportPath
954-
})
951+
sources.SortedSourcesSlice(allSources)
955952
return allSources
956953
}
957954

@@ -1122,12 +1119,14 @@ func (s *Session) loadPackages(pkg *PackageData) (*sources.Sources, error) {
11221119

11231120
func (s *Session) compilePackages(rootSrcs *sources.Sources, tContext *types.Context) (*compiler.Archive, error) {
11241121
for _, srcs := range s.sources {
1125-
s.compilePackage(srcs, tContext)
1122+
if _, err := s.compilePackage(srcs, tContext); err != nil {
1123+
return nil, err
1124+
}
11261125
}
11271126

11281127
rootArchive, ok := s.UpToDateArchives[rootSrcs.ImportPath]
11291128
if !ok {
1130-
// This is simply confirmation that the root package is in the sources map and got compiled.
1129+
// This is confirmation that the root package is in the sources map and got compiled.
11311130
return nil, fmt.Errorf(`root package %q not found`, rootSrcs.ImportPath)
11321131
}
11331132
return rootArchive, nil
@@ -1154,6 +1153,7 @@ func (s *Session) compilePackage(srcs *sources.Sources, tContext *types.Context)
11541153
}
11551154

11561155
s.UpToDateArchives[srcs.ImportPath] = archive
1156+
11571157
return archive, nil
11581158
}
11591159

compiler/compiler_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,7 @@ func compileProject(t *testing.T, root *packages.Package, minify bool) map[strin
704704
for _, srcs := range allSrcs {
705705
sortedSources = append(sortedSources, srcs)
706706
}
707-
sort.Slice(sortedSources, func(i, j int) bool {
708-
return sortedSources[i].ImportPath < sortedSources[j].ImportPath
709-
})
707+
sources.SortedSourcesSlice(sortedSources)
710708
PrepareAllSources(sortedSources, importer, tContext)
711709

712710
archives := map[string]*Archive{}
@@ -733,6 +731,13 @@ func newTime(seconds float64) time.Time {
733731
func reloadCompiledProject(t *testing.T, archives map[string]*Archive, rootPkgPath string) map[string]*Archive {
734732
t.Helper()
735733

734+
// TODO(grantnelson-wf): The tests using this function are out-of-date
735+
// since they are testing the old archive caching that has been disabled.
736+
// At some point, these tests should be updated to test any new caching
737+
// mechanism that is implemented or removed. As is this function is faking
738+
// the old recursive archive loading that is no longer used since it
739+
// doesn't allow cross package analysis for generings.
740+
736741
buildTime := newTime(5.0)
737742
serialized := map[string][]byte{}
738743
for path, a := range archives {

compiler/internal/analysis/info.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ type Info struct {
5858
funcLitInfos map[*ast.FuncLit][]*FuncInfo
5959
InitFuncInfo *FuncInfo // Context for package variable initialization.
6060

61-
infoImporter InfoImporter // For functions from other packages.
61+
infoImporter InfoImporter // To get `Info` for other packages.
6262
allInfos []*FuncInfo
6363
}
6464

65+
// InfoImporter is used to get the `Info` for another package.
66+
// The path is the resolved import path of the package to get the `Info` for.
6567
type InfoImporter func(path string) (*Info, error)
6668

6769
func (info *Info) newFuncInfo(n ast.Node, obj types.Object, typeArgs typesutil.TypeList, resolver *typeparams.Resolver) *FuncInfo {

compiler/internal/analysis/info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ func TestBlocking_IsImportBlocking_ForwardInstances(t *testing.T) {
15761576
}
15771577

15781578
func TestBlocking_IsImportBlocking_BackwardInstances(t *testing.T) {
1579-
// This is tests propagation of information across package boundaries.
1579+
// This tests propagation of information across package boundaries.
15801580
// `FooBaz` has no instances in it until it is referenced in the `test` package.
15811581
// That instance information needs to propagate back across the package
15821582
// boundary to the `other` package. The information for `BazBlocker` and

compiler/package.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,18 @@ type flowData struct {
150150
endCase int
151151
}
152152

153+
// PrepareAllSources prepares all sources for compilation by
154+
// parsing go linknames, type checking, sorting, simplifying, and
155+
// performing cross package analysis.
156+
// The results are stored in the provided sources.
157+
//
158+
// All sources must be given at the same time for cross package analysis to
159+
// work correctly. For consistency, the sources should be sorted by import path.
153160
func PrepareAllSources(allSources []*sources.Sources, importer sources.Importer, tContext *types.Context) error {
154161
// This will be performed recursively for all dependencies so
155-
// most of these prepare calls will be no-ops. Since not all packages will
156-
// be recursively reached via the root source, we need to prepare them
157-
// all here.
162+
// most of these prepare calls will be no-ops.
163+
// Since some packages might not be recursively reached via the root source,
164+
// e.g. runtime, we need to try to prepare them all here.
158165
for _, srcs := range allSources {
159166
if err := srcs.Prepare(importer, sizes32, tContext); err != nil {
160167
return err

compiler/sources/sources.go

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ type Sources struct {
4444
JSFiles []jsFile.JSFile
4545

4646
// TypeInfo is the type information this package.
47-
// This is nil until PrepareInfo is called.
47+
// This is nil until Prepare is called.
4848
TypeInfo *analysis.Info
4949

5050
// Instances is the type parameters instances for this package.
51-
// This is nil until PrepareInfo is called.
51+
// This is nil until Prepare is called.
5252
Instances *typeparams.PackageInstanceSets
5353

5454
// Package is the type-PrepareInfo package.
55-
// This is nil until PrepareInfo is called.
55+
// This is nil until Prepare is called.
5656
Package *types.Package
5757

5858
// GoLinknames is the set of Go linknames for this package.
59-
// This is nil until PrepareInfo is called.
59+
// This is nil until Prepare is called.
6060
GoLinknames []linkname.GoLinkname
6161
}
6262

@@ -67,14 +67,13 @@ type Importer func(path, srcDir string) (*Sources, error)
6767
// determining the type information, go linknames, etc.
6868
//
6969
// The importer function is used to import the sources of other packages
70-
// that are imported by this package being prepared. If the other sources
71-
// are not prepared when returned by the importer, that package will be
72-
// prepared as well before continuing on with the current package.
70+
// that the package being prepared depends on. If the other sources
71+
// are not prepared when returned by the importer, then that package
72+
// will be prepared before continuing on with the current package.
7373
// This is where the recursive nature of the Prepare function comes in.
7474
//
7575
// Note that at the end of this call the analysis information
76-
// has NOT been propagated across packages yet
77-
// and the source files have not been simplified yet.
76+
// has NOT been propagated across packages yet.
7877
func (s *Sources) Prepare(importer Importer, sizes types.Sizes, tContext *types.Context) error {
7978
// Skip if the sources have already been prepared.
8079
if s.isPrepared() {
@@ -113,6 +112,12 @@ func (s *Sources) Prepare(importer Importer, sizes types.Sizes, tContext *types.
113112
return nil
114113
}
115114

115+
// isPrepared returns true if this sources have had Prepare called on it.
116+
//
117+
// This can not determine if the type information has been propagated
118+
// across packages yet, but usually would only be called prior to that.
119+
// For the source to be fully prepared for compilation, the type information
120+
// must be propagated across packages as well.
116121
func (s *Sources) isPrepared() bool {
117122
return s.TypeInfo != nil && s.Package != nil
118123
}
@@ -132,8 +137,8 @@ func (s *Sources) sort() {
132137
// Note this function mutates the original Files slice.
133138
// This must be called after TypeCheck and before analyze since
134139
// this will change the pointers in the AST, for example the pointers
135-
// to function literals will change making it impossible to find them
136-
// in the type information if analyze is called first.
140+
// to function literals will change, making it impossible to find them
141+
// in the type information, if analyze is called first.
137142
func (s *Sources) simplify(typesInfo *types.Info) {
138143
for i, file := range s.Files {
139144
s.Files[i] = astrewrite.Simplify(file, typesInfo, false)
@@ -143,7 +148,7 @@ func (s *Sources) simplify(typesInfo *types.Info) {
143148
// typeCheck the sources. Returns information about declared package types and
144149
// type information for the supplied AST.
145150
//
146-
// This must be called prior to simplify.
151+
// This must be called prior to simplify to get the types.Info used by simplify.
147152
func (s *Sources) typeCheck(importer Importer, sizes types.Sizes, tContext *types.Context) (*types.Info, error) {
148153
const errLimit = 10 // Max number of type checking errors to return.
149154

@@ -195,7 +200,9 @@ func (s *Sources) typeCheck(importer Importer, sizes types.Sizes, tContext *type
195200
// analyze will determine the type parameters instances, blocking,
196201
// and other type information for the package.
197202
//
198-
// This must be called after to simplify.
203+
// This must be called after to simplify to ensure the pointers
204+
// in the AST are still valid.
205+
//
199206
// Note that at the end of this call the analysis information
200207
// has NOT been propagated across packages yet.
201208
func (s *Sources) analyze(typesInfo *types.Info, importer Importer, tContext *types.Context) {
@@ -222,7 +229,6 @@ func (s *Sources) analyze(typesInfo *types.Info, importer Importer, tContext *ty
222229
// parseGoLinknames extracts all //go:linkname compiler directive from the sources.
223230
//
224231
// This will set the GoLinknames field on the Sources struct.
225-
// This must be called prior to simplify.
226232
func (s *Sources) parseGoLinknames() error {
227233
goLinknames := []linkname.GoLinkname{}
228234
var errs errorList.ErrorList
@@ -290,13 +296,22 @@ func (pi *packageImporter) Import(path string) (*types.Package, error) {
290296
return nil, err
291297
}
292298

293-
if !srcs.isPrepared() {
294-
err := srcs.Prepare(pi.importer, pi.sizes, pi.tContext)
295-
if err != nil {
296-
pi.Errors = pi.Errors.AppendDistinct(err)
297-
return nil, err
298-
}
299+
// If the source hasn't been prepared yet, prepare it now.
300+
// This will recursively prepare all of it's dependencies too.
301+
// If the source is already prepared, this will be a no-op.
302+
err = srcs.Prepare(pi.importer, pi.sizes, pi.tContext)
303+
if err != nil {
304+
pi.Errors = pi.Errors.AppendDistinct(err)
305+
return nil, err
299306
}
300307

301308
return srcs.Package, nil
302309
}
310+
311+
// SortedSourcesSlice in place sorts the given slice of Sources by ImportPath.
312+
// This will not change the order of the files within any Sources.
313+
func SortedSourcesSlice(sourcesSlice []*Sources) {
314+
sort.Slice(sourcesSlice, func(i, j int) bool {
315+
return sourcesSlice[i].ImportPath < sourcesSlice[j].ImportPath
316+
})
317+
}

0 commit comments

Comments
 (0)