Skip to content

Commit

Permalink
Use types.Func.Origin instead of typeparams.OriginMethod
Browse files Browse the repository at this point in the history
The Origin method was added in Go 1.19.

Using it also avoids a bug in typeparams.OriginMethod, where methods
named "_" lead to panics.
  • Loading branch information
dominikh committed Jul 3, 2023
1 parent 760ebea commit 65cc494
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 10 deletions.
4 changes: 1 addition & 3 deletions go/ir/methods.go
Expand Up @@ -11,8 +11,6 @@ import (
"go/types"

"honnef.co/go/tools/analysis/lint"

"golang.org/x/exp/typeparams"
)

// MethodValue returns the Function implementing method sel, building
Expand Down Expand Up @@ -118,7 +116,7 @@ func (prog *Program) RuntimeTypes() []types.Type {
// declaredFunc returns the concrete function/method denoted by obj.
// Panic ensues if there is none.
func (prog *Program) declaredFunc(obj *types.Func) *Function {
if origin := typeparams.OriginMethod(obj); origin != obj {
if origin := obj.Origin(); origin != obj {
// Calling method on instantiated type, create a wrapper that calls the generic type's method
base := prog.packageLevelValue(origin)
return makeInstance(prog, base.(*Function), obj.Type().(*types.Signature), nil)
Expand Down
4 changes: 1 addition & 3 deletions go/ir/source.go
Expand Up @@ -14,8 +14,6 @@ import (
"go/ast"
"go/token"
"go/types"

"golang.org/x/exp/typeparams"
)

// EnclosingFunction returns the function that contains the syntax
Expand Down Expand Up @@ -170,7 +168,7 @@ func (prog *Program) packageLevelValue(obj types.Object) Value {
// TODO(adonovan): check the invariant that obj.Type() matches the
// result's Signature, both in the params/results and in the receiver.
func (prog *Program) FuncValue(obj *types.Func) *Function {
obj = typeparams.OriginMethod(obj)
obj = obj.Origin()
fn, _ := prog.packageLevelValue(obj).(*Function)
return fn
}
Expand Down
2 changes: 1 addition & 1 deletion staticcheck/lint.go
Expand Up @@ -3182,7 +3182,7 @@ func CheckDeprecated(pass *analysis.Pass) (interface{}, error) {

obj := pass.TypesInfo.ObjectOf(sel.Sel)
if obj_, ok := obj.(*types.Func); ok {
obj = typeparams.OriginMethod(obj_)
obj = obj_.Origin()
}
if obj.Pkg() == nil {
return true
Expand Down
4 changes: 1 addition & 3 deletions unused/unused.go
Expand Up @@ -17,7 +17,6 @@ import (
"honnef.co/go/tools/go/ast/astutil"
"honnef.co/go/tools/go/types/typeutil"

"golang.org/x/exp/typeparams"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/types/objectpath"
)
Expand Down Expand Up @@ -1151,8 +1150,7 @@ func (g *graph) decl(decl ast.Decl, by types.Object) {
}

case *ast.FuncDecl:
// XXX calling OriginMethod is unnecessary if we use types.Func.Origin
obj := typeparams.OriginMethod(g.info.ObjectOf(decl.Name).(*types.Func))
obj := g.info.ObjectOf(decl.Name).(*types.Func).Origin()
g.see(obj, nil)

if token.IsExported(decl.Name.Name) && g.opts.ExportedIsUsed {
Expand Down

0 comments on commit 65cc494

Please sign in to comment.