go/gcimporter15: implement support for exporting aliases
Tested with 1.6, 1.7, 1.8. Change-Id: Ib0f751484c360b02aa34c993ce795cb94656705f Reviewed-on: https://go-review.googlesource.com/32540 Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
5b3db00587
commit
a4fe4f6140
|
@ -173,6 +173,17 @@ func (p *exporter) pkg(pkg *types.Package, emptypath bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *exporter) obj(obj types.Object) {
|
func (p *exporter) obj(obj types.Object) {
|
||||||
|
if orig := original(obj); orig != obj {
|
||||||
|
if orig == nil {
|
||||||
|
// invalid alias - don't export for now (issue 17731)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p.tag(aliasTag)
|
||||||
|
p.pos(obj)
|
||||||
|
p.string(obj.Name())
|
||||||
|
obj = orig
|
||||||
|
}
|
||||||
|
|
||||||
switch obj := obj.(type) {
|
switch obj := obj.(type) {
|
||||||
case *types.Const:
|
case *types.Const:
|
||||||
p.tag(constTag)
|
p.tag(constTag)
|
||||||
|
|
|
@ -35,14 +35,16 @@ func TestImportTestdataNewExport(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if outFn := compileNewExport(t, "testdata", "exports.go"); outFn != "" {
|
if outFn := compileNewExport(t, "testdata", testfile); outFn != "" {
|
||||||
defer os.Remove(outFn)
|
defer os.Remove(outFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pkg := testPath(t, "./testdata/exports", "."); pkg != nil {
|
// filename should end with ".go"
|
||||||
|
filename := testfile[:len(testfile)-3]
|
||||||
|
if pkg := testPath(t, "./testdata/"+filename, "."); pkg != nil {
|
||||||
// The package's Imports list must include all packages
|
// The package's Imports list must include all packages
|
||||||
// explicitly imported by exports.go, plus all packages
|
// explicitly imported by testfile, plus all packages
|
||||||
// referenced indirectly via exported objects in exports.go.
|
// referenced indirectly via exported objects in testfile.
|
||||||
want := `[package ast ("go/ast") package token ("go/token")]`
|
want := `[package ast ("go/ast") package token ("go/token")]`
|
||||||
got := fmt.Sprint(pkg.Imports())
|
got := fmt.Sprint(pkg.Imports())
|
||||||
if got != want {
|
if got != want {
|
||||||
|
|
|
@ -137,14 +137,16 @@ func TestImportTestdata(t *testing.T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if outFn := compile(t, "testdata", "exports.go"); outFn != "" {
|
if outFn := compile(t, "testdata", testfile); outFn != "" {
|
||||||
defer os.Remove(outFn)
|
defer os.Remove(outFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pkg := testPath(t, "./testdata/exports", "."); pkg != nil {
|
// filename should end with ".go"
|
||||||
|
filename := testfile[:len(testfile)-3]
|
||||||
|
if pkg := testPath(t, "./testdata/"+filename, "."); pkg != nil {
|
||||||
// The package's Imports list must include all packages
|
// The package's Imports list must include all packages
|
||||||
// explicitly imported by exports.go, plus all packages
|
// explicitly imported by testfile, plus all packages
|
||||||
// referenced indirectly via exported objects in exports.go.
|
// referenced indirectly via exported objects in testfile.
|
||||||
// With the textual export format, the list may also include
|
// With the textual export format, the list may also include
|
||||||
// additional packages that are not strictly required for
|
// additional packages that are not strictly required for
|
||||||
// import processing alone (they are exported to err "on
|
// import processing alone (they are exported to err "on
|
||||||
|
|
|
@ -15,3 +15,9 @@ func newAlias(pos token.Pos, pkg *types.Package, name string, orig types.Object)
|
||||||
errorf("unexpected alias in non-Go1.8 export data: %s.%s => %v", pkg.Name(), name, orig)
|
errorf("unexpected alias in non-Go1.8 export data: %s.%s => %v", pkg.Name(), name, orig)
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func original(obj types.Object) types.Object {
|
||||||
|
return obj // don't know about aliases
|
||||||
|
}
|
||||||
|
|
||||||
|
const testfile = "exports17.go"
|
||||||
|
|
|
@ -9,3 +9,13 @@ package gcimporter
|
||||||
import "go/types"
|
import "go/types"
|
||||||
|
|
||||||
func newAlias => types.NewAlias
|
func newAlias => types.NewAlias
|
||||||
|
|
||||||
|
// TODO(gri) Consider exporting this functionality from go/types (issue 17730).
|
||||||
|
func original(obj types.Object) types.Object {
|
||||||
|
if orig, ok := obj.(*types.Alias); ok {
|
||||||
|
return orig
|
||||||
|
}
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
const testfile = "exports18.go"
|
||||||
|
|
|
@ -0,0 +1,109 @@
|
||||||
|
// Copyright 2011 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.
|
||||||
|
|
||||||
|
// This file is used to generate an object file which
|
||||||
|
// serves as test file for gcimporter_test.go.
|
||||||
|
|
||||||
|
package exports
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/ast"
|
||||||
|
"go/build"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Issue 3682: Correctly read dotted identifiers from export data.
|
||||||
|
const init1 = 0
|
||||||
|
|
||||||
|
func init() {}
|
||||||
|
|
||||||
|
const (
|
||||||
|
C0 int = 0
|
||||||
|
C1 = 3.14159265
|
||||||
|
C2 = 2.718281828i
|
||||||
|
C3 = -123.456e-789
|
||||||
|
C4 = +123.456E+789
|
||||||
|
C5 = 1234i
|
||||||
|
C6 = "foo\n"
|
||||||
|
C7 = `bar\n`
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
C8 => math.Pi
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
T1 int
|
||||||
|
T2 [10]int
|
||||||
|
T3 []int
|
||||||
|
T4 *int
|
||||||
|
T5 chan int
|
||||||
|
T6a chan<- int
|
||||||
|
T6b chan (<-chan int)
|
||||||
|
T6c chan<- (chan int)
|
||||||
|
T7 <-chan *ast.File
|
||||||
|
T8 struct{}
|
||||||
|
T9 struct {
|
||||||
|
a int
|
||||||
|
b, c float32
|
||||||
|
d []string `go:"tag"`
|
||||||
|
}
|
||||||
|
T10 struct {
|
||||||
|
T8
|
||||||
|
T9
|
||||||
|
_ *T10
|
||||||
|
}
|
||||||
|
T11 map[int]string
|
||||||
|
T12 interface{}
|
||||||
|
T13 interface {
|
||||||
|
m1()
|
||||||
|
m2(int) float32
|
||||||
|
}
|
||||||
|
T14 interface {
|
||||||
|
T12
|
||||||
|
T13
|
||||||
|
m3(x ...struct{}) []T9
|
||||||
|
}
|
||||||
|
T15 func()
|
||||||
|
T16 func(int)
|
||||||
|
T17 func(x int)
|
||||||
|
T18 func() float32
|
||||||
|
T19 func() (x float32)
|
||||||
|
T20 func(...interface{})
|
||||||
|
T21 struct{ next *T21 }
|
||||||
|
T22 struct{ link *T23 }
|
||||||
|
T23 struct{ link *T22 }
|
||||||
|
T24 *T24
|
||||||
|
T25 *T26
|
||||||
|
T26 *T27
|
||||||
|
T27 *T25
|
||||||
|
T28 func(T28) T28
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
T29 => ast.File
|
||||||
|
T30 => build.Context
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
V0 int
|
||||||
|
V1 = -991.0
|
||||||
|
V2 float32 = 1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
V3 => build.Default
|
||||||
|
)
|
||||||
|
|
||||||
|
func F1() {}
|
||||||
|
func F2(x int) {}
|
||||||
|
func F3() int { return 0 }
|
||||||
|
func F4() float32 { return 0 }
|
||||||
|
func F5(a, b, c int, u, v, w struct{ x, y T1 }, more ...interface{}) (p, q, r chan<- T10)
|
||||||
|
|
||||||
|
func (p *T1) M1()
|
||||||
|
|
||||||
|
func F6 => math.Sin
|
||||||
|
func F7 => ast.IsExported
|
||||||
|
func F8 => build.Import
|
Loading…
Reference in New Issue