Skip to content

Commit

Permalink
go/ssa: Origin is only available after building
Browse files Browse the repository at this point in the history
Origin of an instantiation is only available after the origin
function is done being built. Build the origin on demand if
not yet built.

Fixes golang/go#59427

Change-Id: I930ae496ce0d814890ea2d947100cdfe6ede059f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/491755
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Tim King <taking@google.com>
  • Loading branch information
timothy-king committed May 5, 2023
1 parent 6d1dd12 commit 479f5c6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions go/ssa/ssa.go
Original file line number Diff line number Diff line change
Expand Up @@ -1535,12 +1535,25 @@ func (fn *Function) TypeParams() *typeparams.TypeParamList {
// from fn.Origin().
func (fn *Function) TypeArgs() []types.Type { return fn.typeargs }

// Origin is the function fn is an instantiation of. Returns nil if fn is not
// an instantiation.
// Origin returns the generic function from which fn was instantiated,
// or nil if fn is not an instantiation.
func (fn *Function) Origin() *Function {
if fn.parent != nil && len(fn.typeargs) > 0 {
// Nested functions are BUILT at a different time than there instances.
return fn.parent.Origin().AnonFuncs[fn.anonIdx]
// Nested functions are BUILT at a different time than their instances.
// Build declared package if not yet BUILT. This is not an expected use
// case, but is simple and robust.
fn.declaredPackage().Build()
}
return origin(fn)
}

// origin is the function that fn is an instantiation of. Returns nil if fn is
// not an instantiation.
//
// Precondition: fn and the origin function are done building.
func origin(fn *Function) *Function {
if fn.parent != nil && len(fn.typeargs) > 0 {
return origin(fn.parent).AnonFuncs[fn.anonIdx]
}
return fn.topLevelOrigin
}
Expand Down

0 comments on commit 479f5c6

Please sign in to comment.