Skip to content

Commit d51d23f

Browse files
Go: Fix RPC serialization for varargs (#7271)
* Fix RPC ser/deser for varargs * Adding VariableDeclarations.Varargs field
1 parent fbaf874 commit d51d23f

6 files changed

Lines changed: 49 additions & 4 deletions

File tree

rewrite-go/rewrite/pkg/parser/go_parser.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,15 @@ func (ctx *parseContext) mapFieldListAsParams(fl *ast.FieldList) tree.Container[
669669
if len(field.Names) == 0 {
670670
// Unnamed parameter: just a type expression (e.g., `int` in `func(int)`)
671671
typeExpr := ctx.mapTypeExpr(field.Type)
672+
var varargs *tree.Space
673+
if u, ok := typeExpr.(*tree.Unary); ok && u.Operator.Element == tree.Spread {
674+
varargs = &u.Prefix
675+
typeExpr = u.Operand
676+
}
672677
vd := &tree.VariableDeclarations{
673678
ID: uuid.New(),
674679
TypeExpr: typeExpr,
680+
Varargs: varargs,
675681
Variables: []tree.RightPadded[*tree.VariableDeclarator]{
676682
{Element: &tree.VariableDeclarator{ID: uuid.New(), Name: &tree.Identifier{ID: uuid.New()}}},
677683
},
@@ -706,9 +712,15 @@ func (ctx *parseContext) mapFieldListAsParams(fl *ast.FieldList) tree.Container[
706712
}
707713

708714
typeExpr := ctx.mapTypeExpr(field.Type)
715+
var varargs *tree.Space
716+
if u, ok := typeExpr.(*tree.Unary); ok && u.Operator.Element == tree.Spread {
717+
varargs = &u.Prefix
718+
typeExpr = u.Operand
719+
}
709720
vd := &tree.VariableDeclarations{
710721
ID: uuid.New(),
711722
TypeExpr: typeExpr,
723+
Varargs: varargs,
712724
Variables: vars,
713725
}
714726

rewrite-go/rewrite/pkg/printer/go_printer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ func (p *GoPrinter) VisitVariableDeclarations(vd *tree.VariableDeclarations, par
323323
out.Append(",")
324324
}
325325
}
326-
// Then type expression
326+
// Then varargs + type expression
327+
if vd.Varargs != nil {
328+
p.visitSpace(*vd.Varargs, out)
329+
out.Append("...")
330+
}
327331
if vd.TypeExpr != nil {
328332
p.Visit(vd.TypeExpr, out)
329333
}

rewrite-go/rewrite/pkg/rpc/java_receiver.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,15 @@ func (r *JavaReceiver) receiveVariableDeclarations(vd *tree.VariableDeclarations
356356
vd.TypeExpr = result.(tree.Expression)
357357
}
358358
// varargs
359-
q.Receive(nil, nil)
359+
var currentVarargs any
360+
if vd.Varargs != nil {
361+
currentVarargs = *vd.Varargs
362+
}
363+
varargsResult := q.Receive(currentVarargs, func(v any) any { return receiveSpace(v.(tree.Space), q) })
364+
if varargsResult != nil {
365+
sp := varargsResult.(tree.Space)
366+
vd.Varargs = &sp
367+
}
360368
// variables
361369
beforeVars := make([]any, len(vd.Variables))
362370
for i, v := range vd.Variables {

rewrite-go/rewrite/pkg/rpc/java_sender.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,14 @@ func (s *JavaSender) sendVariableDeclarations(vd *tree.VariableDeclarations, q *
473473
// typeExpression
474474
q.GetAndSend(vd, func(v any) any { return v.(*tree.VariableDeclarations).TypeExpr },
475475
func(v any) { s.parent.Visit(v, q) })
476-
// varargs (nil for Go)
477-
q.GetAndSend(vd, func(_ any) any { return nil }, nil)
476+
// varargs
477+
q.GetAndSend(vd, func(v any) any {
478+
va := v.(*tree.VariableDeclarations).Varargs
479+
if va != nil {
480+
return *va
481+
}
482+
return nil
483+
}, func(v any) { sendSpace(v.(tree.Space), q) })
478484
// variables (list of right-padded NamedVariable)
479485
q.GetAndSendList(vd,
480486
func(v any) []any {

rewrite-go/rewrite/pkg/tree/j.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,7 @@ type VariableDeclarations struct {
10761076
Prefix Space
10771077
Markers Markers
10781078
TypeExpr Expression // the declared type (nil if inferred)
1079+
Varargs *Space // non-nil for variadic params (`...T`); holds prefix of `...`
10791080
Variables []RightPadded[*VariableDeclarator] // the declared variables
10801081
Specs *Container[Statement] // non-nil for grouped `var ( ... )`; Before = space before `(`
10811082
}

rewrite-go/src/integTest/java/org/openrewrite/golang/rpc/GolangParserIntegTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,18 @@ void literalWithNonPrimitiveType() {
451451
)
452452
);
453453
}
454+
455+
@Test
456+
void variadicParameter() {
457+
rewriteRun(
458+
go(
459+
"""
460+
package main
461+
462+
func foo(args ...string) {
463+
}
464+
"""
465+
)
466+
);
467+
}
454468
}

0 commit comments

Comments
 (0)