GeezerVison is showing github.com — [kotlin] Add @TypeName to VariableDeclaration, fix destructuring type leak by stokpop · Pull Request #7168 · pmd/pmd
Conversation
stokpop
commented
Oct 8, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes one item from #7091 (follow-up tracking issue for #6893): "Fix @TypeName leaking from first destructured component onto whole declaration node".
Note: this PR needs a rebase once #7165 is merged. #7165 suppresses empty-list attributes such as @AnnotationFqNames = () in tree dumps; whichever PR merges second has to regenerate the tree-dump golden files. The examples below are shown without @AnnotationFqNames for that reason.
Problem
For a destructuring declaration such as val (a, b) = Pair(1, "x"), kotlin-type-mapper records each component separately (kind=DESTRUCTURED_VARIABLE, with the correct type resolved through component1()/component2()). KotlinTypeAnnotationVisitor matched any declaration on the same line without filtering by kind, so the type of the first component leaked onto the whole PropertyDeclaration, which has no single type:
PropertyDeclaration[@Mutable = false, @TypeName = "kotlin.Int"]
+- MultiVariableDeclaration[]
+- VariableDeclaration[@Identifier = "a"]
+- VariableDeclaration[@Identifier = "b"]
The same leak existed in the line-based fallback of pmd-kotlin:typeIs(), pmd-kotlin:typeIsExactly() and pmd-kotlin:isNullable().
Changes
PropertyDeclaration only matches kind=PROPERTY declarations, so a destructuring declaration has no @TypeName. The fallback of typeIs/typeIsExactly/isNullable ignores destructured components for a PropertyDeclaration as well.
VariableDeclaration now has its own @TypeName, so "find all variables of type X" also covers destructured variables: //VariableDeclaration[pmd-kotlin:typeIs('kotlin.Int')]. It covers property variables, for-loop variables, lambda parameters and each component of a destructuring declaration (val (a, b) = ..., for ((a, b) in ...), { (a, b) -> ... }).
VariableDeclaration is matched by name within its enclosing declaration, without falling back to a declaration on an adjacent line. Before, a variable without its own declaration entry could borrow the type of a neighbouring one. This also corrects an existing golden file, where a multi-line string property got kotlin.Int from the line above.
Example from the new VariableTypes tree-dump fixture:
fun variableTypes(items: List<String>, pairs: List<Pair<Int, String>>) {
val single: String = "x"
val (first, second) = Pair(1, "y")
for (item in items) { }
for ((number, text) in pairs) { }
pairs.forEach { (left, right) -> }
items.forEach { typed: String -> }
items.forEach { inferred -> }
}
PropertyDeclaration[@Mutable = false, @TypeName = "kotlin.String"]
VariableDeclaration[@Identifier = "single", @TypeName = "kotlin.String"]
PropertyDeclaration[@Mutable = false]
MultiVariableDeclaration[]
VariableDeclaration[@Identifier = "first", @TypeName = "kotlin.Int"]
VariableDeclaration[@Identifier = "second", @TypeName = "kotlin.String"]
ForStatement[@TypeName = "kotlin.String"]
VariableDeclaration[@Identifier = "item", @TypeName = "kotlin.String"]
ForStatement[@TypeName = "kotlin.Pair<kotlin.Int, kotlin.String>"]
MultiVariableDeclaration[]
VariableDeclaration[@Identifier = "number", @TypeName = "kotlin.Int"]
VariableDeclaration[@Identifier = "text", @TypeName = "kotlin.String"]
MultiVariableDeclaration[]
VariableDeclaration[@Identifier = "left", @TypeName = "kotlin.Int"]
VariableDeclaration[@Identifier = "right", @TypeName = "kotlin.String"]
VariableDeclaration[@Identifier = "typed", @TypeName = "kotlin.String"]
VariableDeclaration[@Identifier = "inferred", @TypeName = "kotlin.String"]
(Excerpt of the relevant nodes; tokens and expressions left out.)
Known limitations
API change
The type attributes are @Experimental. A destructuring PropertyDeclaration no longer reports a type; the release notes have an entry under "API Changes". Deprecating type info on PropertyDeclaration in favour of VariableDeclaration is tracked separately in #7091.
Tests
Test-first: unit tests in KotlinTypeAnnotationVisitorTest, KotlinTypeIsFunctionTest and KotlinIsNullableFunctionTest were red before the fixes. New tree-dump fixture VariableTypes.kt/.txt; regenerated affected golden files. Docs updated in kotlin.md.
Related
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
jborgers reacted with thumbs up emoji
|
Documentation Preview
Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.
Regression Tester Report
(comment created at 2026-11-05 09:57:55+00:00 for f1f7dbf)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
stokpop
added a commit
to stokpop/PMD-jPinpoint-rules
that referenced
this pull request
Oct 12, 2026
val (a, b) = ... destructuring declarations recorded each component as a
separate kind=DESTRUCTURED_VARIABLE entry by kotlin-type-mapper, but
visitPropertyDeclaration matched any declaration on the same line
regardless of kind, so the whole PropertyDeclaration node picked up the
first component's type (e.g. kotlin.Int for 'a') instead of having no
type at all.
Filter to kind=PROPERTY, mirroring visitClassParameter, so destructuring
declarations correctly have no @typename on the whole node.
…e leak
Each VariableDeclaration in a property declaration now carries its own
@typename, including each component of a destructuring declaration
(val (a, b) = ...), so destructured variables can be found by type.
The line-based declaration fallback in typeIs/typeIsExactly/isNullable
ignored the destructuring fix on the attribute, still matching a
component's type on the whole PropertyDeclaration. Skip
DESTRUCTURED_VARIABLE declarations there for PropertyDeclaration nodes.
Extract annotatePropertyType() shared by PropertyDeclaration and
ClassParameter. Regenerate tree dumps, document in kotlin.md and
release notes.
Single for-loop variables (for (item in items)) now carry @typename on
their VariableDeclaration too. Destructured components only match their
own DESTRUCTURED_VARIABLE entry, so the enclosing for-loop parameter
(whose range also covers "(a, b)") can never be picked by ordering.
Add VariableTypes tree dump covering property, destructured, for-loop,
destructured for-loop and destructured lambda variables. Document
querying VariableDeclaration to find variables by type.
Match VariableDeclaration type info by name within the enclosing
declaration instead of falling back to the first candidate on an
adjacent line. An inferred lambda parameter no longer borrows the type
of a neighbouring variable, and a property whose initializer starts on
the next line gets its own type.
…eters
kotlin-type-mapper 0.7.2 records lambda parameters with an inferred type and
starts property declarations at their val/var keyword. An inferred lambda
parameter such as { x -> ... } now gets @typename on its VariableDeclaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
I'll push some minor improvements (more tests, simplified KotlinTypeAnnotationVisitor impl) and once CI build is green, will merge it for 7.30.0.
One thing to add maybe to #7091: I don't understand, why ForStatement has a TypeName. It looks a bit odd (and then we could argue, that a PropertyDeclaration for destructuring vars should have the equivalent Pair<...> type as well). Since it is existing functionality, we shouldn't change it right now. But something worth to check/cleanup maybe.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Nov 7, 2026
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|