Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark receiver parameter type as referenced. #807

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class JdepsGenExtension(
is FunctionDescriptor -> {
resultingDescriptor.returnType?.let { addImplicitDep(it) }
resultingDescriptor.valueParameters.forEach { valueParameter ->
addImplicitDep(valueParameter.type)
collectTypeReferences(valueParameter.type, isExplicit = false)
}
val virtualFileClass = resultingDescriptor.getContainingKotlinJvmBinaryClass() as? VirtualFileKotlinClass
?: return
Expand Down Expand Up @@ -217,25 +217,37 @@ class JdepsGenExtension(
* are other types required for compilation such as supertypes and interfaces of those explicit
* types.
*/
private fun collectTypeReferences(kotlinType: KotlinType, collectSuperTypes: Boolean = true) {
addExplicitDep(kotlinType)
private fun collectTypeReferences(kotlinType: KotlinType,
isExplicit: Boolean = true,
collectSuperTypes: Boolean = true) {
if (isExplicit) {
addExplicitDep(kotlinType)
} else {
addImplicitDep(kotlinType)
}

if (collectSuperTypes) {
kotlinType.supertypes().forEach {
addImplicitDep(it)
}
}

collectTypeArguments(kotlinType)
collectTypeArguments(kotlinType, isExplicit)
}

fun collectTypeArguments(kotlinType: KotlinType, visitedKotlinTypes: MutableSet<KotlinType> = mutableSetOf()) {
fun collectTypeArguments(kotlinType: KotlinType,
isExplicit: Boolean,
visitedKotlinTypes: MutableSet<KotlinType> = mutableSetOf()) {
visitedKotlinTypes.add(kotlinType)
kotlinType.arguments.map { it.type }.forEach { typeArgument ->
addExplicitDep(typeArgument)
if (isExplicit) {
addExplicitDep(typeArgument)
} else {
addImplicitDep(typeArgument)
}
typeArgument.supertypes().forEach { addImplicitDep(it) }
if (!visitedKotlinTypes.contains(typeArgument)) {
collectTypeArguments(typeArgument, visitedKotlinTypes)
collectTypeArguments(typeArgument, isExplicit, visitedKotlinTypes)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,74 @@ class KotlinBuilderJvmJdepsTest {
assertImplicit(jdeps).doesNotContain(depWithReturnTypesSuperType)
}

@Test
fun `function call receiver type`() {
val depWithReceiverTypeSuperType = ctx.runCompileTask(Consumer { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource("SomeSuperType.kt",
"""
package something

open class SomeSuperType {}
""")
c.outputJar()
c.outputJdeps()
c.compileKotlin()
c.setLabel("depWithSuperType")
})
val depWithReceiverType = ctx.runCompileTask(Consumer { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource("SomeType.kt",
"""
package something

class SomeType : SomeSuperType() {}
""")
c.outputJar()
c.outputJdeps()
c.compileKotlin()
c.setLabel("depWithReceiverType")
c.addDirectDependencies(depWithReceiverTypeSuperType)
})

val depWithFunction = ctx.runCompileTask(Consumer { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource("ContainsFunction.kt",
"""
package something

fun receiverSomeType(arg: SomeType.() -> Unit) {}
""")
c.outputJar()
c.compileKotlin()
c.addDirectDependencies(depWithReceiverType)
c.addTransitiveDependencies(depWithReceiverTypeSuperType)
c.setLabel("depWithFunction")
})

val dependingTarget = ctx.runCompileTask(Consumer { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource("CallsFunctionWithReceiver.kt",
"""
package something

fun foo() {
receiverSomeType {
}
}
""")
c.outputJar()
c.compileKotlin()
c.addDirectDependencies(depWithFunction)
c.addTransitiveDependencies(depWithReceiverType, depWithReceiverTypeSuperType)
c.outputJdeps()
c.setLabel("dependingTarget")
})
val jdeps = depsProto(dependingTarget)

assertThat(jdeps.ruleLabel).isEqualTo(dependingTarget.label())

assertExplicit(jdeps).contains(depWithFunction.singleCompileJar())
assertImplicit(jdeps).contains(depWithReceiverType.singleCompileJar())
assertImplicit(jdeps).contains(depWithReceiverTypeSuperType.singleCompileJar())
}

@Test
fun `constructor parameters are required implicit dependencies`() {
val fooDep = ctx.runCompileTask(Consumer { c: KotlinJvmTestBuilder.TaskBuilder ->
Expand Down