Skip to content

Commit

Permalink
Update to dagger 2.50 (#830)
Browse files Browse the repository at this point in the history
* Update dagger + force later guava version in tests

* Generate new Dagger createFactoryProvider functions

* Prohibit extension functions for @BINDS

* Disable KspComponentProcessor in KSP tests
  • Loading branch information
ZacSweers authored Feb 6, 2024
1 parent 0679595 commit 622e663
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 121 deletions.
2 changes: 1 addition & 1 deletion compiler-utils/dependencies/runtimeClasspath.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
com.google.dagger:dagger:2.46.1
com.google.dagger:dagger:2.50
com.squareup:kotlinpoet-jvm:1.16.0
com.squareup:kotlinpoet:1.16.0
javax.inject:javax.inject:1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.tschuchort.compiletesting.kspArgs
import com.tschuchort.compiletesting.kspWithCompilation
import com.tschuchort.compiletesting.symbolProcessorProviders
import dagger.internal.codegen.ComponentProcessor
import dagger.internal.codegen.KspComponentProcessor
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.config.JvmTarget
import java.io.File
Expand Down Expand Up @@ -107,7 +108,11 @@ public class AnvilCompilation internal constructor(
ServiceLoader.load(
SymbolProcessorProvider::class.java,
SymbolProcessorProvider::class.java.classLoader,
),
)
// TODO for now, we don't want to run the dagger KSP processor while we're testing
// KSP. This will change when we start supporting dagger-KSP, at which point we can
// change this filter to be based on https://github.com/square/anvil/pull/713
.filterNot { it is KspComponentProcessor.Provider },
)
addAll(mode.symbolProcessorProviders)
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dependencies {

testImplementation(testFixtures(project(":compiler-utils")))
testImplementation(libs.dagger2.compiler)
// Force later guava version for Dagger's needs
testImplementation(libs.guava)
testImplementation(libs.kotlin.annotationProcessingEmbeddable)
testImplementation(libs.kotlin.compileTesting)
testImplementation(libs.kotlin.compileTesting.ksp)
Expand Down
2 changes: 1 addition & 1 deletion compiler/dependencies/runtimeClasspath.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
com.google.dagger:dagger:2.46.1
com.google.dagger:dagger:2.50
com.squareup:kotlinpoet-jvm:1.16.0
com.squareup:kotlinpoet-ksp:1.16.0
com.squareup:kotlinpoet:1.16.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,24 @@ internal object AssistedFactoryCodeGen : AnvilApplicabilityChecker {
.build(),
)
.apply {
fun createFactory(name: String, providerTypeName: ClassName): FunSpec {
return FunSpec.builder(name)
.jvmStatic()
.addTypeVariables(typeParameters)
.addParameter(DELEGATE_FACTORY_NAME, generatedFactoryTypeName)
.returns(providerTypeName.parameterizedBy(baseFactoryTypeName))
.addStatement(
"return %T.create(%T($DELEGATE_FACTORY_NAME))",
InstanceFactory::class,
implParameterizedTypeName,
)
.build()
}
TypeSpec.companionObjectBuilder()
.addFunction(createFactory("create", Provider::class.asClassName()))
// New in Dagger 2.50: factories for dagger.internal.Provider
.addFunction(
FunSpec.builder("create")
.jvmStatic()
.addTypeVariables(typeParameters)
.addParameter(DELEGATE_FACTORY_NAME, generatedFactoryTypeName)
.returns(Provider::class.asClassName().parameterizedBy(baseFactoryTypeName))
.addStatement(
"return %T.create(%T($DELEGATE_FACTORY_NAME))",
InstanceFactory::class,
implParameterizedTypeName,
)
.build(),
createFactory("createFactoryProvider", dagger.internal.Provider::class.asClassName()),
)
.build()
.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ internal class BindsMethodValidator : PrivateCodeGenerator() {
)
}

if (function.function.isExtensionDeclaration()) {
throw AnvilCompilationExceptionFunctionReference(
message = "@Binds methods can not be an extension function",
functionReference = function,
)
}

val hasSingleBindingParameter =
(function.parameters.size == 1 && !function.function.isExtensionDeclaration()) ||
(function.parameters.isEmpty() && function.function.isExtensionDeclaration())
function.parameters.size == 1 && !function.function.isExtensionDeclaration()
if (!hasSingleBindingParameter) {
throw AnvilCompilationExceptionFunctionReference(
message = "@Binds methods must have exactly one parameter, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import dagger.internal.Factory
import dagger.internal.Preconditions
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.isExtensionDeclaration
import java.io.File

internal object ProvidesMethodFactoryCodeGen : AnvilApplicabilityChecker {
Expand Down Expand Up @@ -151,6 +152,12 @@ internal object ProvidesMethodFactoryCodeGen : AnvilApplicabilityChecker {
}

private fun CallableReference.Companion.from(function: KSFunctionDeclaration): CallableReference {
if (function.extensionReceiver != null) {
throw KspAnvilException(
message = "@Provides methods cannot be extension functions",
node = function,
)
}
val type = function.returnType?.resolve() ?: throw KspAnvilException(
message = "Error occurred in type resolution and could not resolve return type.",
node = function,
Expand Down Expand Up @@ -281,6 +288,12 @@ internal object ProvidesMethodFactoryCodeGen : AnvilApplicabilityChecker {
}

private fun CallableReference.Companion.from(function: MemberFunctionReference.Psi): CallableReference {
if (function.function.isExtensionDeclaration()) {
throw AnvilCompilationExceptionFunctionReference(
message = "@Provides methods can not be an extension function",
functionReference = function,
)
}
val type = function.returnTypeOrNull() ?: throw AnvilCompilationExceptionFunctionReference(
message = "Dagger provider methods must specify the return type explicitly when using " +
"Anvil. The return type cannot be inferred implicitly.",
Expand Down
Loading

0 comments on commit 622e663

Please sign in to comment.