diff --git a/go/gcimporter15/bexport.go b/go/gcimporter15/bexport.go index fc569e4f..39f6ce39 100644 --- a/go/gcimporter15/bexport.go +++ b/go/gcimporter15/bexport.go @@ -173,6 +173,17 @@ func (p *exporter) pkg(pkg *types.Package, emptypath bool) { } 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) { case *types.Const: p.tag(constTag) diff --git a/go/gcimporter15/gcimporter17_test.go b/go/gcimporter15/gcimporter17_test.go index cca237c8..d0394db2 100644 --- a/go/gcimporter15/gcimporter17_test.go +++ b/go/gcimporter15/gcimporter17_test.go @@ -35,14 +35,16 @@ func TestImportTestdataNewExport(t *testing.T) { return } - if outFn := compileNewExport(t, "testdata", "exports.go"); outFn != "" { + if outFn := compileNewExport(t, "testdata", testfile); 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 - // explicitly imported by exports.go, plus all packages - // referenced indirectly via exported objects in exports.go. + // explicitly imported by testfile, plus all packages + // referenced indirectly via exported objects in testfile. want := `[package ast ("go/ast") package token ("go/token")]` got := fmt.Sprint(pkg.Imports()) if got != want { diff --git a/go/gcimporter15/gcimporter_test.go b/go/gcimporter15/gcimporter_test.go index 0bf6cbde..7241215a 100644 --- a/go/gcimporter15/gcimporter_test.go +++ b/go/gcimporter15/gcimporter_test.go @@ -137,14 +137,16 @@ func TestImportTestdata(t *testing.T) { return } - if outFn := compile(t, "testdata", "exports.go"); outFn != "" { + if outFn := compile(t, "testdata", testfile); 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 - // explicitly imported by exports.go, plus all packages - // referenced indirectly via exported objects in exports.go. + // explicitly imported by testfile, plus all packages + // referenced indirectly via exported objects in testfile. // With the textual export format, the list may also include // additional packages that are not strictly required for // import processing alone (they are exported to err "on diff --git a/go/gcimporter15/newalias16.go b/go/gcimporter15/newalias16.go index 39e1345f..d36f8589 100644 --- a/go/gcimporter15/newalias16.go +++ b/go/gcimporter15/newalias16.go @@ -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) panic("unreachable") } + +func original(obj types.Object) types.Object { + return obj // don't know about aliases +} + +const testfile = "exports17.go" diff --git a/go/gcimporter15/newalias18.go b/go/gcimporter15/newalias18.go index 572b88be..446a0388 100644 --- a/go/gcimporter15/newalias18.go +++ b/go/gcimporter15/newalias18.go @@ -9,3 +9,13 @@ package gcimporter import "go/types" 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" diff --git a/go/gcimporter15/testdata/exports.go b/go/gcimporter15/testdata/exports17.go similarity index 100% rename from go/gcimporter15/testdata/exports.go rename to go/gcimporter15/testdata/exports17.go diff --git a/go/gcimporter15/testdata/exports18.go b/go/gcimporter15/testdata/exports18.go new file mode 100644 index 00000000..0033f302 --- /dev/null +++ b/go/gcimporter15/testdata/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