Skip to content

Commit 1c6f3cc

Browse files
committed
go/analysis/passes/ctrlfow: rely on typeutil.StaticCallee
typeutil.StaticCallee now supports generic functions, so that can be used instead of ctrlflow computing static callee for generics on its own. Change-Id: I38aaf2d91bf1b7f4d0bf5ba6a6684271c1569464 Reviewed-on: https://go-review.googlesource.com/c/tools/+/361406 Run-TryBot: Zvonimir Pavlinovic <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Trust: Zvonimir Pavlinovic <[email protected]>
1 parent 1ae27d3 commit 1c6f3cc

File tree

1 file changed

+1
-47
lines changed

1 file changed

+1
-47
lines changed

go/analysis/passes/ctrlflow/ctrlflow.go

+1-47
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ import (
1616

1717
"golang.org/x/tools/go/analysis"
1818
"golang.org/x/tools/go/analysis/passes/inspect"
19-
"golang.org/x/tools/go/ast/astutil"
2019
"golang.org/x/tools/go/ast/inspector"
2120
"golang.org/x/tools/go/cfg"
2221
"golang.org/x/tools/go/types/typeutil"
23-
"golang.org/x/tools/internal/typeparams"
2422
)
2523

2624
var Analyzer = &analysis.Analyzer{
@@ -194,7 +192,7 @@ func (c *CFGs) callMayReturn(call *ast.CallExpr) (r bool) {
194192
// return depending on the parameter type, but in some
195193
// cases the answer is definite. We let ctrlflow figure
196194
// that out.
197-
fn := staticCallee(c.pass.TypesInfo, call)
195+
fn := typeutil.StaticCallee(c.pass.TypesInfo, call)
198196
if fn == nil {
199197
return true // callee not statically known; be conservative
200198
}
@@ -210,50 +208,6 @@ func (c *CFGs) callMayReturn(call *ast.CallExpr) (r bool) {
210208
return !c.pass.ImportObjectFact(fn, new(noReturn))
211209
}
212210

213-
// staticCallee returns static function, if any, called by call.
214-
// Effectivelly reduces to typeutil.StaticCallee. In addition,
215-
// returns static function parameterized by a type, if any.
216-
//
217-
// TODO(zpavlinovic): can this be replaced by typeutil.StaticCallee
218-
// in the future?
219-
func staticCallee(info *types.Info, call *ast.CallExpr) *types.Func {
220-
if fn := typeutil.StaticCallee(info, call); fn != nil {
221-
return fn
222-
}
223-
return staticTypeParamCallee(info, call)
224-
}
225-
226-
// staticTypeParamCallee returns the static function in call, if any,
227-
// expected to be parameterized by a type.
228-
func staticTypeParamCallee(info *types.Info, call *ast.CallExpr) *types.Func {
229-
ix := typeparams.GetIndexExprData(astutil.Unparen(call.Fun))
230-
if ix == nil {
231-
return nil
232-
}
233-
234-
var obj types.Object
235-
switch fun := ix.X.(type) {
236-
case *ast.Ident:
237-
obj = info.Uses[fun] // type, var, builtin, or declared func
238-
case *ast.SelectorExpr:
239-
if sel, ok := info.Selections[fun]; ok {
240-
obj = sel.Obj() // method or field
241-
} else {
242-
obj = info.Uses[fun.Sel] // qualified identifier?
243-
}
244-
}
245-
246-
if f, ok := obj.(*types.Func); ok && !interfaceMethod(f) {
247-
return f
248-
}
249-
return nil
250-
}
251-
252-
func interfaceMethod(f *types.Func) bool {
253-
recv := f.Type().(*types.Signature).Recv()
254-
return recv != nil && types.IsInterface(recv.Type())
255-
}
256-
257211
var panicBuiltin = types.Universe.Lookup("panic").(*types.Builtin)
258212

259213
func hasReachableReturn(g *cfg.CFG) bool {

0 commit comments

Comments
 (0)