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:
parent
1d0142ba47
commit
5b939d657d
|
@ -85,19 +85,11 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("no metadata for %v", pkgPath)
|
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{
|
pkg := &pkg{
|
||||||
id: meta.id,
|
id: meta.id,
|
||||||
pkgPath: meta.pkgPath,
|
pkgPath: meta.pkgPath,
|
||||||
files: meta.files,
|
files: meta.files,
|
||||||
imports: make(map[string]*pkg),
|
imports: make(map[string]*pkg),
|
||||||
types: typ,
|
|
||||||
typesSizes: meta.typesSizes,
|
typesSizes: meta.typesSizes,
|
||||||
typesInfo: &types.Info{
|
typesInfo: &types.Info{
|
||||||
Types: make(map[ast.Expr]types.TypeAndValue),
|
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),
|
analyses: make(map[*analysis.Analyzer]*analysisEntry),
|
||||||
}
|
}
|
||||||
appendError := func(err error) {
|
|
||||||
imp.view.appendPkgError(pkg, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ignore function bodies for any dependency packages.
|
// Ignore function bodies for any dependency packages.
|
||||||
ignoreFuncBodies := imp.topLevelPkgID != pkg.id
|
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)
|
files, parseErrs, err := imp.parseFiles(meta.files, ignoreFuncBodies)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, err := range parseErrs {
|
for _, err := range parseErrs {
|
||||||
appendError(err)
|
imp.view.appendPkgError(pkg, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If something unexpected happens, don't cache a package with 0 parsed files.
|
// Use the default type information for the unsafe package.
|
||||||
if len(files) == 0 {
|
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)
|
return nil, fmt.Errorf("no parsed files for package %s", pkg.pkgPath)
|
||||||
|
} else {
|
||||||
|
pkg.types = types.NewPackage(meta.pkgPath, meta.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
pkg.syntax = files
|
pkg.syntax = files
|
||||||
|
@ -140,7 +130,9 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
|
||||||
seen[pkgPath] = struct{}{}
|
seen[pkgPath] = struct{}{}
|
||||||
|
|
||||||
cfg := &types.Config{
|
cfg := &types.Config{
|
||||||
Error: appendError,
|
Error: func(err error) {
|
||||||
|
imp.view.appendPkgError(pkg, err)
|
||||||
|
},
|
||||||
IgnoreFuncBodies: ignoreFuncBodies,
|
IgnoreFuncBodies: ignoreFuncBodies,
|
||||||
Importer: &importer{
|
Importer: &importer{
|
||||||
view: imp.view,
|
view: imp.view,
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ import (
|
||||||
// We hardcode the expected number of test cases to ensure that all tests
|
// 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.
|
// are being executed. If a test is added, this number must be changed.
|
||||||
const (
|
const (
|
||||||
ExpectedCompletionsCount = 121
|
ExpectedCompletionsCount = 122
|
||||||
ExpectedCompletionSnippetCount = 14
|
ExpectedCompletionSnippetCount = 14
|
||||||
ExpectedDiagnosticsCount = 17
|
ExpectedDiagnosticsCount = 17
|
||||||
ExpectedFormatCount = 5
|
ExpectedFormatCount = 5
|
||||||
|
|
Loading…
Reference in New Issue