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

KSP2: IllegalArgumentException: Failed requirement. #1966

Closed
ZacSweers opened this issue Jun 11, 2024 · 14 comments
Closed

KSP2: IllegalArgumentException: Failed requirement. #1966

ZacSweers opened this issue Jun 11, 2024 · 14 comments
Assignees
Labels
AA waiting for upstream fix bug Something isn't working P1 major features or blocking bugs
Milestone

Comments

@ZacSweers
Copy link
Contributor

We encountered a crash in KSP2 (version 2.0.0-1.0.22) in a very specific scenario. The crash comes from FIR/AA, but not sure if this is something wrong with KSP2's impl as the code itself compiles fine in K2 without KSP running.

  • Define an interface in one module that defines a property from the JDK/Android SDK built against.
    • In our case, we originally encountered this with SharedPreferences from android, but found it also repros with just Java's Runnable
    • Does not occur with custom user sources or kotlin-origin classes. So - it may be that the type just needs to be originally written in Java
  • Define an abstract implementation of it in a separate module that overrides the property
    • In case it matters - different package name too
  • KSP fails to init (doesn't even get to processing) with the pasted stacktrace below

Repro:

// in :lib1
interface IRepro {
  val prefs: Runnable
}

// in :lib2
abstract class BaseRepro(
  override val prefs: Runnable,
) : IRepro

When KSP2 runs on lib2, the below trace happens

Caused by: java.lang.IllegalArgumentException: Failed requirement.	
	at org.jetbrains.kotlin.fir.scopes.impl.FirOverrideUtilsKt.isAbstractAccordingToRawStatus(FirOverrideUtils.kt:104)	
	at org.jetbrains.kotlin.fir.java.scopes.JavaOverrideChecker.chooseIntersectionVisibility(JavaOverrideChecker.kt:398)	
	at org.jetbrains.kotlin.fir.scopes.impl.FirTypeIntersectionScopeContext.createIntersectionOverride(FirTypeIntersectionScopeContext.kt:219)	
	at org.jetbrains.kotlin.fir.scopes.impl.FirIntersectionOverrideStorage$cacheByScope$1$1.invoke(FirTypeIntersectionScopeContext.kt:514)	
	at org.jetbrains.kotlin.fir.scopes.impl.FirIntersectionOverrideStorage$cacheByScope$1$1.invoke(FirTypeIntersectionScopeContext.kt:513)	
	at org.jetbrains.kotlin.analysis.low.level.api.fir.fir.caches.FirThreadSafeCache.getValue(FirThreadSafeCache.kt:27)	
	at org.jetbrains.kotlin.fir.scopes.impl.FirTypeIntersectionScopeContext$ResultOfIntersection$NonTrivial.chosenSymbol_delegate$lambda$0(FirTypeIntersectionScopeContext.kt:72)	
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)	
	at org.jetbrains.kotlin.fir.scopes.impl.FirTypeIntersectionScopeContext$ResultOfIntersection$NonTrivial.getChosenSymbol(FirTypeIntersectionScopeContext.kt:70)	
	at org.jetbrains.kotlin.fir.java.scopes.JavaClassUseSiteMemberScope.collectProperties(JavaClassUseSiteMemberScope.kt:178)	
	at org.jetbrains.kotlin.fir.scopes.impl.AbstractFirUseSiteMemberScope.processPropertiesByName(AbstractFirUseSiteMemberScope.kt:156)	
	at org.jetbrains.kotlin.fir.java.scopes.JavaClassMembersEnhancementScope.processPropertiesByName(JavaClassMembersEnhancementScope.kt:33)	
	at org.jetbrains.kotlin.fir.scopes.FirDelegatingContainingNamesAwareScope.processPropertiesByName(FirDelegatingContainingNamesAwareScope.kt:46)	
	at org.jetbrains.kotlin.fir.scopes.FirContainingNamesAwareScopeKt.processAllCallables(FirContainingNamesAwareScope.kt:32)	
	at org.jetbrains.kotlin.analysis.api.fir.scopes.FirCallableFilteringScope.cachedCallableNames_delegate$lambda$2(FirCallableFilteringScope.kt:24)	
	at kotlin.UnsafeLazyImpl.getValue(Lazy.kt:81)	
	at org.jetbrains.kotlin.analysis.api.fir.scopes.FirCallableFilteringScope.getCachedCallableNames(FirCallableFilteringScope.kt:22)	
	at org.jetbrains.kotlin.analysis.api.fir.scopes.FirCallableFilteringScope.getCallableNames(FirCallableFilteringScope.kt:32)	
	at org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirDelegatingNamesAwareScope.getPossibleCallableNames(KtFirDelegatingNamesAwareScope.kt:25)	
	at org.jetbrains.kotlin.analysis.api.fir.scopes.KtFirBasedScope.getCallableSymbols(KtFirBasedScope.kt:27)	
	at org.jetbrains.kotlin.analysis.api.scopes.KtScope.getCallableSymbols$default(KtScope.kt:31)	
	at org.jetbrains.kotlin.analysis.api.scopes.KtScope$getAllSymbols$1$1.invokeSuspend(KtScope.kt:19)	
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)	
	at kotlin.sequences.SequenceBuilderIterator.hasNext(SequenceBuilder.kt:129)	
	at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:331)	
	at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:318)	
	at kotlin.sequences.DistinctIterator.computeNext(Sequences.kt:588)	
	at kotlin.collections.AbstractIterator.tryToComputeNext(AbstractIterator.kt:55)	
	at kotlin.collections.AbstractIterator.hasNext(AbstractIterator.kt:34)	
	at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:214)	
	at com.google.devtools.ksp.common.MemoizedSequence$CachedIterator.hasNext(MemoizedSequence.kt:32)	
	at com.google.devtools.ksp.impl.symbol.kotlin.KSClassDeclarationImpl$declarations$2.invoke(KSClassDeclarationImpl.kt:240)	
	at com.google.devtools.ksp.impl.symbol.kotlin.KSClassDeclarationImpl$declarations$2.invoke(KSClassDeclarationImpl.kt:178)	
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)	
	at com.google.devtools.ksp.impl.symbol.kotlin.KSClassDeclarationImpl.getDeclarations(KSClassDeclarationImpl.kt:178)	
	at com.google.devtools.ksp.common.symbolCollector.visitDeclarationContainer(IncrementalContextBase.kt:56)	
	at com.google.devtools.ksp.common.symbolCollector.visitDeclarationContainer(IncrementalContextBase.kt:34)	
	at com.google.devtools.ksp.visitor.KSDefaultVisitor.visitClassDeclaration(KSDefaultVisitor.kt:80)	
	at com.google.devtools.ksp.impl.symbol.kotlin.KSClassDeclarationImpl.accept(KSClassDeclarationImpl.kt:175)	
	at com.google.devtools.ksp.common.symbolCollector.visitDeclarationContainer(IncrementalContextBase.kt:57)	
	at com.google.devtools.ksp.common.symbolCollector.visitDeclarationContainer(IncrementalContextBase.kt:34)	
	at com.google.devtools.ksp.visitor.KSDefaultVisitor.visitFile(KSDefaultVisitor.kt:32)	
	at com.google.devtools.ksp.impl.symbol.kotlin.KSFileJavaImpl.accept(KSFileJavaImpl.kt:58)	
	at com.google.devtools.ksp.common.IncrementalContextBase.collectDefinedSymbols(IncrementalContextBase.kt:111)	
	at com.google.devtools.ksp.common.IncrementalContextBase.calcDirtyFiles(IncrementalContextBase.kt:218)	
	at com.google.devtools.ksp.impl.KotlinSymbolProcessing.execute(KotlinSymbolProcessing.kt:486)	
	at com.google.devtools.ksp.impl.KSPLoader$Companion.loadAndRunKSP(KSPLoader.kt:36)	
	at com.google.devtools.ksp.impl.KSPLoader.loadAndRunKSP(KSPLoader.kt)
@ZacSweers
Copy link
Contributor Author

Tried a few different attempted workarounds but no luck so far

  • change the property type to a typealias
  • change the type to an intermediary kotlin interface that extends the original type (i.e. interface KRunnable : Runnable)

@neetopia
Copy link
Contributor

potentially could be a same issue as #1930

tldr: when getting declarations of a class KSP queries analysis API for declared member scope, inside which AA will perform a serial of checks including checking for override e.t.c.. which has put an assertion for the symbols to be checked in resolved status, I am not certain about the cause of the status not being resolved though, since manually resolving does not seem to help, the check was added recently, I might need to talk to JB regarding this.

@ZacSweers
Copy link
Contributor Author

Possibly yeah. Looks like this also happens with functions

@ZacSweers
Copy link
Contributor Author

Also happens if the base class is an abstract or open class

@ZacSweers
Copy link
Contributor Author

Actually this seems to intermittently also happen with non-java classes too :/

@ZacSweers
Copy link
Contributor Author

I am able to get this working by just not using constructor parameter properties

abstract class BaseRepro(
  prefs: Runnable,
) : IRepro {
  override val prefs: Runnable = prefs
}

@neetopia
Copy link
Contributor

Actually this seems to intermittently also happen with non-java classes too :/

can I get a case for non-java classes?

@ZacSweers
Copy link
Contributor Author

Just another kotlin class in another module

@neetopia neetopia self-assigned this Jun 12, 2024
@neetopia neetopia added bug Something isn't working AA waiting for upstream fix labels Jun 12, 2024
@neetopia
Copy link
Contributor

My concern is this is more widespread than #1930 , I've created https://youtrack.jetbrains.com/issue/KT-69070 issue for this, feel free to add more context there if you feel the issue is related in your case.

@ZacSweers
Copy link
Contributor Author

Any luck with the above? With Kotlin 2.1 approaching, I'm worried about our ability to continue using KSP1, but this blocks KSP2 adoption

@ting-yuan ting-yuan assigned ting-yuan and unassigned neetopia Aug 2, 2024
@ting-yuan ting-yuan added the P1 major features or blocking bugs label Aug 2, 2024
@ting-yuan ting-yuan added this to the 2.1 milestone Aug 2, 2024
@ting-yuan
Copy link
Collaborator

I'm having a hard time reproducing it, even though that the stacktrace suggested that it happened before any processors run. @ZacSweers could you provide a mini gradle project?

@ZacSweers
Copy link
Contributor Author

I'll see what I can do, but also happy to hop on a call and pair if that's easier

@ting-yuan
Copy link
Collaborator

KSP 2.0.20-1.0.25 is out and contains the upstream fix. Would you mind to give it a try?

@ZacSweers
Copy link
Contributor Author

Looks like it's working for us in that version 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AA waiting for upstream fix bug Something isn't working P1 major features or blocking bugs
Projects
None yet
Development

No branches or pull requests

3 participants