Skip to content

Commit 198cae3

Browse files
committed
go/ssa: split pkg() into different cases for *Package and *types.Package
Splits (*Function).pkg() into different cases for returning a *Package and the *types.Package. Updates golang/go#48525 Change-Id: I1c59679c5acd898cf2009a2e3a0ea96b62f82a06 Reviewed-on: https://go-review.googlesource.com/c/tools/+/391334 Reviewed-by: Zvonimir Pavlinovic <[email protected]> Run-TryBot: Tim King <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Trust: Tim King <[email protected]>
1 parent ee31f70 commit 198cae3

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

go/ssa/func.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func (f *Function) RelString(from *types.Package) string {
414414

415415
// Package-level function?
416416
// Prefix with package name for cross-package references only.
417-
if p := f.pkg(); p != nil && p != from {
417+
if p := f.relPkg(); p != nil && p != from {
418418
return fmt.Sprintf("%s.%s", p.Path(), f.name)
419419
}
420420

@@ -442,9 +442,26 @@ func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *ty
442442
types.WriteSignature(buf, sig, types.RelativeTo(from))
443443
}
444444

445-
func (f *Function) pkg() *types.Package {
446-
if f.Pkg != nil {
447-
return f.Pkg.Pkg
445+
// declaredPackage returns the package fn is declared in or nil if the
446+
// function is not declared in a package.
447+
func (fn *Function) declaredPackage() *Package {
448+
switch {
449+
case fn.Pkg != nil:
450+
return fn.Pkg // non-generic function
451+
// generics:
452+
// case fn.Origin != nil:
453+
// return fn.Origin.pkg // instance of a named generic function
454+
case fn.parent != nil:
455+
return fn.parent.declaredPackage() // instance of an anonymous [generic] function
456+
default:
457+
return nil // function is not declared in a package, e.g. a wrapper.
458+
}
459+
}
460+
461+
// relPkg returns types.Package fn is printed in relationship to.
462+
func (fn *Function) relPkg() *types.Package {
463+
if p := fn.declaredPackage(); p != nil {
464+
return p.Pkg
448465
}
449466
return nil
450467
}
@@ -479,7 +496,7 @@ func WriteFunction(buf *bytes.Buffer, f *Function) {
479496
fmt.Fprintf(buf, "# Recover: %s\n", f.Recover)
480497
}
481498

482-
from := f.pkg()
499+
from := f.relPkg()
483500

484501
if f.FreeVars != nil {
485502
buf.WriteString("# Free variables:\n")

go/ssa/print.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
func relName(v Value, i Instruction) string {
2929
var from *types.Package
3030
if i != nil {
31-
from = i.Parent().pkg()
31+
from = i.Parent().relPkg()
3232
}
3333
switch v := v.(type) {
3434
case Member: // *Function or *Global
@@ -66,12 +66,12 @@ func relString(m Member, from *types.Package) string {
6666
// It never appears in disassembly, which uses Value.Name().
6767

6868
func (v *Parameter) String() string {
69-
from := v.Parent().pkg()
69+
from := v.Parent().relPkg()
7070
return fmt.Sprintf("parameter %s : %s", v.Name(), relType(v.Type(), from))
7171
}
7272

7373
func (v *FreeVar) String() string {
74-
from := v.Parent().pkg()
74+
from := v.Parent().relPkg()
7575
return fmt.Sprintf("freevar %s : %s", v.Name(), relType(v.Type(), from))
7676
}
7777

@@ -86,7 +86,7 @@ func (v *Alloc) String() string {
8686
if v.Heap {
8787
op = "new"
8888
}
89-
from := v.Parent().pkg()
89+
from := v.Parent().relPkg()
9090
return fmt.Sprintf("%s %s (%s)", op, relType(deref(v.Type()), from), v.Comment)
9191
}
9292

@@ -160,7 +160,7 @@ func (v *UnOp) String() string {
160160
}
161161

162162
func printConv(prefix string, v, x Value) string {
163-
from := v.Parent().pkg()
163+
from := v.Parent().relPkg()
164164
return fmt.Sprintf("%s %s <- %s (%s)",
165165
prefix,
166166
relType(v.Type(), from),
@@ -191,7 +191,7 @@ func (v *MakeClosure) String() string {
191191
}
192192

193193
func (v *MakeSlice) String() string {
194-
from := v.Parent().pkg()
194+
from := v.Parent().relPkg()
195195
return fmt.Sprintf("make %s %s %s",
196196
relType(v.Type(), from),
197197
relName(v.Len, v),
@@ -223,12 +223,12 @@ func (v *MakeMap) String() string {
223223
if v.Reserve != nil {
224224
res = relName(v.Reserve, v)
225225
}
226-
from := v.Parent().pkg()
226+
from := v.Parent().relPkg()
227227
return fmt.Sprintf("make %s %s", relType(v.Type(), from), res)
228228
}
229229

230230
func (v *MakeChan) String() string {
231-
from := v.Parent().pkg()
231+
from := v.Parent().relPkg()
232232
return fmt.Sprintf("make %s %s", relType(v.Type(), from), relName(v.Size, v))
233233
}
234234

@@ -273,7 +273,7 @@ func (v *Next) String() string {
273273
}
274274

275275
func (v *TypeAssert) String() string {
276-
from := v.Parent().pkg()
276+
from := v.Parent().relPkg()
277277
return fmt.Sprintf("typeassert%s %s.(%s)", commaOk(v.CommaOk), relName(v.X, v), relType(v.AssertedType, from))
278278
}
279279

go/ssa/sanity.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ func (s *sanity) checkFunction(fn *Function) bool {
409409
s.errorf("nil Prog")
410410
}
411411

412-
_ = fn.String() // must not crash
413-
_ = fn.RelString(fn.pkg()) // must not crash
412+
_ = fn.String() // must not crash
413+
_ = fn.RelString(fn.relPkg()) // must not crash
414414

415415
// All functions have a package, except delegates (which are
416416
// shared across packages, or duplicated as weak symbols in a

0 commit comments

Comments
 (0)