Skip to content

Commit

Permalink
internal/analysisinternal: TypeExpr: don't forget Variadic
Browse files Browse the repository at this point in the history
The syntax generated for type func(...T) was func([]T),
causing extract operations to misbehave.

Fixes golang/go#63287

Change-Id: I37978a13b2b51f0802ea694b7711a7d6444384fb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/555458
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
adonovan committed Jan 12, 2024
1 parent 54cf5bc commit dbc9d3e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
This test exercises extract on a variadic function.
It is a regression test for bug #63287 in which
the final paramater's "..." would go missing.

-- go.mod --
module testdata
go 1.18

-- a/a.go --
package a

//@codeactionedit(block, "refactor.extract", out, "Extract function")

func _() {
var logf func(string, ...any)
{ println(logf) } //@loc(block, re`{.*}`)
}

-- @out/a/a.go --
@@ -7 +7 @@
- { println(logf) } //@loc(block, re`{.*}`)
+ { newFunction(logf) } //@loc(block, re`{.*}`)
@@ -10 +10,4 @@
+func newFunction(logf func( string, ...any)) {
+ println(logf)
+}
+
-- end --
4 changes: 4 additions & 0 deletions internal/analysisinternal/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func TypeExpr(f *ast.File, pkg *types.Package, typ types.Type) ast.Expr {
},
})
}
if t.Variadic() {
last := params[len(params)-1]
last.Type = &ast.Ellipsis{Elt: last.Type.(*ast.ArrayType).Elt}
}
var returns []*ast.Field
for i := 0; i < t.Results().Len(); i++ {
r := TypeExpr(f, pkg, t.Results().At(i).Type())
Expand Down

0 comments on commit dbc9d3e

Please sign in to comment.