arrow icon indicating copy to clipboard operation
arrow copied to clipboard

Arrow Core 2 causes undeclared type variable: A when using NonEmptyList

Open dcampogiani opened this issue 1 year ago • 8 comments

Hi,

when I try to inject with Hilt a component returning NonEmptyList, such as:

internal fun interface StateBuilder {
    fun build(): NonEmptyList<String>
}

I'm getting

bad class file: /Users/danielecampogiani/Developing/android/arrow-2-empty-list-issue/app/build/tmp/kotlin-classes/debug/com/danielecampogiani/StateBuilder.class
undeclared type variable: A
Please remove or make sure it appears in the correct subdirectory of the classpath.

I've created a reproducible issue in this repo: https://github.com/dcampogiani/Arrow-Core-2-NonEmptyList-Issue

Dependencies:

  • arrowCore = "2.0.0"
  • hilt = "2.54"
  • kotlin = "2.1.0"

Everything works fine if I use arrowCore=1.2.4, or replace NonEmptyList with List

dcampogiani avatar Dec 24 '24 10:12 dcampogiani

NonEmptyList v1.2.4 is a normal class. Since 2.0.0, it is a @JvmInline value class. Does Hilt/Dagger support @JvmInline value class?

hoc081098 avatar Dec 24 '24 10:12 hoc081098

I believe a similar issue has been reported, and a hot fix either has been released or will be released soon. The issue is that the type parameter of Nel needs to have the same name as the type parameter of List, so the fix does precisely that. I'll try to find the related issue

kyay10 avatar Dec 24 '24 11:12 kyay10

@kyay10 if you are referring to: https://github.com/arrow-kt/arrow/pull/3549, I've tried with version 2.0.1-apha.1 and now the error is:

/Users/danielecampogiani/Developing/android/arrow-2-empty-list-issue/app/build/generated/ksp/debug/java/com/danielecampogiani/MainViewModel_Factory.java:28: error: cannot access StateBuilder
  private final Provider<StateBuilder> builderProvider;
                         ^
   bad class file: /Users/danielecampogiani/Developing/android/arrow-2-empty-list-issue/app/build/tmp/kotlin-classes/debug/com/danielecampogiani/StateBuilder.class
undeclared type variable: E
Please remove or make sure it appears in the correct subdirectory of the classpath.

From: undeclared type variable: A to: undeclared type variable: E

dcampogiani avatar Dec 27 '24 08:12 dcampogiani

Thanks for the reproducer. It turns out that this is a reported bug in the Kotlin compiler. It seems that if you use NonEmptyList<String>, the compiler still "leaves" an undeclared type variable standing for the type parameter of NonEmptyList, and which ultimately causes the problem.

serras avatar Jan 10 '25 16:01 serras

Are there any work arounds for this, or do we have to sit tight until the Kotlin compiler bug is fixed?

hugomd avatar Apr 11 '25 21:04 hugomd

Unfortunately the only option until the fix is released is to use List<A> instead and perform validation manually.

serras avatar Apr 12 '25 16:04 serras

Thanks for the response @serras, I'll give that a go 😄

hugomd avatar Apr 12 '25 19:04 hugomd

Update: the bug is fixed in the compiler, but will be released in 2.2.20. I'll close this issue once it's released and we check it works.

serras avatar May 16 '25 11:05 serras

@dcampogiani @hugomd could any of you test whether 2.2.20-Beta1 fixes this issue for you? I think it does, but I'd like to double check before closing this issue.

serras avatar Jul 23 '25 19:07 serras

@serras can I find the 2.2.20-Beta1 deployed anywhere?

dcampogiani avatar Jul 24 '25 08:07 dcampogiani

@dcampogiani it's deployed as usual, so you can just change your Kotlin version 2.2.20-Beta1 as with any other

serras avatar Jul 29 '25 19:07 serras

Sorry, my bad, I thought 2.2.20-Beta1 was referring to the arrow version. I'll try as soon as I can.

dcampogiani avatar Jul 30 '25 13:07 dcampogiani

@dcampogiani @hugomd could any of you test whether 2.2.20-Beta1 fixes this issue for you? I think it does, but I'd like to double check before closing this issue.

@serras I've confirmed 2.2.20-Beta1 fixes this for us! The inner type is appropriately propagated 🎉

Thank you for pushing on this, glad to see it finally fixed 🙏🏻

hugomd avatar Aug 08 '25 06:08 hugomd