https://kotlinlang.org logo
Title
k

Kroppeb

05/29/2019, 10:57 PM
I have an issue with the following piece of code:
inline fun StringReader.tryTo(block: StringReader.TryTo.() -> Unit){
	contract {
		callsInPlace(block, kotlin.contracts.InvocationKind.EXACTLY_ONCE)
	}
	try {
		it.block()
	} catch (e: Exception) {
		it.reset()
	}
}
And
override fun parse(reader: StringReader): String? {
	reader.tryTo {
		throw Exception("hi")
	}
	return null // Marked as unreachable code
}
On a inline lambda seems to not realise that an exception that is thrown in the lambda can be caught in the inlined function. This gets even worse when the return is not nullable and the user doesn't add a return as it is marked as unreachable and as a result at runtime the function will return null.
Thinking about it, this might be my fault. I should throw the exception again or mark it as
AT_MOST_ONCE
.
Otherwise I might get partially initialised values.
k

karelpeeters

05/29/2019, 11:05 PM
Either it's a bug or the requirements of
callsInPlace
need to be clarified, there's nothing in the docs. Definitely report this on YouTrack.
k

Kroppeb

05/29/2019, 11:09 PM
Ok
k

karelpeeters

05/29/2019, 11:10 PM
And post the link because now I'm interested 😒imple_smile:
k

Kroppeb

05/29/2019, 11:12 PM
Found this in similar issues: https://youtrack.jetbrains.com/issue/KT-26523
If this problem occurs only with runCatching I'd say that we should remove contract in 1.3
Maybe someone should have actually tested this =P
Can you add a related issue?
k

karelpeeters

05/29/2019, 11:34 PM
If you click the "open command dialog" button on the top left you can type "relates to <id>", I'm not sure if that actually works though, I've never tried that.
k

Kroppeb

05/30/2019, 8:54 AM
It gave a msg like you are not allowed to or something
k

karelpeeters

05/30/2019, 10:35 AM
Makes sense, we aren't allowed to edit most properties of the issues.