Skip to content

Commit e06df02

Browse files
homurollSpace Team
authored andcommitted
[K/N] Fixed a corner case in casts optimization pass
The alogrithm handles differently two types of variables - T? and Boolean. The first type is used in expressions like if (var == null) { .. } or if (var != null) { .. }, while the other is used in just if (variable) { .. } or if (!variable) { .. }. But in one case, a variable might change its type - when a Boolean? variable gets aliased to a Boolean variable. This wasn't expected and there was an assertion for such a case (a variable can't change its kind). This commit fixes this by supporting this peculiar corner case. #KT-81257 Fixed
1 parent 8ee4fb9 commit e06df02

File tree

1 file changed

+7
-1
lines changed
  • kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations

1 file changed

+7
-1
lines changed

kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/CastsOptimization.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,13 @@ internal class CastsOptimization(val context: Context) : BodyLoweringPass {
642642
)
643643
}
644644
is VariableValue.NullablePredicate -> variableValue.predicate
645-
is VariableValue.BooleanPredicate -> error("Unexpected boolean predicate for ${variable.render()}")
645+
is VariableValue.BooleanPredicate -> {
646+
// Happens when a bool? variable aliases to a bool variable.
647+
NullablePredicate(
648+
ifNull = Predicate.False, // Never happens.
649+
ifNotNull = Predicate.Empty
650+
)
651+
}
646652
}
647653

648654
fun buildBooleanPredicate(variable: IrValueDeclaration): BooleanPredicate =

0 commit comments

Comments
 (0)