Skip to content

Commit 394cd0a

Browse files
committed
skip the receiver arg only when a call has one
A call via a bound method value has no receiver arg, so skipping the first arg misaligned the args with the params. This also explains the out of range index in alwaysReceivedConst, so drop that check.
1 parent 19648af commit 394cd0a

2 files changed

Lines changed: 18 additions & 24 deletions

File tree

‎check/check.go‎

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -777,36 +777,28 @@ func (c *Checker) alwaysReceivedConst(callSites []ssa.CallInstruction, par *ssa.
777777
// times, hence a potential false positive.
778778
return ""
779779
}
780-
if ast.IsExported(par.Parent().Name()) {
780+
fn := par.Parent()
781+
if ast.IsExported(fn.Name()) {
781782
// we might not have all call sites for an exported func
782783
return ""
783784
}
784-
var seen *ssa.Const
785-
origPos := pos
786-
if par.Parent().Signature.Recv() != nil {
787-
// go/ast's CallExpr.Args does not include the receiver, but
788-
// go/ssa's equivalent does.
789-
origPos--
785+
if fn.Signature.Recv() != nil {
786+
pos-- // paramArgs and go/ast's CallExpr.Args exclude the receiver
790787
}
788+
var seen *ssa.Const
791789
seenOrig := ""
792790
for _, site := range callSites {
793791
call := site.Common()
794-
if pos >= len(call.Args) {
795-
// TODO: investigate? Weird crash in
796-
// internal/x/net/http2/hpack/hpack_test.go, where we
797-
// roughly do: "at := d.mustAt; at(3)".
798-
return ""
799-
}
800-
cnst := constValue(call.Args[pos])
792+
cnst := constValue(paramArgs(call, fn)[pos])
801793
if cnst == nil {
802794
return "" // not a constant
803795
}
804796
origArg := ""
805797
origCall := c.callByPos[call.Pos()]
806-
if origPos >= len(origCall.Args) {
798+
if pos >= len(origCall.Args) {
807799
// variadic parameter that wasn't given
808800
} else {
809-
origArg = nodeStr(origCall.Args[origPos])
801+
origArg = nodeStr(origCall.Args[pos])
810802
}
811803
if seen == nil {
812804
seen = cnst // first constant
@@ -1057,6 +1049,15 @@ func (c *Checker) multipleImpls(pkg *packages.Package, fn *ssa.Function) bool {
10571049
return count[name] > 1
10581050
}
10591051

1052+
// paramArgs returns the arguments of a call to fn, excluding any receiver,
1053+
// which a call via a bound method value like "f := x.Method" does not have.
1054+
func paramArgs(call *ssa.CallCommon, fn *ssa.Function) []ssa.Value {
1055+
if fn.Signature.Recv() != nil && len(call.Args) > fn.Signature.Params().Len() {
1056+
return call.Args[1:]
1057+
}
1058+
return call.Args
1059+
}
1060+
10601061
// receivesExtractedArgs returns the statically called function, if its multiple
10611062
// arguments were all received via another function call. That is, if a call to
10621063
// function "foo" was of the form "foo(bar())". This often means that the
@@ -1071,12 +1072,7 @@ func receivesExtractedArgs(freeVars map[ssa.Value]*ssa.Function, call ssa.CallIn
10711072
// there aren't multiple parameters
10721073
return nil
10731074
}
1074-
args := comm.Args
1075-
if callee.Signature.Recv() != nil {
1076-
// skip the receiver argument
1077-
args = args[1:]
1078-
}
1079-
if c := callExtract(call, args); c != nil {
1075+
if c := callExtract(call, paramArgs(comm, callee)); c != nil {
10801076
return callee
10811077
}
10821078
return nil

‎testdata/script/extractparams.txtar‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ foo.go:25:38: (FooType).methodResultAsParam - a is unused
99
foo.go:32:29: funcResultsAsParams - b is unused
1010
foo.go:37:31: funcResultNotAsParams - b is unused
1111
foo.go:58:26: returnResultsOwn - result 0 (testdata.tld/foo.FooType) is never used
12-
foo.go:76:48: (FooType).methodValueResultsAsParams - b is unused
1312
-- foo.go --
1413
package foo
1514

@@ -85,7 +84,6 @@ func ReturnResultsCalls() {
8584
_, Sink = returnResultsDirectly()
8685
}
8786

88-
// TODO: reported, as the args of a call via a method value are misread.
8987
func (f FooType) methodValueResultsAsParams(a, b FooType) FooType {
9088
DoWork()
9189
return a + 1

0 commit comments

Comments
 (0)