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

Gradle transformJvmMainAtomicfu failed when using atomic() in companion obj #94

Closed
Him188 opened this issue Oct 10, 2019 · 4 comments
Closed

Comments

@Him188
Copy link

Him188 commented Oct 10, 2019

Caused by: java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0
        at org.objectweb.asm.Frame.a(Unknown Source)
        at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
        at org.objectweb.asm.tree.MethodNode.accept(Unknown Source)
        at kotlinx.atomicfu.transformer.AtomicFUTransformer$TransformerMV.visitEnd(AtomicFUTransformer.kt:611)
        at org.objectweb.asm.ClassReader.b(Unknown Source)
        at org.objectweb.asm.ClassReader.accept(Unknown Source)
        at org.objectweb.asm.ClassReader.accept(Unknown Source)
        at kotlinx.atomicfu.transformer.AtomicFUTransformer.transformFile(AtomicFUTransformer.kt:237)
        at kotlinx.atomicfu.transformer.AtomicFUTransformer.transform(AtomicFUTransformer.kt:172)
        at kotlinx.atomicfu.plugin.gradle.AtomicFUTransformTask.transform(AtomicFUGradlePlugin.kt:387)

This works fine

class Bot( ... ) {
    private val count = atomic<Int>(0)

    val id = count.getAndUpdate { it + 1 }
}

But this doesn't work

class Bot( ... ){
    val id = count.getAndUpdate { it + 1 }

    companion object {
      private val count = atomic<Int>(0)
    }
}

And after I removed private, I got a new exception Unsupported return within atomic operation

@Him188
Copy link
Author

Him188 commented Oct 10, 2019

Exceptions are the same when using atomic(0) instead of atomic<Int>(0)

@Him188 Him188 closed this as completed Oct 17, 2019
@elizarov
Copy link
Contributor

Have you found a workaround or an issue was resolved?

@Him188
Copy link
Author

Him188 commented Oct 17, 2019

@elizarov No.

@Him188
Copy link
Author

Him188 commented Oct 17, 2019

It might because count is private in the companion, accessing it from outside the companion object produces that issue.

I added a new fun to do getAndIncrease in the companion, which works well

companion object{
private val count = atomic(0)
fun nextCount() = count.getAndIncrease()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants