Skip to content

[kotlin] Add @TypeName to VariableDeclaration, fix destructuring type leak - #7168

Merged
adangel merged 10 commits into
pmd:mainfrom
stokpop:kotlin/fix-destructuring-type-leak
Nov 5, 2026
Merged

adangel merged 10 commits into
pmd:mainfrom
stokpop:kotlin/fix-destructuring-type-leak

Conversation

@stokpop

@stokpop stokpop commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

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

@pmd-actions-helper

pmd-actions-helper Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

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)

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.
@stokpop
stokpop force-pushed the kotlin/fix-destructuring-type-leak branch from c16f1a1 to 7be4b9c Compare October 29, 2026 15:56
@stokpop
stokpop marked this pull request as ready for review October 30, 2026 06:04
@adangel adangel added the in:type-resolution Affects the type resolution code label Nov 5, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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.

@adangel adangel added this to the 7.30.0 milestone Nov 5, 2026
@adangel
adangel merged commit 8cdd00d into pmd:main Nov 5, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:type-resolution Affects the type resolution code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants