diff --git a/internal/lsp/cache/check.go b/internal/lsp/cache/check.go index c295c298..5c8cdf1d 100644 --- a/internal/lsp/cache/check.go +++ b/internal/lsp/cache/check.go @@ -85,19 +85,11 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) { if !ok { return nil, fmt.Errorf("no metadata for %v", pkgPath) } - // Use the default type information for the unsafe package. - var typ *types.Package - if meta.pkgPath == "unsafe" { - typ = types.Unsafe - } else { - typ = types.NewPackage(meta.pkgPath, meta.name) - } pkg := &pkg{ id: meta.id, pkgPath: meta.pkgPath, files: meta.files, imports: make(map[string]*pkg), - types: typ, typesSizes: meta.typesSizes, typesInfo: &types.Info{ Types: make(map[ast.Expr]types.TypeAndValue), @@ -109,25 +101,23 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) { }, analyses: make(map[*analysis.Analyzer]*analysisEntry), } - appendError := func(err error) { - imp.view.appendPkgError(pkg, err) - } - // Ignore function bodies for any dependency packages. ignoreFuncBodies := imp.topLevelPkgID != pkg.id - - // Don't type-check function bodies if we are not in the top-level package. files, parseErrs, err := imp.parseFiles(meta.files, ignoreFuncBodies) if err != nil { return nil, err } for _, err := range parseErrs { - appendError(err) + imp.view.appendPkgError(pkg, err) } - // If something unexpected happens, don't cache a package with 0 parsed files. - if len(files) == 0 { + // Use the default type information for the unsafe package. + if meta.pkgPath == "unsafe" { + pkg.types = types.Unsafe + } else if len(files) == 0 { // not the unsafe package, no parsed files return nil, fmt.Errorf("no parsed files for package %s", pkg.pkgPath) + } else { + pkg.types = types.NewPackage(meta.pkgPath, meta.name) } pkg.syntax = files @@ -140,7 +130,9 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) { seen[pkgPath] = struct{}{} cfg := &types.Config{ - Error: appendError, + Error: func(err error) { + imp.view.appendPkgError(pkg, err) + }, IgnoreFuncBodies: ignoreFuncBodies, Importer: &importer{ view: imp.view, diff --git a/internal/lsp/testdata/unsafe/unsafe.go b/internal/lsp/testdata/unsafe/unsafe.go new file mode 100644 index 00000000..e6a9f8ee --- /dev/null +++ b/internal/lsp/testdata/unsafe/unsafe.go @@ -0,0 +1,14 @@ +package unsafe + +import ( + "unsafe" +) + +// Pre-set this marker, as we don't have a "source" for it in this package. +/* unsafe.Sizeof */ //@item(Sizeof, "Sizeof", "invalid type", "text") + +func _() { + x := struct{}{} + _ = unsafe.Sizeof(x) //@complete("i", Sizeof) +} + diff --git a/internal/lsp/tests/tests.go b/internal/lsp/tests/tests.go index 81f1a107..ff6e2b0b 100644 --- a/internal/lsp/tests/tests.go +++ b/internal/lsp/tests/tests.go @@ -26,7 +26,7 @@ import ( // We hardcode the expected number of test cases to ensure that all tests // are being executed. If a test is added, this number must be changed. const ( - ExpectedCompletionsCount = 121 + ExpectedCompletionsCount = 122 ExpectedCompletionSnippetCount = 14 ExpectedDiagnosticsCount = 17 ExpectedFormatCount = 5