diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index 41526aba0..34b648656 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -113,10 +113,11 @@ func lastStmtIsReturn(decl *ast.FuncDecl) (b bool) { return false } -func analyzeFuncUsage(pkgs map[*types.Package]*loader.PackageInfo) funcUsage { +func analyzeFuncUsage(mainPkg *loader.PackageInfo, pkgs map[*types.Package]*loader.PackageInfo) funcUsage { usage := funcUsage{} for _, pkg := range pkgs { + isMain := pkg == mainPkg for _, f := range pkg.Files { ast.Inspect(f, func(node ast.Node) bool { switch n := node.(type) { @@ -127,6 +128,11 @@ func analyzeFuncUsage(pkgs map[*types.Package]*loader.PackageInfo) funcUsage { case *ast.SelectorExpr: usage[t.Sel.Name] = true } + case *ast.FuncDecl: + // exported functions are always assumed to be used + if isMain && n.Name.IsExported() { + usage[n.Name.Name] = true + } } return true }) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 1e91e3bb8..25d9a6e62 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1426,7 +1426,7 @@ func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error { return c.prog.Err } - funUsage := analyzeFuncUsage(info.program.AllPackages) + funUsage := analyzeFuncUsage(pkg, info.program.AllPackages) // Bring all imported functions into scope. for _, pkg := range info.program.AllPackages {