Skip to content
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 @@ -412,20 +412,27 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
method: AstMember,
scope: AstType?,
key: TypeKey,
) = withCycleDetection(key, method) {
TypeResult.Provides(
className = context.className,
methodName = method.name,
accessor = accessor,
receiver = method.receiverParameterType?.let {
val key = TypeKey(it, method.qualifier(options))
resolve(context, method, key)
},
isProperty = method is AstProperty,
parameters = (method as? AstFunction)?.let {
resolveParams(context, method, scope, it.parameters)
} ?: emptyMap(),
)
): TypeResult {
if (scope != null && method is AstFunction && method.isSuspend) {
throw FailedToGenerateException(
"@Provides scoped with @${scope.simpleName} cannot be suspend, consider returning Deferred<T> instead."
)
}
return withCycleDetection(key, method) {
TypeResult.Provides(
className = context.className,
methodName = method.name,
accessor = accessor,
receiver = method.receiverParameterType?.let {
val key = TypeKey(it, method.qualifier(options))
resolve(context, method, key)
},
isProperty = method is AstProperty,
parameters = (method as? AstFunction)?.let {
resolveParams(context, method, scope, it.parameters)
} ?: emptyMap(),
)
}
}

private fun Scoped(
Expand All @@ -444,7 +451,7 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
context: Context,
constructor: AstConstructor,
scope: AstType?,
key: TypeKey
key: TypeKey,
) = withCycleDetection(key, constructor) {
TypeResult.Constructor(
type = constructor.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -822,4 +822,33 @@ class FailureTest {
contains("Cycle detected")
}
}

@ParameterizedTest
@EnumSource(Target::class)
fun fails_if_scoped_provides_is_suspend(target: Target) {
val projectCompiler = ProjectCompiler(target, workingDir)

assertFailure {
projectCompiler.source(
"MyComponent.kt",
"""
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Scope
import me.tatarka.inject.annotations.Assisted
import me.tatarka.inject.annotations.Inject
import me.tatarka.inject.annotations.Provides

@Scope annotation class MyScope

@MyScope
@Component abstract class MyComponent {
abstract val bar: String
@Provides @MyScope suspend fun bar(): String = TODO()
}
""".trimIndent()
).compile()
}.output().all {
contains("@Provides scoped with @MyScope cannot be suspend, consider returning Deferred<T> instead.")
}
}
}