Skip to content

Generated Dagger factory won't compile if a module has an annotation that requires OptIn #1133

@Egorand

Description

@Egorand

Seems to be a dupe of #488, but I'm bumping into it with Anvil 2.5.1 (Kotlin version is 2.1.0), so submitting a new report.

We've got the following annotation:

@RequiresOptIn(
  level = RequiresOptIn.Level.ERROR,
  message = "...",
)
@Retention(AnnotationRetention.BINARY)
@Target(
  AnnotationTarget.CLASS,
  AnnotationTarget.FUNCTION,
  AnnotationTarget.FIELD,
  AnnotationTarget.PROPERTY,
)
annotation class UnstableSyncValueMigrationApi

Which annotates a class:

@UnstableSyncValueMigrationApi
class DebugSyncValueMigrationBypass @Inject constructor() : SyncValueMigrationBypass

That's bound in a module:

@UnstableSyncValueMigrationApi
@Module
@ContributesTo(SandboxedScope::class)
abstract class SyncValueMigrationBypassModule {
  @Binds abstract fun bind(impl: DebugSyncValueMigrationBypass): SyncValueMigrationBypass
}

Anvil generates the following factory:

// Generated by Anvil.
// https://github.com/square/anvil
@file:Suppress(
  "DEPRECATION",
  "OPT_IN_USAGE",
  "OPT_IN_USAGE_ERROR",
)

package com.squareup.cash.`data`.featureflags.syncvalue

import dagger.`internal`.Factory
import kotlin.Suppress
import kotlin.jvm.JvmStatic

public object DebugSyncValueMigrationBypass_Factory : Factory<DebugSyncValueMigrationBypass> {
  override fun `get`(): DebugSyncValueMigrationBypass = newInstance()

  @JvmStatic
  public fun create(): DebugSyncValueMigrationBypass_Factory = this

  @JvmStatic
  public fun newInstance(): DebugSyncValueMigrationBypass = DebugSyncValueMigrationBypass()
}

Which fails to compile:

e: file:///.../DebugSyncValueMigrationBypass_Factory.kt:16:16 Base declaration of supertype 'Factory' needs opt-in. The declaration override must be annotated with '@com.squareup.cash.common.backend.featureflags.syncvalue.UnstableSyncValueMigrationApi' or '@OptIn(com.squareup.cash.common.backend.featureflags.syncvalue.UnstableSyncValueMigrationApi::class)'

The workaround I found is to change RequiresOptIn.Level to WARNING, which allows the compilation to go through, but is not what we ultimately want.

This happens in cash-android, so please feel free to reach out internally for more troubleshooting data!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions