{"record":{"id":"70e8131572faa5d6","repo":"apple/pkl","slug":"cannot-convert-pkl-base-int-value-to-kotlin-ush","errorCode":null,"errorMessage":"Cannot convert pkl.base#Int `$value` to kotlin.UShort because it is outside range `0...$max`","messagePattern":"Cannot convert pkl\\.base#Int `\\$value` to kotlin\\.UShort because it is outside range `0\\.\\.\\.\\$max`","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/mapper/KotlinConversions.kt","lineNumber":53,"sourceCode":"    }\n\n  val pIntToUInt: Conversion<Long, UInt> =\n    Conversion.of(PClassInfo.Int, UInt::class.java) { value, _ ->\n      val max = 0xFFFFFFFF // use literal instead of `UInt.MAX_VALUE.toLong()`\n      if (value < 0 || value > max) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.UInt \" +\n            \"because it is outside range `0...$max`\"\n        )\n      }\n      value.toUInt()\n    }\n\n  val pIntToUShort: Conversion<Long, UShort> =\n    Conversion.of(PClassInfo.Int, UShort::class.java) { value, _ ->\n      val max = 0xFFFF // use literal instead of `UShort.MAX_VALUE.toLong()`\n      if (value < 0 || value > max) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.UShort \" +\n            \"because it is outside range `0...$max`\"\n        )\n      }\n      value.toUShort()\n    }\n\n  val pIntToUByte: Conversion<Long, UByte> =\n    Conversion.of(PClassInfo.Int, UByte::class.java) { value, _ ->\n      val max = 0xFF // use literal instead of `UByte.MAX_VALUE.toLong()`\n      if (value < 0 || value > max) {\n        throw ConversionException(\n          \"Cannot convert pkl.base#Int `$value` to kotlin.UByte \" +\n            \"because it is outside range `0...$max`\"\n        )\n      }\n      value.toUByte()\n    }","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-kotlin/src/main/kotlin/org/pkl/config/kotlin/mapper/KotlinConversions.kt#L35-L71","documentation":"Range validation in KotlinConversions.pIntToUShort: a pkl.base#Int (64-bit) is converted to kotlin.UShort only if it fits 0..65535 (0xFFFF); out-of-range values throw ConversionException. The input at fault is any negative Long or Long greater than 65535 passed to a UShort-typed target.","triggerScenarios":"config.to<UShort>() on a Pkl Int outside 0..65535, e.g. port 70000 or a negative value.","commonSituations":"Network port or enum-like numeric codes configured out of the 16-bit range; negative sentinel values.","solutions":["Clamp or validate the Int value to 0..65535 before conversion.","Use a wider Kotlin type (Int/Long/UInt) if the value cannot fit UShort."],"exampleFix":"// before (Pkl)\nport: Int = 70000\n// after (Pkl)\nport: Int(isBetween(0, 65535)) = 8080","handlingStrategy":"validation","validationCode":"val v: Long = config[\"port\"]\nrequire(v in 0..65535) { \"port must fit in UShort (0..65535), got $v\" }","typeGuard":"fun Long.toUShortSafe(): UShort? = if (this in 0..65535) toUShort() else null","tryCatchPattern":"val port = try {\n  config.to<UShort>()\n} catch (e: ConversionException) {\n  0uS // or fail fast with context\n}","preventionTips":["Constrain Pkl properties: `port: Int(isBetween(0, 65535))`.","Validate network ports at config-authoring time with pkl eval.","Use UInt for values that might exceed 16 bits."],"tags":["kotlin","unsigned","range","conversion"],"backgroundTag":"value-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}