Skip to content

Commit b2560d1

Browse files
committed
go/gcimporter15: backport compiler exporter fix for golang/go#15514
Code from https://go-review.googlesource.com/#/c/27639/ . Remain backward-compatible with possibly installed packages that remain in Go1.6 format. Change-Id: If424e7a881c81bbfcacf38c0946542793c406abd Reviewed-on: https://go-review.googlesource.com/27640 Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 2bbdb45 commit b2560d1

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

go/gcimporter15/bexport.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,7 @@ func (p *exporter) method(m *types.Func) {
455455
p.paramList(sig.Results(), false)
456456
}
457457

458-
// fieldName is like qualifiedName but it doesn't record the package
459-
// for blank (_) or exported names.
458+
// fieldName is like qualifiedName but it doesn't record the package for exported names.
460459
func (p *exporter) fieldName(f *types.Var) {
461460
name := f.Name()
462461

@@ -468,12 +467,12 @@ func (p *exporter) fieldName(f *types.Var) {
468467
base = ptr.Elem()
469468
}
470469
if named, ok := base.(*types.Named); ok && !named.Obj().Exported() {
471-
name = "?"
470+
// anonymous field with unexported base type name
471+
name = "?" // unexported name to force export of package
472472
}
473473
}
474-
475474
p.string(name)
476-
if name == "?" || name != "_" && !f.Exported() {
475+
if !f.Exported() {
477476
p.pkg(f.Pkg(), false)
478477
}
479478
}

go/gcimporter15/bimport.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type importer struct {
2626
data []byte
2727
path string
2828
buf []byte // for reading strings
29+
version int
2930

3031
// object lists
3132
strList []string // in order of appearance
@@ -76,12 +77,14 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
7677
if s := p.string(); s != go17version {
7778
return p.read, nil, fmt.Errorf("importer: unknown export data format: %s (imported package compiled with old compiler?)", s)
7879
}
80+
p.version = 0
7981
} else {
8082
// Go1.8 extensible encoding
8183
const exportVersion = "version 1"
8284
if s := p.rawStringln(b); s != exportVersion {
8385
return p.read, nil, fmt.Errorf("importer: unknown export data format: %s (imported package compiled with old compiler?)", s)
8486
}
87+
p.version = 1
8588
p.debugFormat = p.rawStringln(p.rawByte()) == "debug"
8689
p.trackAllTypes = p.int() != 0
8790
p.posInfoFormat = p.int() != 0
@@ -532,19 +535,20 @@ func (p *importer) method(parent *types.Package) *types.Func {
532535
}
533536

534537
func (p *importer) fieldName(parent *types.Package) (*types.Package, string) {
538+
name := p.string()
535539
pkg := parent
536540
if pkg == nil {
537541
// use the imported package instead
538542
pkg = p.pkgList[0]
539543
}
540-
name := p.string()
541-
if name == "" {
542-
return pkg, "" // anonymous
544+
if p.version < 1 && name == "_" {
545+
// versions < 1 don't export a package for _ fields
546+
// TODO: remove once versions are not supported anymore
547+
return pkg, name
543548
}
544-
if name == "?" || name != "_" && !exported(name) {
545-
// explicitly qualified field
549+
if name != "" && !exported(name) {
546550
if name == "?" {
547-
name = "" // anonymous
551+
name = ""
548552
}
549553
pkg = p.pkg()
550554
}

0 commit comments

Comments
 (0)