Skip to content

Commit

Permalink
go/types: add Func.Signature method
Browse files Browse the repository at this point in the history
Unfortunately we can't enforce the repr invariant
that Func.typ != nil without thinking about the
object color invariants. For now, return a trivial
Signature if typ == nil, which should never happen
in bug-free client code.

Fixes #65772

Change-Id: I7f89c6d8fdc8dcd4b8880572e54bb0ed9b6136eb
Reviewed-on: https://go-review.googlesource.com/c/go/+/565375
Commit-Queue: Robert Griesemer <[email protected]>
Reviewed-by: Robert Findley <[email protected]>
Reviewed-by: Robert Griesemer <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
adonovan committed Apr 18, 2024
1 parent dfc86e9 commit 0106462
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 12 deletions.
1 change: 1 addition & 0 deletions api/next/65772.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg go/types, method (*Func) Signature() *Signature #65772
4 changes: 4 additions & 0 deletions doc/next/6-stdlib/99-minor/go/types/65772.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The [`Func`](/go/types#Func) type, which represents a function or
method symbol, now has a [`Signature`](/go/types#Func.Signature)
method that returns the function's type, which is always a
`Signature`.
22 changes: 21 additions & 1 deletion src/cmd/compile/internal/types2/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,34 @@ type Func struct {
// NewFunc returns a new function with the given signature, representing
// the function's type.
func NewFunc(pos syntax.Pos, pkg *Package, name string, sig *Signature) *Func {
// don't store a (typed) nil signature
var typ Type
if sig != nil {
typ = sig
} else {
// Don't store a (typed) nil *Signature.
// We can't simply replace it with new(Signature) either,
// as this would violate object.{Type,color} invariants.
// TODO(adonovan): propose to disallow NewFunc with nil *Signature.
}
return &Func{object{nil, pos, pkg, name, typ, 0, colorFor(typ), nopos}, false, nil}
}

// Signature returns the signature (type) of the function or method.
func (obj *Func) Signature() *Signature {
if obj.typ != nil {
return obj.typ.(*Signature) // normal case
}
// No signature: Signature was called either:
// - within go/types, before a FuncDecl's initially
// nil Func.Type was lazily populated, indicating
// a types bug; or
// - by a client after NewFunc(..., nil),
// which is arguably a client bug, but we need a
// proposal to tighten NewFunc's precondition.
// For now, return a trivial signature.
return new(Signature)
}

// FullName returns the package- or receiver-type-qualified name of
// function or method obj.
func (obj *Func) FullName() string {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/compile/internal/types2/subst.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func (subst *subster) termlist(in []*Term) (out []*Term, copied bool) {
func replaceRecvType(in []*Func, old, new Type) (out []*Func, copied bool) {
out = in
for i, method := range in {
sig := method.Type().(*Signature)
sig := method.Signature()
if sig.recv != nil && sig.recv.Type() == old {
if !copied {
// Allocate a new methods slice before mutating for the first time.
Expand Down
8 changes: 4 additions & 4 deletions src/go/types/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,7 @@ func f(x int) { y := x; print(y) }
wantParent = false
}
case *Func:
if obj.Type().(*Signature).Recv() != nil { // method
if obj.Signature().Recv() != nil { // method
wantParent = false
}
}
Expand Down Expand Up @@ -2615,9 +2615,9 @@ func fn() {

// Methods and method fields
{"concreteMethod", lookup("t").(*Named).Method(0)},
{"recv", lookup("t").(*Named).Method(0).Type().(*Signature).Recv()},
{"mParam", lookup("t").(*Named).Method(0).Type().(*Signature).Params().At(0)},
{"mResult", lookup("t").(*Named).Method(0).Type().(*Signature).Results().At(0)},
{"recv", lookup("t").(*Named).Method(0).Signature().Recv()},
{"mParam", lookup("t").(*Named).Method(0).Signature().Params().At(0)},
{"mResult", lookup("t").(*Named).Method(0).Signature().Results().At(0)},

// Interface methods
{"interfaceMethod", lookup("i").Underlying().(*Interface).Method(0)},
Expand Down
4 changes: 2 additions & 2 deletions src/go/types/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ type T struct{} // receiver type after method declaration
}

m := f.Decls[0].(*ast.FuncDecl)
res1 := defs[m.Name].(*Func).Type().(*Signature).Results().At(0)
res1 := defs[m.Name].(*Func).Signature().Results().At(0)
res2 := defs[m.Type.Results.List[0].Names[0]].(*Var)

if res1 != res2 {
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestIssue28005(t *testing.T) {
// must match the method's name per the choice in the source file.
for i := 0; i < iface.NumMethods(); i++ {
m := iface.Method(i)
recvName := m.Type().(*Signature).Recv().Type().(*Named).Obj().Name()
recvName := m.Signature().Recv().Type().(*Named).Obj().Name()
if recvName != m.Name() {
t.Errorf("perm %v: got recv %s; want %s", perm, recvName, m.Name())
}
Expand Down
22 changes: 21 additions & 1 deletion src/go/types/object.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/go/types/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,8 @@ func (check *Checker) collectObjects() {
check.declarePkgObj(d.spec.Name, obj, &declInfo{file: fileScope, tdecl: d.spec})
case funcDecl:
name := d.decl.Name.Name
obj := NewFunc(d.decl.Name.Pos(), pkg, name, nil)
hasTParamError := false // avoid duplicate type parameter errors
obj := NewFunc(d.decl.Name.Pos(), pkg, name, nil) // signature set later
hasTParamError := false // avoid duplicate type parameter errors
if d.decl.Recv.NumFields() == 0 {
// regular function
if d.decl.Recv != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/go/types/subst.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0106462

Please sign in to comment.