Skip to content

Commit

Permalink
fix inline directive for a function only called from other packages
Browse files Browse the repository at this point in the history
When a top-level function is called from another packages, the
selector expression `pkg.FuncName` is not handled correctly.

This commit fixes this case by checking the Ident in the selector
expression.

Fixes #17
  • Loading branch information
RaduBerinde committed Dec 22, 2024
1 parent 3141cbd commit daa3520
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
7 changes: 4 additions & 3 deletions gcassert.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,11 @@ func (v *inlinedDeclVisitor) Visit(node ast.Node) (w ast.Visitor) {
obj = v.p.TypesInfo.Uses[n]
case *ast.SelectorExpr:
sel := v.p.TypesInfo.Selections[n]
if sel == nil {
break
if sel != nil {
obj = sel.Obj()
} else {
obj = v.p.TypesInfo.Uses[n.Sel]
}
obj = sel.Obj()
}
if _, ok := v.mustInlineFuncs[obj]; ok {
lineInfo := v.directiveMap[lineNumber]
Expand Down
9 changes: 5 additions & 4 deletions gcassert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ testdata/inline.go:51: sum += notInlinable(i): call was not inlined
testdata/inline.go:55: sum += 1: call was not inlined
testdata/inline.go:58: test(0).neverInlinedMethod(10): call was not inlined
testdata/inline.go:60: otherpkg.A{}.NeverInlined(sum): call was not inlined
testdata/inline.go:62: otherpkg.NeverInlinedFunc(sum): call was not inlined
testdata/issue5.go:4: Gen().Layout(): call was not inlined
`

testCases := []struct{
name string
pkgs []string
cwd string
name string
pkgs []string
cwd string
expected string
}{
{
Expand All @@ -130,7 +131,7 @@ testdata/issue5.go:4: Gen().Layout(): call was not inlined
"./testdata",
"./testdata/otherpkg",
},
cwd: cwd,
cwd: cwd,
expected: expectedOutput,
},
}
Expand Down
2 changes: 2 additions & 0 deletions testdata/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ func caller() {
sum += test(0).neverInlinedMethod(10)

otherpkg.A{}.NeverInlined(sum)

otherpkg.NeverInlinedFunc(sum)
}
7 changes: 7 additions & 0 deletions testdata/otherpkg/foo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ func (a A) NeverInlined(n int) {
fmt.Println(i)
}
}

//gcassert:inline
func NeverInlinedFunc(n int) {
for i := 0; i < n; i++ {
fmt.Println(i)
}
}

0 comments on commit daa3520

Please sign in to comment.