Skip to content

Commit

Permalink
[KT] Remove reliance on the origin to check if it's a call to `iterat…
Browse files Browse the repository at this point in the history
…or.hasNext()`

In K2, `IrStatementOrigin.FOR_LOOP_HAS_NEXT` origin is not set on the loop condition if it does not see a direct call to `iterator.hasNext()` function (case where the iterable overrides `iterator()` and returns a more specific type.)

PiperOrigin-RevId: 696872017
  • Loading branch information
jDramaix authored and copybara-github committed Nov 15, 2024
1 parent bc04f3a commit 399b6ef
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ private class LoopTransformer(
context.ir.symbols.iterable.getSimpleFunction("iterator")!!.owner
}

private val iteratorHasNextFunction: IrSimpleFunction by lazy {
context.ir.symbols.iterator.getSimpleFunction("hasNext")!!.owner
}

override fun visitBlock(expression: IrBlock): IrExpression {
// The psi2ir transformer wraps all `for` loop into an `IrBlock` with origin `FOR_LOOP`.
// After this check, we are sure that we are manipulating `while` and `do while` loop that has
Expand Down Expand Up @@ -216,7 +220,8 @@ private class LoopTransformer(
// call on the iterator.
if (
innerLoopBody.inductionVariableUpdate != null ||
(condition as? IrCall)?.origin != IrStatementOrigin.FOR_LOOP_HAS_NEXT
condition !is IrCall ||
!condition.symbol.owner.overrides(iteratorHasNextFunction)
) {
return null
}
Expand Down

0 comments on commit 399b6ef

Please sign in to comment.