diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index 4efbcc44c..f89af6d9e 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -246,7 +246,11 @@ func (c *codegen) analyzeFuncUsage() funcUsage { case *ast.CallExpr: switch t := n.Fun.(type) { case *ast.Ident: - usage[c.getIdentName("", t.Name)] = true + var pkgPath string + if !isMain { + pkgPath = pkg.Path() + } + usage[c.getIdentName(pkgPath, t.Name)] = true case *ast.SelectorExpr: name, _ := c.getFuncNameFromSelector(t) usage[name] = true @@ -254,7 +258,7 @@ func (c *codegen) analyzeFuncUsage() funcUsage { case *ast.FuncDecl: // exported functions are always assumed to be used if isMain && n.Name.IsExported() { - usage[c.getFuncNameFromDecl(pkg.Path(), n)] = true + usage[c.getFuncNameFromDecl("", n)] = true } } return true diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 062dc185f..62cbe2f34 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1986,7 +1986,11 @@ func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error { case *ast.FuncDecl: // Don't convert the function if it's not used. This will save a lot // of bytecode space. - name := c.getFuncNameFromDecl(pkg.Path(), n) + pkgPath := "" + if pkg != c.mainPkg.Pkg { // not a main package + pkgPath = pkg.Path() + } + name := c.getFuncNameFromDecl(pkgPath, n) if !isInitFunc(n) && !isDeployFunc(n) && funUsage.funcUsed(name) && (!isInteropPath(pkg.Path()) && !canInline(pkg.Path())) { c.convertFuncDecl(f, n, pkg) diff --git a/pkg/compiler/import_test.go b/pkg/compiler/import_test.go index def752427..c72097a56 100644 --- a/pkg/compiler/import_test.go +++ b/pkg/compiler/import_test.go @@ -48,3 +48,16 @@ func TestMultipleDirFileImport(t *testing.T) { ` eval(t, src, big.NewInt(1)) } + +func TestImportNameSameAsOwn(t *testing.T) { + src := `package foo + import "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/foo" + func get3() int { return 3 } + func Main() int { + return get3() + } + func unused() int { + return foo.Bar() + }` + eval(t, src, big.NewInt(3)) +}