IR Scripting: allow to specify nullable types for provided props...

but only explicitly. This does not fix a breaking change described in
#KT-52120, because it seems the correct behavior, but it allows
to "workaround" the problem by specifying nullability explicitly.
Also improve handling of nullable bindings in JSR-223.
#KT-49173 fixed
#KT-51213 fixed
This commit is contained in:
Ilya Chernikov
2022-04-22 15:02:18 +02:00
committed by teamcity
parent 4a66cd0c69
commit 49902bb851
8 changed files with 82 additions and 16 deletions
@@ -14,31 +14,37 @@ import kotlin.reflect.KType
*/
class KotlinType private constructor(
val typeName: String,
@Transient val fromClass: KClass<*>? = null
@Transient val fromClass: KClass<*>?,
val isNullable: Boolean
// TODO: copy properties from KType
) : Serializable {
/**
* Constructs KotlinType from fully-qualified [qualifiedTypeName] in a dot-separated form, e.g. "org.acme.Outer.Inner"
*/
constructor(qualifiedTypeName: String) : this(qualifiedTypeName, null)
@JvmOverloads
constructor(qualifiedTypeName: String, isNullable: Boolean = false)
: this(qualifiedTypeName.removeSuffix("?"), null, isNullable = isNullable || qualifiedTypeName.endsWith('?'))
/**
* Constructs KotlinType from reflected [kclass]
*/
constructor(kclass: KClass<*>) : this(kclass.java.name, kclass)
@JvmOverloads
constructor(kclass: KClass<*>, isNullable: Boolean = false) : this(kclass.java.name, kclass, isNullable)
// TODO: implement other approach for non-class types
/**
* Constructs KotlinType from reflected [ktype]
* Constructs KotlinType from reflected [type]
*/
constructor(type: KType) : this(type.classifier as KClass<*>)
constructor(type: KType) : this(type.classifier as KClass<*>, type.isMarkedNullable)
override fun equals(other: Any?): Boolean =
(other as? KotlinType)?.let { typeName == it.typeName } == true
(other as? KotlinType)?.let { typeName == it.typeName && isNullable == it.isNullable } == true
override fun hashCode(): Int = typeName.hashCode()
override fun hashCode(): Int = typeName.hashCode() + 31 * isNullable.hashCode()
fun withNullability(isNullable: Boolean): KotlinType = KotlinType(typeName, fromClass, isNullable)
companion object {
private const val serialVersionUID: Long = 1L
private const val serialVersionUID: Long = 2L
}
}
}
@@ -261,6 +261,10 @@ obj
put("boundValue", 100)
})
Assert.assertEquals(111, result2)
engine.put("nullable", null)
val result3 = engine.eval("bindings[\"nullable\"]?.let { it as Int } ?: -1")
Assert.assertEquals(-1, result3)
}
@Test
@@ -280,6 +284,10 @@ obj
put("boundValue", 100)
})
Assert.assertEquals(111, result2)
engine.put("nullable", null)
val result3 = engine.eval("nullable?.let { it as Int } ?: -1")
Assert.assertEquals(-1, result3)
}
@Test
@@ -223,6 +223,55 @@ class ScriptingHostTest : TestCase() {
Assert.assertEquals(greeting, output)
}
@Test
fun testProvidedPropertiesNullability() {
val stringType = KotlinType(String::class)
val definition = createJvmScriptDefinitionFromTemplate<SimpleScriptTemplate>(
compilation = {
providedProperties(
"notNullable" to stringType,
"nullable" to stringType.withNullability(true)
)
},
evaluation = {
providedProperties(
"notNullable" to "something",
"nullable" to null
)
}
)
val defaultEvalConfig = definition.evaluationConfiguration
val notNullEvalConfig = defaultEvalConfig.with {
providedProperties("nullable" to "!")
}
val wrongNullEvalConfig = defaultEvalConfig.with {
providedProperties("notNullable" to null)
}
with(BasicJvmScriptingHost()) {
// compile time
val comp0 = runBlocking {
compiler("nullable.length".toScriptSource(), definition.compilationConfiguration)
}
assertTrue(comp0 is ResultWithDiagnostics.Failure)
val errors = comp0.reports.filter { it.severity == ScriptDiagnostic.Severity.ERROR }
assertTrue( errors.any { it.message == "Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?" })
// runtime
fun evalWith(evalConfig: ScriptEvaluationConfiguration) =
eval("notNullable+(nullable ?: \"0\")".toScriptSource(), definition.compilationConfiguration, evalConfig).valueOrThrow().returnValue
val ret0 = evalWith(defaultEvalConfig)
assertEquals("something0", (ret0 as? ResultValue.Value)?.value)
val ret1 = evalWith(notNullEvalConfig)
assertEquals("something!", (ret1 as? ResultValue.Value)?.value)
val ret2 = evalWith(wrongNullEvalConfig)
assertTrue((ret2 as? ResultValue.Error)?.error is java.lang.NullPointerException)
}
}
@Test
fun testDiamondImportWithoutSharing() {
val greeting = listOf("Hi from common", "Hi from middle", "Hi from common", "sharedVar == 3")
@@ -20,10 +20,10 @@ fun configureProvidedPropertiesFromJsr223Context(context: ScriptConfigurationRef
}
for ((k, v) in allBindings) {
// only adding bindings that are not already defined and also skip local classes
if (!updatedProperties.containsKey(k) && v::class.qualifiedName != null) {
if (!updatedProperties.containsKey(k) && (v == null || v::class.qualifiedName != null)) {
// TODO: add only valid names
// TODO: find out how it's implemented in other jsr223 engines for typed languages, since this approach prevent certain usage scenarios, e.g. assigning back value of a "sibling" type
updatedProperties[k] = KotlinType(v::class)
updatedProperties[k] = if (v == null) KotlinType(Any::class, isNullable = true) else KotlinType(v::class)
}
}
ScriptCompilationConfiguration(context.compilationConfiguration) {