go/gcimporter15: implement types.Package.SetName for Go 1.5

This makes gcimporter15 build against Go 1.5.

Change-Id: I14e7ff80b28d99f996abc19a0a74b08e5e1bbd75
Reviewed-on: https://go-review.googlesource.com/18771
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
Robert Griesemer 2016-01-20 12:59:31 -08:00
parent 2fb4b8bc95
commit e6d5370703
4 changed files with 73 additions and 1 deletions

View File

@ -390,7 +390,7 @@ func (p *parser) getPkg(id, name string) *types.Package {
// package exists already and we have an expected package name;
// make sure names match or set package name if necessary
if pname := pkg.Name(); pname == "" {
pkg.SetName(name)
setName(pkg, name)
} else if pname != name {
p.errorf("%s package name mismatch: %s (given) vs %s (expected)", pname, name)
}

View File

@ -0,0 +1,31 @@
// Copyright 2016 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.
// +build !go1.6
package gcimporter
import (
"go/types"
"unsafe"
)
func setName(pkg *types.Package, name string) {
(*types_Package)(unsafe.Pointer(pkg)).name = name
}
// The underlying type of types_Package is identical to
// the underlying type of types.Package. We use it with
// package unsafe to set the name field since 1.5 does
// not have the Package.SetName method.
// TestSetName verifies that the layout with respect to
// the name field is correct.
type types_Package struct {
path string
name string
scope *types.Scope
complete bool
imports []*types.Package
fake bool
}

View File

@ -0,0 +1,13 @@
// Copyright 2016 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.
// +build go1.6
package gcimporter
import "go/types"
func setName(pkg *types.Package, name string) {
pkg.SetName(name)
}

View File

@ -0,0 +1,28 @@
// Copyright 2016 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.
// +build go1.5
package gcimporter
import (
"go/types"
"testing"
)
func TestSetName(t *testing.T) {
pkg := types.NewPackage("path", "foo")
scope := pkg.Scope()
// verify setName
setName(pkg, "bar")
if name := pkg.Name(); name != "bar" {
t.Fatalf(`got package name %q; want "bar"`, name)
}
// verify no other fields are changed
if pkg.Path() != "path" || pkg.Scope() != scope || pkg.Complete() || pkg.Imports() != nil {
t.Fatalf("setName changed other fields")
}
}