Skip to content

Map Scala parameterless method calls to J.MethodInvocation - #9915

Merged
timtebeek merged 4 commits into
mainfrom
tim/gh-9913-parameterless-calls
Nov 17, 2026
Merged

timtebeek merged 4 commits into
mainfrom
tim/gh-9913-parameterless-calls

Conversation

@timtebeek

@timtebeek timtebeek commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Addresses the second part of #9913. Builds on #9914, which typed the method name of every Scala method invocation.

A qualified call to a method without an argument list, such as xs.reverse, "abc".length or xs.sum, was parsed as a J.FieldAccess with no method type, so MethodMatcher and UsesMethod could not find it. It is now a J.MethodInvocation whose empty argument list carries OmitParentheses. Its method type is instantiated the way the compiler applied it, which also gives xs.sum the type Int instead of its type parameter B. This also covers methods whose only parameter list is implicit (sum, sorted, flatten). Vals, fields, objects and packages are still J.FieldAccess, and so are the targets of setter assignments (c.count = 5) and methods the compiler eta-expands. Unqualified parameterless calls such as a bare foo are still a J.Identifier, which is out of scope here.

This is a draft because it changes the tree shape that recipes see. Visitor recipes that recognise these calls in visitFieldAccess will silently stop matching until they are updated to handle J.MethodInvocation, so those updates should be lined up before this merges.

Verified with ./gradlew :rewrite-scala:test, where all 1117 tests pass. memberSelectionHasType and implicitArgumentsPassedByTheCompilerAreMarked were updated for the new shape, and there are new tests for the issue's examples and for the cases that keep their shape.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 13, 2026
Base automatically changed from tim/gh-9913-quick-wins to main November 13, 2026 15:30
@timtebeek

Copy link
Copy Markdown
Member Author

This does require changes downstream in recipes link UseCount, UseIsEmpty and UseLastOption, as those had looked for the field type previously, whereas an implied method invocation is perhaps the better representation.

A qualified call to a method without an argument list, as xs.reverse,
"abc".length or xs.sum, was a J.FieldAccess without a method type, so
MethodMatcher and UsesMethod could not find it. It is now a
J.MethodInvocation whose empty arguments carry OmitParentheses, with the
method type instantiated as the typer applied it, which also types xs.sum
as Int rather than its type parameter. Assignment targets and
eta-expanded methods keep their current shape.

Refs #9913
@timtebeek
timtebeek force-pushed the tim/gh-9913-parameterless-calls branch from 94c65f0 to 2e014bc Compare November 13, 2026 15:53
@timtebeek timtebeek added scala bug Something isn't working parser labels Nov 13, 2026
@timtebeek
timtebeek marked this pull request as ready for review November 13, 2026 15:57
@timtebeek

Copy link
Copy Markdown
Member Author

@greg-at-moderne this is the more opinionated LST structure change that needs more thorough review, where I might lack the knowledge of the Scala internals & semantics. I still think this improves how we map elements to what they represent, but it does mean we need to account for the change downstream as well.

@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Nov 13, 2026
@greg-at-moderne

Copy link
Copy Markdown
Contributor

IMHO we should map method calls as J.MethodInvocation indeed, even if they syntactically look like a field access. So I agree with the general idea here.

@greg-at-moderne greg-at-moderne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One open question stands out.

class B {
val c = new Counter
c.count = 5
val f: () => String = "abc".toUpperCase

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular line I am not sure of whether it should be mapped as Method Invocation. This is similar to Object::toString method reference in Java. And this I am not sure how it's mapped (would need to check). I suggest to make sure this is mapped the same way in Scala (if possible).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. This line isn't mapped as a method invocation: the eta-expanded "abc".toUpperCase stays a J.FieldAccess with an EtaExpansion marker, as it was before this PR. I changed the test to assert that explicitly in 5940a56. Mapping eta-expansion like Java's method references would change all eta-expansions, not just parameterless ones, so I opened #9918 to follow up on that.

@timtebeek

Copy link
Copy Markdown
Member Author

As discussed on Slack with Knut and Greg we'll go ahead with this change, as it's a clear fix, and make the downstream adjustments.

@timtebeek
timtebeek merged commit 75aa657 into main Nov 17, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/gh-9913-parameterless-calls branch November 17, 2026 10:59
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Nov 17, 2026
timtebeek added a commit to sullis/rewrite-testing-frameworks that referenced this pull request Nov 17, 2026
… types in sync

openrewrite/rewrite#9915 parses a typed call without parentheses, such as
`5.seconds`, as a `J.MethodInvocation` whose arguments carry `OmitParentheses`.
`ReplaceScalaPostfixDurations` now produces that instead of a `J.FieldAccess`,
and matches typed units by `DurationConversions`, falling back to their names
where the enclosing Gatling call leaves them untyped. The field access branch
of `MigrateGatlingScalaDsl` stays for untyped sources, which are still parsed
that way.

As Scala calls into Gatling are now typed, the renamed calls get renamed method
types, and `maxConnectionsPerHost` takes the `int` it is passed.

`ignoreDefaultChecks` and `ignoreDefaultHeaders` are request builder methods,
so the test now calls them on a request rather than on the protocol.
timtebeek added a commit that referenced this pull request Nov 18, 2026
…Invocation (#10027)

* Scala: map a parameterless method called by its bare name to J.MethodInvocation

A call written without receiver or parentheses, as size inside a subclass
of ArrayList or currentThread after a static import, was a J.Identifier
typed with the method's return type, so ChangeMethodName and other
MethodMatcher-based recipes never saw it. It is now a select-less
J.MethodInvocation whose empty arguments carry OmitParentheses, as #9915
did for calls with a receiver.

Fixes #10024

* Build both parameterless invocation shapes through one helper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parser scala

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Scala parser leaves method types missing for type table stubs, parameterless calls and qualified method names

2 participants