@@ -155,6 +155,7 @@ data class TypeResultGenerator(val options: Options, val implicitAccessor: Acces
155
155
add(" $paramName = " )
156
156
add(param.generate())
157
157
}
158
+
158
159
else -> add(param.generate())
159
160
}
160
161
}
@@ -197,13 +198,15 @@ data class TypeResultGenerator(val options: Options, val implicitAccessor: Acces
197
198
} else {
198
199
add(" %T::class.java.name" , typeName)
199
200
}
201
+
200
202
is ParameterizedTypeName -> {
201
203
addTypeName(typeName.rawType)
202
204
for (arg in typeName.typeArguments) {
203
205
add(" +" )
204
206
addTypeName(arg)
205
207
}
206
208
}
209
+
207
210
is LambdaTypeName -> {
208
211
val functionName = if (typeName.isSuspending) {
209
212
ClassName (" kotlin.coroutines" , " SuspendFunction${typeName.parameters.size} " )
@@ -219,6 +222,7 @@ data class TypeResultGenerator(val options: Options, val implicitAccessor: Acces
219
222
add(" +" )
220
223
addTypeName(typeName.returnType)
221
224
}
225
+
222
226
else -> add(" %S" , typeName)
223
227
}
224
228
} else {
@@ -323,8 +327,14 @@ data class TypeResultGenerator(val options: Options, val implicitAccessor: Acces
323
327
324
328
private fun TypeResult.LateInit.generate (): CodeBlock {
325
329
return CodeBlock .builder().apply {
326
- beginControlFlow(" run" )
327
- addStatement(" lateinit var %N: %T" , name, result.key.type.toTypeName())
330
+ // Using the run extension method creates a new 'this' scope which causes kotlin's type inference for the
331
+ // current 'this' to be dropped. This breaks scoped parent resolution as we use
332
+ // require(parent is ScopedComponent) to access the parent scope.
333
+ // To work around this we explicitly pass the return type to ensure it calls the top-level run overload
334
+ // instead of the extension method.
335
+ val returnType = result.key.type.toTypeName()
336
+ beginControlFlow(" run<%T>" , returnType)
337
+ addStatement(" lateinit var %N: %T" , name, returnType)
328
338
add(result.generate())
329
339
beginControlFlow(" .also" )
330
340
addStatement(" %N = it" , name)
0 commit comments