internal/lsp: fix type checking for unsafe package

Change-Id: I229a9329f38b8fc7f38e964652c582858c4edb5b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181678
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-06-11 14:50:43 -04:00
parent 1d0142ba47
commit 5b939d657d
3 changed files with 25 additions and 19 deletions

View File

@ -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,

14
internal/lsp/testdata/unsafe/unsafe.go vendored Normal file
View File

@ -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)
}

View File

@ -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