Weird bug with inline functions

This is a minimal reproducible example.

import java.lang.IllegalStateException // Line 1

object BadLauncher {

	class A() {

	}

	inline fun A.b() {
		throw IllegalStateException()
	}

	@JvmStatic
	fun main(args:Array<String>) {
		A().b()
	}} // Line 16

Exceptions with:
Exception in thread “main” java.lang.IllegalStateException
at BadLauncher.main(main.kt:17)

There is no 17th line in this program. The stacktrace doesn’t seem to know where the exception came from.

It’s even weirder like this:
import java.lang.IllegalStateException // Line 1

object BadLauncher {

	class A() {

	}

	inline fun A.b() {
		throw IllegalStateException()
	}

	@JvmStatic
	fun main(args:Array<String>) {
		A().b()
	}}

// I'm
// a
// little
// teapot // Line 21

Exceptions with:
Exception in thread “main” java.lang.IllegalStateException
at dev.fluffyderg.velandia.desktop.BadLauncher.main(BadLauncher.kt:22)
It seems to just point to the end of the entire file no matter if it is in run or debug mode…

This is pretty annoying as i’m making use of this feature in more complicated functions and the stack trace will not take me to where the exceptions are originating from.

One such example is implementing the non-nullable exception throwing versions of min() and max() that were talked about during the Kotlin 1.4 presentations (rather than the minOrNull() and maxOrNull() methods which were the original min() and max() functions in 1.3). These functions are inline in the standard library _Collections.kt class.

This is not a bug but an intended feature. inline functions have special line numbers outside of the file. This is necessary to support both the line number within the inline function as well as where the inline function is called from.
Intellij Idea has a tool to resolve those stacktraces. I personally use idea so I don’t know what standalone tools exist if you don’t want to use idea but I’m sure there is one.

1 Like

I am using Idea, I tried both DCEVM 11 Runtime, OpenJDK 14 Runtime, and the included Jetbrains Java 11 runtime and they all have the same error. I can see Idea has options to go to the inline function body and call site but they both do not do anything when clicked.

1 Like

strange… all of a sudden the stacktrace tool started working after i tried a real test with a lambda parameter, compiling and then reverting. I probably forgot to compile after switching runtimes or something.

Yeah i’m pretty sure it has something to do with DCEVM 11 (a runtime for hot swapping classes at runtime). Afrter compiling with IDEA-included Runtime it the tool seems to be working

1 Like

We have all been there. I just love it when I can get something to work ask someone else for help and they do the exact same thing I tried but it works for them. Sometimes all you need to do is ask someone else and everything works like magic :wink:

2 Likes