Commit b2a2a605 authored by Robert Griesemer's avatar Robert Griesemer

go/internal/srcimporter: report reimport of incomplete packages

See the issue below for details.

For #19337.

Change-Id: I7637dcd4408f1bc4a9b3050a107aadb4de6f950b
Reviewed-on: https://go-review.googlesource.com/37620Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent e54bc92a
...@@ -92,10 +92,15 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type ...@@ -92,10 +92,15 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type
if pkg == &importing { if pkg == &importing {
return nil, fmt.Errorf("import cycle through package %q", bp.ImportPath) return nil, fmt.Errorf("import cycle through package %q", bp.ImportPath)
} }
if pkg.Complete() { if !pkg.Complete() {
// package exists but is not complete - we cannot handle this
// at the moment since the source importer replaces the package
// wholesale rather than augmenting it (see #19337 for details)
return nil, fmt.Errorf("reimported partially imported package %q", bp.ImportPath)
}
return pkg, nil return pkg, nil
} }
} else {
p.packages[bp.ImportPath] = &importing p.packages[bp.ImportPath] = &importing
defer func() { defer func() {
// clean up in case of error // clean up in case of error
...@@ -106,7 +111,6 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type ...@@ -106,7 +111,6 @@ func (p *Importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type
p.packages[bp.ImportPath] = nil p.packages[bp.ImportPath] = nil
} }
}() }()
}
// collect package files // collect package files
bp, err = p.ctxt.ImportDir(bp.Dir, 0) bp, err = p.ctxt.ImportDir(bp.Dir, 0)
......
...@@ -132,3 +132,19 @@ func TestImportedTypes(t *testing.T) { ...@@ -132,3 +132,19 @@ func TestImportedTypes(t *testing.T) {
} }
} }
} }
func TestReimport(t *testing.T) {
if runtime.GOOS == "nacl" {
t.Skip("no source code available")
}
// Reimporting a partially imported (incomplete) package is not supported (see issue #19337).
// Make sure we recognize the situation and report an error.
mathPkg := types.NewPackage("math", "math") // incomplete package
importer := New(&build.Default, token.NewFileSet(), map[string]*types.Package{mathPkg.Path(): mathPkg})
_, err := importer.ImportFrom("math", ".", 0)
if err == nil || !strings.HasPrefix(err.Error(), "reimport") {
t.Errorf("got %v; want reimport error", err)
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment