GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [kotlin] Add @TypeName to VariableDeclaration, fix destructuring type leak by stokpop · Pull Request #7168 · pmd/pmd

Conversation

@stokpop

@stokpop stokpop commented Oct 8, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.)

  • Bumps kotlin-type-mapper to 0.7.2 (fix: property declaration position and inferred lambda parameters stokpop/kotlin-type-mapper#36): it records lambda parameters with an inferred type, so { x -> ... } gets @TypeName too, and starts property declarations at their val/var keyword instead of the initializer.

Known limitations

  • The implicit it has no declaration node and no @TypeName; tracked in [kotlin] Follow-ups from XPath functions/type attributes PR (#6893) #7091.

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

  • [kotlin] Follow-ups from XPath functions/type attributes PR (#6893) #7091 — tracking issue
  • [kotlin] Suppress empty-list attribute values in tree dump tests #7165 — rebase needed after merge (tree-dump golden files)
  • fix: property declaration position and inferred lambda parameters stokpop/kotlin-type-mapper#36 — inferred lambda parameters and property positions (released in 0.7.2)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jborgers reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@pmd-actions-helper

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

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stokpop stokpop mentioned this pull request Oct 8, 2026
[kotlin] Follow-ups from XPath functions/type attributes PR (#6893) #7091
Open
2 of 19 tasks
stokpop added a commit to stokpop/PMD-jPinpoint-rules that referenced this pull request Oct 12, 2026
@stokpop
fix(kotlin): ImproperVariableName allows unnamed _ variables and para…
a86de6b
…meters

Notes: track upstream pmd/pmd#7168.
stokpop added 5 commits October 29, 2026 15:10
@stokpop
[kotlin] Fix @typename leaking from first destructured component
8114963
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.
@stokpop
[kotlin] Add @typename to VariableDeclaration, close typeIs/isNullabl…
1b5de4f
…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.
@stokpop
[kotlin] Add @typename to for-loop VariableDeclaration
6da37a7
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.
@stokpop
[kotlin] Add @typename to explicitly typed lambda parameters
9408573
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.
@stokpop
[kotlin] Bump kotlin-type-mapper to 0.7.2, type inferred lambda param…
7be4b9c
…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
Merge branch 'main' into pr-7168
97c0cd4
@adangel adangel added the in:type-resolution Affects the type resolution code label Nov 5, 2026
adangel added 4 commits November 5, 2026 10:28
@adangel
[kotlin] Update javadoc (add missing lambda parameters and VariableDe…
9a33c48
…claration)
@adangel
[kotlin] Add additional tests for destructuring components
717e494
@adangel
[kotlin] KotlinTypeAnnotationVisitor: simplify visitVariableDeclaration
badd1a9
@adangel
[doc] Update release notes (pmd#7168)
f1f7dbf
adangel
adangel approved these changes Nov 5, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread ...rc/main/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...rc/main/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...rc/main/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...est/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitorTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-kotlin/src/test/resources/net/sourceforge/pmd/lang/kotlin/ast/testdata/VariableTypes.kt Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...rc/main/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...rc/main/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../main/java/net/sourceforge/pmd/lang/kotlin/rule/xpath/internal/KotlinIsNullableFunction.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread .../src/main/java/net/sourceforge/pmd/lang/kotlin/rule/xpath/internal/KotlinTypeIsFunction.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread ...est/java/net/sourceforge/pmd/lang/kotlin/types/internal/KotlinTypeAnnotationVisitorTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@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

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Nov 7, 2026
Emit the type of the whole destructured value for destructuring declarations stokpop/kotlin-type-mapper#37
Closed
[kotlin] Add @TypeName to MultiVariableDeclaration #7345
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

in:type-resolution Affects the type resolution code

Projects

None yet

Milestone

7.30.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@stokpop @adangel
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.