Skip to content

Commit 618a5b9

Browse files
authored
Merge pull request #10427 from igfoo/igfoo/werror
Kotlin: Compile with -Werror, and fix warnings
2 parents a20b416 + 8a5bc3b commit 618a5b9

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

java/kotlin-extractor/build.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ def compile_to_dir(srcs, classpath, java_classpath, output):
8686
run_process([kotlinc,
8787
# kotlinc can default to 256M, which isn't enough when we are extracting the build
8888
'-J-Xmx2G',
89-
'-Xopt-in=kotlin.RequiresOptIn',
89+
'-Werror',
90+
'-opt-in=kotlin.RequiresOptIn',
9091
'-d', output,
9192
'-module-name', 'codeql-kotlin-extractor',
9293
'-no-reflect', '-no-stdlib',

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ open class KotlinFileExtractor(
16941694
result
16951695
}
16961696

1697-
private fun isFunction(target: IrFunction, pkgName: String, classNameLogged: String, classNamePredicate: (String) -> Boolean, fName: String, hasQuestionMark: Boolean? = false): Boolean {
1697+
private fun isFunction(target: IrFunction, pkgName: String, classNameLogged: String, classNamePredicate: (String) -> Boolean, fName: String, isNullable: Boolean? = false): Boolean {
16981698
val verbose = false
16991699
fun verboseln(s: String) { if(verbose) println(s) }
17001700
verboseln("Attempting match for $pkgName $classNameLogged $fName")
@@ -1704,14 +1704,14 @@ open class KotlinFileExtractor(
17041704
}
17051705
val extensionReceiverParameter = target.extensionReceiverParameter
17061706
val targetClass = if (extensionReceiverParameter == null) {
1707-
if (hasQuestionMark == true) {
1707+
if (isNullable == true) {
17081708
verboseln("Nullablility of type didn't match (target is not an extension method)")
17091709
return false
17101710
}
17111711
target.parent
17121712
} else {
17131713
val st = extensionReceiverParameter.type as? IrSimpleType
1714-
if (hasQuestionMark != null && st?.hasQuestionMark != hasQuestionMark) {
1714+
if (isNullable != null && st?.isNullable() != isNullable) {
17151715
verboseln("Nullablility of type didn't match")
17161716
return false
17171717
}
@@ -1738,8 +1738,8 @@ open class KotlinFileExtractor(
17381738
return true
17391739
}
17401740

1741-
private fun isFunction(target: IrFunction, pkgName: String, className: String, fName: String, hasQuestionMark: Boolean? = false) =
1742-
isFunction(target, pkgName, className, { it == className }, fName, hasQuestionMark)
1741+
private fun isFunction(target: IrFunction, pkgName: String, className: String, fName: String, isNullable: Boolean? = false) =
1742+
isFunction(target, pkgName, className, { it == className }, fName, isNullable)
17431743

17441744
private fun isNumericFunction(target: IrFunction, fName: String): Boolean {
17451745
return isFunction(target, "kotlin", "Int", fName) ||
@@ -2582,8 +2582,8 @@ open class KotlinFileExtractor(
25822582
indexVarDecl.initializer?.let { indexVarInitializer ->
25832583
(e.statements[2] as? IrCall)?.let { arraySetCall ->
25842584
if (isFunction(arraySetCall.symbol.owner, "kotlin", "(some array type)", { isArrayType(it) }, "set")) {
2585-
val updateRhs = arraySetCall.getValueArgument(1)
2586-
if (updateRhs == null) {
2585+
val updateRhs0 = arraySetCall.getValueArgument(1)
2586+
if (updateRhs0 == null) {
25872587
logger.errorElement("Update RHS not found", e)
25882588
return false
25892589
}
@@ -2596,7 +2596,7 @@ open class KotlinFileExtractor(
25962596
receiverVal -> receiverVal.symbol.owner == arrayVarDecl.symbol.owner
25972597
} ?: false
25982598
},
2599-
updateRhs
2599+
updateRhs0
26002600
)?.let { updateRhs ->
26012601
val origin = e.origin
26022602
if (origin == null) {
@@ -3421,15 +3421,11 @@ open class KotlinFileExtractor(
34213421

34223422
data class ReceiverInfo(val receiver: IrExpression, val type: IrType, val field: Label<DbField>, val indexOffset: Int)
34233423

3424-
private fun makeReceiverInfo(callableReferenceExpr: IrCallableReference<out IrSymbol>, receiver: IrExpression?, indexOffset: Int): ReceiverInfo? {
3424+
private fun makeReceiverInfo(receiver: IrExpression?, indexOffset: Int): ReceiverInfo? {
34253425
if (receiver == null) {
34263426
return null
34273427
}
34283428
val type = receiver.type
3429-
if (type == null) {
3430-
logger.warnElement("Receiver has no type", callableReferenceExpr)
3431-
return null
3432-
}
34333429
val field: Label<DbField> = tw.getFreshIdLabel()
34343430
return ReceiverInfo(receiver, type, field, indexOffset)
34353431
}
@@ -3442,8 +3438,8 @@ open class KotlinFileExtractor(
34423438
: GeneratedClassHelper(locId, ids) {
34433439

34443440
// Only one of the receivers can be non-null, but we defensively handle the case when both are null anyway
3445-
private val dispatchReceiverInfo = makeReceiverInfo(callableReferenceExpr, callableReferenceExpr.dispatchReceiver, 0)
3446-
private val extensionReceiverInfo = makeReceiverInfo(callableReferenceExpr, callableReferenceExpr.extensionReceiver, if (dispatchReceiverInfo == null) 0 else 1)
3441+
private val dispatchReceiverInfo = makeReceiverInfo(callableReferenceExpr.dispatchReceiver, 0)
3442+
private val extensionReceiverInfo = makeReceiverInfo(callableReferenceExpr.extensionReceiver, if (dispatchReceiverInfo == null) 0 else 1)
34473443

34483444
fun extractReceiverField() {
34493445
val firstAssignmentStmtIdx = 1

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ open class KotlinUsesExtractor(
327327
return f
328328
return globalExtensionState.syntheticToRealFunctionMap.getOrPut(f) {
329329
val result = replacementClass.declarations.findSubType<IrSimpleFunction> { replacementDecl ->
330-
replacementDecl is IrSimpleFunction && replacementDecl.name == f.name && replacementDecl.valueParameters.size == f.valueParameters.size && replacementDecl.valueParameters.zip(f.valueParameters).all {
330+
replacementDecl.name == f.name && replacementDecl.valueParameters.size == f.valueParameters.size && replacementDecl.valueParameters.zip(f.valueParameters).all {
331331
erase(it.first.type) == erase(it.second.type)
332332
}
333333
}
@@ -650,7 +650,7 @@ open class KotlinUsesExtractor(
650650
otherIsPrimitive: Boolean,
651651
javaClass: IrClass,
652652
kotlinPackageName: String, kotlinClassName: String): TypeResults {
653-
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.hasQuestionMark && primitiveName != null) {
653+
val javaResult = if ((context == TypeContext.RETURN || (context == TypeContext.OTHER && otherIsPrimitive)) && !s.isNullable() && primitiveName != null) {
654654
val label: Label<DbPrimitive> = tw.getLabelFor("@\"type;$primitiveName\"", {
655655
tw.writePrimitives(it, primitiveName)
656656
})
@@ -660,7 +660,7 @@ open class KotlinUsesExtractor(
660660
}
661661
val kotlinClassId = useClassInstance(kotlinClass, listOf()).typeResult.id
662662
val kotlinResult = if (true) TypeResult(fakeKotlinType(), "TODO", "TODO") else
663-
if (s.hasQuestionMark) {
663+
if (s.isNullable()) {
664664
val kotlinSignature = "$kotlinPackageName.$kotlinClassName?" // TODO: Is this right?
665665
val kotlinLabel = "@\"kt_type;nullable;$kotlinPackageName.$kotlinClassName\""
666666
val kotlinId: Label<DbKt_nullable_type> = tw.getLabelFor(kotlinLabel, {
@@ -704,13 +704,13 @@ open class KotlinUsesExtractor(
704704
owner is IrClass -> {
705705
val args = if (s.isRawType()) null else s.arguments
706706

707-
return useSimpleTypeClass(owner, args, s.hasQuestionMark)
707+
return useSimpleTypeClass(owner, args, s.isNullable())
708708
}
709709
owner is IrTypeParameter -> {
710710
val javaResult = useTypeParameter(owner)
711711
val aClassId = makeClass("kotlin", "TypeParam") // TODO: Wrong
712712
val kotlinResult = if (true) TypeResult(fakeKotlinType(), "TODO", "TODO") else
713-
if (s.hasQuestionMark) {
713+
if (s.isNullable()) {
714714
val kotlinSignature = "${javaResult.signature}?" // TODO: Wrong
715715
val kotlinLabel = "@\"kt_type;nullable;type_param\"" // TODO: Wrong
716716
val kotlinId: Label<DbKt_nullable_type> = tw.getLabelFor(kotlinLabel, {
@@ -1485,7 +1485,7 @@ open class KotlinUsesExtractor(
14851485
if (t.isArray() || t.isNullableArray()) {
14861486
val elementType = t.getArrayElementType(pluginContext.irBuiltIns)
14871487
val erasedElementType = erase(elementType)
1488-
return owner.typeWith(erasedElementType).codeQlWithHasQuestionMark(t.hasQuestionMark)
1488+
return owner.typeWith(erasedElementType).codeQlWithHasQuestionMark(t.isNullable())
14891489
}
14901490

14911491
return if (t.arguments.isNotEmpty())

java/kotlin-extractor/src/main/kotlin/utils/TypeSubstitution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private fun IrSimpleType.substituteTypeArguments(substitutionMap: Map<IrTypePara
5656

5757
return IrSimpleTypeImpl(
5858
classifier,
59-
hasQuestionMark,
59+
isNullable(),
6060
newArguments,
6161
annotations
6262
)
@@ -92,7 +92,7 @@ private fun subProjectedType(substitutionMap: Map<IrTypeParameterSymbol, IrTypeA
9292
if (conflictingVariance(outerVariance, substitutedTypeArg.variance))
9393
IrStarProjectionImpl
9494
else {
95-
val newProjectedType = substitutedTypeArg.type.let { if (t.hasQuestionMark) it.codeQlWithHasQuestionMark(true) else it }
95+
val newProjectedType = substitutedTypeArg.type.let { if (t.isNullable()) it.codeQlWithHasQuestionMark(true) else it }
9696
val newVariance = combineVariance(outerVariance, substitutedTypeArg.variance)
9797
makeTypeProjection(newProjectedType, newVariance)
9898
}
@@ -196,7 +196,7 @@ fun IrTypeArgument.withQuestionMark(b: Boolean): IrTypeArgument =
196196
is IrStarProjection -> this
197197
is IrTypeProjection ->
198198
this.type.let { when(it) {
199-
is IrSimpleType -> if (it.hasQuestionMark == b) this else makeTypeProjection(it.codeQlWithHasQuestionMark(b), this.variance)
199+
is IrSimpleType -> if (it.isNullable() == b) this else makeTypeProjection(it.codeQlWithHasQuestionMark(b), this.variance)
200200
else -> this
201201
}}
202202
else -> this

0 commit comments

Comments
 (0)