Skip to content

Commit 6480332

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/lsp/cache: use types.Unsafe in analysisPackage
Before, the early return in typeCheck forgot to set an.types. Now the logic is pulled into single spot in the caller. This change consolidates the various places that set analysisNode.types to just two (syntax and export). Change-Id: I2f1ea6b70888bd5874f76c57443fe46b1c04ae67 Reviewed-on: https://go-review.googlesource.com/c/tools/+/504558 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Alan Donovan <[email protected]> Auto-Submit: Alan Donovan <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 155320d commit 6480332

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

gopls/internal/lsp/cache/analysis.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,6 @@ func (snapshot *snapshot) Analyze(ctx context.Context, pkgs map[PackageID]unit,
229229
allDeps: make(map[PackagePath]*analysisNode),
230230
exportDeps: make(map[PackagePath]*analysisNode),
231231
}
232-
// Unsafe must use a distinguished types.Package.
233-
// Mark it as fully populated from birth.
234-
if m.PkgPath == "unsafe" {
235-
an.typesOnce.Do(func() { an.types = types.Unsafe })
236-
}
237232
nodes[id] = an
238233

239234
// -- recursion --
@@ -405,6 +400,11 @@ func (an *analysisNode) String() string { return string(an.m.ID) }
405400
// Postcondition: an.types and an.exportDeps are populated.
406401
func (an *analysisNode) _import() *types.Package {
407402
an.typesOnce.Do(func() {
403+
if an.m.PkgPath == "unsafe" {
404+
an.types = types.Unsafe
405+
return
406+
}
407+
408408
an.types = types.NewPackage(string(an.m.PkgPath), string(an.m.Name))
409409

410410
// getPackages recursively imports each dependency
@@ -662,6 +662,12 @@ func (an *analysisNode) run(ctx context.Context) (*analyzeSummary, error) {
662662
// Type-check the package syntax.
663663
pkg := an.typeCheck(parsed)
664664

665+
// Publish the completed package.
666+
an.typesOnce.Do(func() { an.types = pkg.types })
667+
if an.types != pkg.types {
668+
log.Fatalf("typesOnce prematurely done")
669+
}
670+
665671
// Compute the union of exportDeps across our direct imports.
666672
// This is the set that will be needed by the fact decoder.
667673
allExportDeps := make(map[PackagePath]*analysisNode)
@@ -747,18 +753,17 @@ func (an *analysisNode) run(ctx context.Context) (*analyzeSummary, error) {
747753
}, nil
748754
}
749755

750-
// Postcondition: an.types and an.exportDeps are populated.
756+
// Postcondition: analysisPackage.types and an.exportDeps are populated.
751757
func (an *analysisNode) typeCheck(parsed []*source.ParsedGoFile) *analysisPackage {
752758
m := an.m
753-
fset := an.fset
754759

755760
if false { // debugging
756761
log.Println("typeCheck", m.ID)
757762
}
758763

759764
pkg := &analysisPackage{
760765
m: m,
761-
fset: fset,
766+
fset: an.fset,
762767
parsed: parsed,
763768
files: make([]*ast.File, len(parsed)),
764769
compiles: len(m.Errors) == 0, // false => list error
@@ -777,6 +782,7 @@ func (an *analysisNode) typeCheck(parsed []*source.ParsedGoFile) *analysisPackag
777782

778783
// Unsafe has no syntax.
779784
if m.PkgPath == "unsafe" {
785+
pkg.types = types.Unsafe
780786
return pkg
781787
}
782788

@@ -862,7 +868,7 @@ func (an *analysisNode) typeCheck(parsed []*source.ParsedGoFile) *analysisPackag
862868
// TODO(adonovan): do we actually need this??
863869
typesinternal.SetUsesCgo(cfg)
864870

865-
check := types.NewChecker(cfg, fset, pkg.types, pkg.typesInfo)
871+
check := types.NewChecker(cfg, pkg.fset, pkg.types, pkg.typesInfo)
866872

867873
// Type checking errors are handled via the config, so ignore them here.
868874
_ = check.Files(pkg.files)
@@ -874,12 +880,6 @@ func (an *analysisNode) typeCheck(parsed []*source.ParsedGoFile) *analysisPackag
874880
}
875881
}
876882

877-
// Publish the completed package.
878-
an.typesOnce.Do(func() { an.types = pkg.types })
879-
if an.types != pkg.types {
880-
log.Fatalf("typesOnce prematurely done")
881-
}
882-
883883
// Emit the export data and compute the recursive hash.
884884
export, err := gcimporter.IExportShallow(pkg.fset, pkg.types)
885885
if err != nil {

0 commit comments

Comments
 (0)