loader: use name "main" for the main package

This should make exported names a bit more consistent.

I believe there was a bug report for this issue, but I can't easily find
it. In any case, I think it's an important improvement to match the
behavior of the Go toolchain.
Этот коммит содержится в:
Ayke van Laethem 2021-01-13 17:00:08 +01:00 коммит произвёл Ron Evans
родитель c4d642007e
коммит dbc438b2ae
2 изменённых файлов: 13 добавлений и 3 удалений

Просмотреть файл

@ -67,7 +67,7 @@ func NewProgram(lprogram *loader.Program) *Program {
program := lprogram.LoadSSA()
program.Build()
mainPkg := program.ImportedPackage(lprogram.MainPkg().ImportPath)
mainPkg := program.Package(lprogram.MainPkg().Pkg)
if mainPkg == nil {
panic("could not find main package")
}
@ -79,7 +79,7 @@ func NewProgram(lprogram *loader.Program) *Program {
}
for _, pkg := range lprogram.Sorted() {
p.AddPackage(program.ImportedPackage(pkg.ImportPath))
p.AddPackage(program.Package(pkg.Pkg))
}
return p

Просмотреть файл

@ -43,6 +43,7 @@ type Program struct {
type PackageJSON struct {
Dir string
ImportPath string
Name string
ForTest string
// Source files
@ -335,7 +336,16 @@ func (p *Package) Check() error {
// Do typechecking of the package.
checker.Importer = p
typesPkg, err := checker.Check(p.ImportPath, p.program.fset, p.Files, &p.info)
packageName := p.ImportPath
if p.Name == "main" {
// The main package normally has a different import path, such as
// "command-line-arguments" or "./testdata/cgo". Therefore, use the name
// "main" in such a case: this package isn't imported from anywhere.
// This is safe as it isn't possible to import a package with the name
// "main".
packageName = "main"
}
typesPkg, err := checker.Check(packageName, p.program.fset, p.Files, &p.info)
if err != nil {
if err, ok := err.(Errors); ok {
return err