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

Update to dagger 2.50 #830

Merged
merged 15 commits into from
Feb 6, 2024
Merged
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()),
Comment on lines +575 to +578
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does Dagger always generate both now? Or are they keying off anything to decide on one vs the other? Looks like all the tests passed so it should be fine, but it feels a little surprising

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It always generates both now. The original for backward compatibility, the new one for forward compatibility. They're going to support both going forward

)
.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
Loading