Skip to content

Go: override Java receiver visitors for Go-specific type mismatches#7258

Merged
jkschneider merged 1 commit intomainfrom
jkschneider/go-receiver-overrides
Apr 2, 2026
Merged

Go: override Java receiver visitors for Go-specific type mismatches#7258
jkschneider merged 1 commit intomainfrom
jkschneider/go-receiver-overrides

Conversation

@jkschneider
Copy link
Copy Markdown
Member

Summary

Add GolangReceiverDelegate overrides for 5 Java receiver visitor methods that fail when deserializing Go ASTs:

  • visitArrayType — Go slice element types like []*T produce J.Unary, not TypeTree
  • visitLiteral — Go primitive literals arrive as JavaType.Class, not JavaType.Primitive
  • visitMethodDeclaration — Go return types can be pointer/slice types that don't implement TypeTree
  • visitReturn — Go function literals in return position are J.MethodDeclaration, not Expression
  • visitVariableDeclarations — Go pointer/slice/map types in type position don't implement TypeTree

Each override reads the RPC value without the Java-side type constraint, then uses the typed setter when possible or reflection when the Go node doesn't satisfy the Java interface.

Test plan

  • Built and tested against 15 popular Go repos (gorilla/mux, gin-gonic/gin, go-chi/chi, sirupsen/logrus, stretchr/testify, spf13/cobra, spf13/viper, gofiber/fiber, json-iterator/go, go-yaml/yaml, FiloSottile/mkcert, wagoodman/dive, pocketbase/pocketbase, nektos/act, usememos/memos)
  • 1,235 total .go files — 0 Go parse errors (100% success, up from 368 errors on current main)
  • Recipe installation and execution works end-to-end (184 recipes from recipes-go)

Go's AST uses nodes in positions where Java's model requires stricter
types. For example, Go pointer types are J.Unary (not TypeTree),
function literals are J.MethodDeclaration (not Expression), and Go
primitives may arrive as JavaType.Class (not JavaType.Primitive).

Add GolangReceiverDelegate overrides for visitArrayType,
visitLiteral, visitMethodDeclaration, visitReturn, and
visitVariableDeclarations. Each reads the RPC value without the
Java-side type constraint, then uses the typed setter when possible
or reflection when the Go node doesn't satisfy the Java interface.

Tested against 15 popular Go repos (1,235 .go files total):
0 Go parse errors (100% success rate).
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Apr 2, 2026
@jkschneider jkschneider merged commit 8174845 into main Apr 2, 2026
1 check passed
@jkschneider jkschneider deleted the jkschneider/go-receiver-overrides branch April 2, 2026 19:39
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Apr 2, 2026
jkschneider added a commit that referenced this pull request Apr 2, 2026
jkschneider added a commit that referenced this pull request Apr 2, 2026
jkschneider added a commit that referenced this pull request Apr 2, 2026
…rrides (#7261)

* Revert "Go: override Java receiver visitors for Go-specific type mismatches (#7258)"

This reverts commit 8174845.

* Go: add StatementExpression for func literals, fix variadic params and parse input order

Add Go.StatementExpression (both Java and Go sides) to wrap
MethodDeclaration in expression contexts, following the same pattern
as Cs.StatementExpression in rewrite-csharp. Func literals like
`func(x int) int { return x }` are parsed as MethodDeclaration (a
Statement) but need to appear in returns, assignments, and call args.

Fix variadic parameter handling: detect `...T` in mapFieldListAsParams,
store the `...` prefix in VariableDeclarations.Varargs, and pass only
the element type through mapTypeExpr. Previously `...T` was mapped as
Unary{Spread, T} in the TypeExpr field, which violated the TypeTree
constraint.

Fix RPC parse input order: check Text before Path/SourcePath so text-
based inputs (from integration tests) aren't mistakenly treated as
file paths.

Add 8 integration tests. 51 of 52 pass (goroutineAndDefer fails due
to directly-invoked func literal `go func(){}()` padding mismatch).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant