Repository navigation
Fix shortenFullyQualifiedTypes corrupting method references on fields (#3133) - #3134
Open
maxandersen wants to merge 1 commit into
Open
maxandersen wants to merge 1 commit into
maxandersen wants to merge 1 commit into
Conversation
maxandersen
force-pushed
the
fix-3133-methodref-scope
branch
from
October 6, 2026 23:38
ef52431 to
e02c017
Compare
…diffplug#3133) `localVar.field::method` is parsed by JavaParser as a ClassOrInterfaceType scope, so the formatter shortened `catalog.aliases::get` to `aliases::get` and injected a bogus `import catalog.aliases;`, breaking compilation. Method-reference scopes now go through the same known-package / minimum-depth heuristic already used for other expression scopes (FieldAccessExpr / MethodCallExpr) before being treated as a fully-qualified type. This also leaves single-segment, non-package scopes alone (e.g. a static-imported or field `config.Database::connect`), which the type path previously mis-shortened. Adds regression tests: field method-ref left untouched; single-segment non-package scope left untouched; genuine FQN type method-ref (java.lang.Integer::parseInt) still shortened.
maxandersen
force-pushed
the
fix-3133-methodref-scope
branch
from
October 6, 2026 23:42
e02c017 to
cb79023
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3133.
Problem
shortenFullyQualifiedTypes()corrupts a method reference on a field/variable. JavaParser parses the scope oflocalVar.field::methodas aClassOrInterfaceType(the LHS of::is type-ambiguous), so the formatter treatedcatalog.aliases::getas a fully-qualified type, rewrote it toaliases::get, and injected a bogusimport catalog.aliases;— which does not compile. It only manifests onspotlessApply(not the read-onlyspotlessCheck), so a developer formats, commits, and then the build breaks.Before / After (the bug)
Fix
The expression-context path (
FieldAccessExpr/MethodCallExprscopes) already guards againstvariable.fieldshapes via a known-package / minimum-depth heuristic. A method-reference scope, however, reaches theClassOrInterfaceTypevisitor instead, which trusts any package-like (lowercase first segment) dotted name — socatalog.aliasesslips through.This routes method-reference scopes through the same heuristic: the existing inline check in
processExpressionScopeis extracted into one sharedisTrustedFullyQualifiedName(dottedName, knownPackages)used by both paths. A dotted name is trusted as a fully-qualified type only when the package (segments before the first upper-case segment) is already known from imports / the file's own package /java.lang, or has at least two segments. This:catalog.aliases::getalone (no upper-case type segment);config.Database::connect(upper-case simple name butconfigis not a known package) — which the old type path mis-shortened;java.lang.Integer::parseInt→Integer::parseInt.No symbol resolution / classpath is used (consistent with the step's "when in doubt, leave it alone" design).
Tests
issue3133_methodReferenceOnFieldNotShortened—catalog.aliases::getleft untouched, no bogus import.methodReferenceWithSingleSegmentScopeLeftAlone—config.Database::connectleft untouched.methodReferenceOnRealTypeStillShortened—java.lang.Integer::parseInt→Integer::parseInt.All 39
ShortenFullyQualifiedTypesStepTestcases pass.Found while enabling the step on a real codebase (jbangdev/jbang), where
catalog.aliases::getbroke the build.