Repository navigation
[kotlin] Add @TypeName to VariableDeclaration, fix destructuring type leak - #7168
Conversation
|
Compared to main: (comment created at 2026-11-05 09:57:55+00:00 for f1f7dbf) |
…meters Notes: track upstream pmd/pmd#7168.
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.
c16f1a1 to
7be4b9c
Compare
adangel
left a comment
There was a problem hiding this comment.
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.
Fixes one item from #7091 (follow-up tracking issue for #6893): "Fix
@TypeNameleaking from first destructured component onto whole declaration node".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 throughcomponent1()/component2()).KotlinTypeAnnotationVisitormatched any declaration on the same line without filtering by kind, so the type of the first component leaked onto the wholePropertyDeclaration, which has no single type:The same leak existed in the line-based fallback of
pmd-kotlin:typeIs(),pmd-kotlin:typeIsExactly()andpmd-kotlin:isNullable().Changes
PropertyDeclarationonly matcheskind=PROPERTYdeclarations, so a destructuring declaration has no@TypeName. The fallback oftypeIs/typeIsExactly/isNullableignores destructured components for aPropertyDeclarationas well.VariableDeclarationnow 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) -> ... }).VariableDeclarationis 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 gotkotlin.Intfrom the line above.Example from the new
VariableTypestree-dump fixture:(Excerpt of the relevant nodes; tokens and expressions left out.)
{ x -> ... }gets@TypeNametoo, and starts property declarations at theirval/varkeyword instead of the initializer.Known limitations
ithas 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 destructuringPropertyDeclarationno longer reports a type; the release notes have an entry under "API Changes". Deprecating type info onPropertyDeclarationin favour ofVariableDeclarationis tracked separately in #7091.Tests
Test-first: unit tests in
KotlinTypeAnnotationVisitorTest,KotlinTypeIsFunctionTestandKotlinIsNullableFunctionTestwere red before the fixes. New tree-dump fixtureVariableTypes.kt/.txt; regenerated affected golden files. Docs updated inkotlin.md.Related