mirror of https://github.com/golang/go.git
[dev.go2go] go/go2go: add package qualifier when needed
Fixes #39842 Change-Id: I5046f1bdd7dbc6d1e17cf3835826933aaadf1581 Reviewed-on: https://go-review.googlesource.com/c/go/+/239711 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
a9f31a8c68
commit
4dfbf5ab9c
|
|
@ -648,6 +648,7 @@ func (t *translator) translateExpr(pe *ast.Expr) {
|
|||
}
|
||||
switch e := (*pe).(type) {
|
||||
case *ast.Ident:
|
||||
t.translateIdent(pe)
|
||||
case *ast.Ellipsis:
|
||||
t.translateExpr(&e.Elt)
|
||||
case *ast.BasicLit:
|
||||
|
|
@ -715,6 +716,38 @@ func (t *translator) translateExpr(pe *ast.Expr) {
|
|||
}
|
||||
}
|
||||
|
||||
// translateIdent translates a simple identifier from Go with
|
||||
// contracts to Go 1. These are usually fine as is, but a reference
|
||||
// to a non-generic name in another package may need a package qualifier.
|
||||
func (t *translator) translateIdent(pe *ast.Expr) {
|
||||
e := (*pe).(*ast.Ident)
|
||||
obj := t.importer.info.ObjectOf(e)
|
||||
if obj == nil {
|
||||
return
|
||||
}
|
||||
if named, ok := obj.Type().(*types.Named); ok && len(named.TParams()) > 0 {
|
||||
// A generic function that will be instantiated locally.
|
||||
return
|
||||
}
|
||||
ipkg := obj.Pkg()
|
||||
if ipkg == nil || ipkg == t.tpkg {
|
||||
// We don't need a package qualifier if it's defined
|
||||
// in the current package.
|
||||
return
|
||||
}
|
||||
if obj.Parent() != ipkg.Scope() {
|
||||
// We only need a package qualifier if it's defined in
|
||||
// package scope.
|
||||
return
|
||||
}
|
||||
|
||||
// Add package qualifier.
|
||||
*pe = &ast.SelectorExpr{
|
||||
X: ast.NewIdent(ipkg.Name()),
|
||||
Sel: e,
|
||||
}
|
||||
}
|
||||
|
||||
// translateSelectorExpr translates a selector expression
|
||||
// from Go with contracts to Go 1.
|
||||
func (t *translator) translateSelectorExpr(pe *ast.Expr) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package a
|
||||
|
||||
func F() {}
|
||||
|
||||
func G(type T)() {
|
||||
F()
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import "./a"
|
||||
|
||||
func main() {
|
||||
a.G(int)()
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// rundir
|
||||
|
||||
// Copyright 2020 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ignored
|
||||
Loading…
Reference in New Issue