Merge pull request #1808 from nspcc-dev/compiler/importalias

compiler: fix import alias handling
This commit is contained in:
Roman Khimov 2021-03-04 16:46:44 +03:00 committed by GitHub
commit c0d066f031
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View file

@ -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

View file

@ -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)

View file

@ -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))
}