Skip to content

[rubysrc2cpg] Change for-loop lowering#6023

Open
TNSelahle wants to merge 8 commits into
masterfrom
tebogo/rubysrc2cpg-for-lowering
Open

[rubysrc2cpg] Change for-loop lowering#6023
TNSelahle wants to merge 8 commits into
masterfrom
tebogo/rubysrc2cpg-for-lowering

Conversation

@TNSelahle

Copy link
Copy Markdown
Member
  • Ruby's for-loop lowering was changed from C-syntax style to a call to __core.Array.each

As an example,

arr = [1, 2]
for i in arr
  puts i
end

is now lowered as

arr = [1, 2]
arr.each do |i|
  puts i
end

@TNSelahle TNSelahle requested a review from ml86 June 12, 2026 10:50
Comment on lines +484 to +485
base.argumentIndex shouldBe 0
base.name shouldBe "x"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is not correct. There should be some sort of range object representing (1..x).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct. The test case had 2 separate for loops. I've split them up for better readability now.

Comment on lines +491 to +495
inside(cpg.method("foo1").astChildren.collectAll[Method].isLambda.l) { case closureMethod :: Nil =>
inside(closureMethod.parameter.indexGt(0).l) { case iParam :: Nil =>
iParam.name shouldBe "i"
}

closureMethod.call.nameExact("puts").size shouldBe 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be a captured variable for x. Test for it.

Comment on lines +510 to +514
inside(cpg.method("foo2").astChildren.collectAll[Method].isLambda.l) { case closureMethod :: Nil =>
inside(closureMethod.parameter.indexGt(0).l) { case iParam :: Nil =>
iParam.name shouldBe "i"
}
closureMethod.call.nameExact("puts").size shouldBe 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again missing the test for the captured variable.

Comment on lines +737 to +742
inside(cpg.method.isModule.astChildren.collectAll[Method].isLambda.l) { case closureMethod :: Nil =>
inside(closureMethod.parameter.indexGt(0).l) { case numParam :: Nil =>
numParam.name shouldBe "num"
}
val List(putsCall) = closureMethod.call.nameExact("puts").l
putsCall.code shouldBe "puts num"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again captured variable test.

Comment on lines 471 to 476
|def foo2
| x = 3
| for i in 1..x do
| puts x + i
| end
|end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That test code seem to be not needed anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants