Kotlin: declare caller-scope type variables on synthetic Template class#7414
Merged
Kotlin: declare caller-scope type variables on synthetic Template class#7414
Conversation
The Kotlin `BlockStatementTemplateGenerator` ignored the `typeVariables`
collected from parameter types, emitting a bare `class Template {`. When a
template parameter's type referenced a caller-scope generic (e.g.
`Container<T>` from an enclosing `fun <T : Any> ...`), the substitution
produced `__P__.p<Container<T>>()` inside a class where `T` was undeclared.
Now the Kotlin generator declares those type variables using Kotlin
syntax (`T : Bound` for a single bound, `where` clause for multiple).
Fixes #7407
greg-at-moderne
approved these changes
Apr 17, 2026
| } | ||
| } | ||
|
|
||
| private static String kotlinTypeParameters(Collection<JavaType.GenericTypeVariable> typeVariables) { |
Contributor
There was a problem hiding this comment.
Not fully sure, can we re-use the printer here?
Member
Author
There was a problem hiding this comment.
The KotlinPrinter operates on LST nodes (e.g. J.TypeParameter), whereas here we only have JavaType.GenericTypeVariable metadata from Substitutions.getTypeVariables() — there's no direct conversion. To improve reuse I've extracted the string-building helper into KotlinTypeUtils.toKotlinTypeParameters(...) so it's available to other template/code-generation paths that also start from JavaType metadata.
# Conflicts: # rewrite-kotlin/src/main/java/org/openrewrite/kotlin/internal/template/KotlinBlockStatementTemplateGenerator.java
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.
Summary
The Kotlin
BlockStatementTemplateGeneratorpreviously ignored the type variables collected from parameter types, emitting a bareclass Template {even when substituted parameters referenced caller-scope generics likeContainer<T>. The resulting synthetic class failed to parse becauseTwas undeclared.This change declares those type variables on the synthetic class using Kotlin syntax (
T : Boundfor a single bound,whereclause for multi-bound), mirroring the Java generator'sTemplate<T>handling.Fixes BlockStatementTemplateGenerator leaks unbound type variables from caller scope into synthetic template class #7407
Test plan
KotlinTemplateTest.parameterTypeWithCallerScopeTypeVariableasserting the generated template containsclass Template<T./gradlew :rewrite-kotlin:test