Skip to content

Commit

Permalink
add test and make sure that we evaluate the receiver fist in OCASTTra…
Browse files Browse the repository at this point in the history
…nslator>>#emitToDo:step:

fixes pharo-project#16927
  • Loading branch information
MarcusDenker committed Jul 26, 2024
1 parent 47b6b7d commit c3c5c9b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/OpalCompiler-Core/OCASTTranslator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ OCASTTranslator >> emitToDo: aMessageNode step: step [
block := aMessageNode arguments last.
iterator := block arguments first variable.


"evaluate the receiver first"
self visitNode: aMessageNode receiver.
iterator emitStore: methodBuilder.

"now the limit argument"
limitEmit := [self visitNode: limit].
"if the limit is not just a literal or a non-writable variable, make a temp store it there"
(limit isLiteralNode or: [limit isVariable and: [limit variable isWritable not]]) ifFalse: [
Expand All @@ -381,8 +387,7 @@ OCASTTranslator >> emitToDo: aMessageNode step: step [
limitEmit := [methodBuilder pushTemp: ('0limit_', iterator name)]].

"push start. allocate and initialize iterator"
self visitNode: aMessageNode receiver.
iterator emitStore: methodBuilder.

methodBuilder jumpBackTarget: #start.
iterator emitValue: methodBuilder.
limitEmit value.
Expand Down
9 changes: 9 additions & 0 deletions src/OpalCompiler-Tests/OCCompilerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ OCCompilerTest >> testNoShadowing [
^nil ]
]

{ #category : 'tests' }
OCCompilerTest >> testOptimizedToDo [
| stream results |
results := OrderedCollection new.
stream := #( 1 5 ) readStream.
stream next to: stream next do: [ :i | results add: i ].
self assertCollection: results hasSameElements: #(1 2 3 4 5)
]

{ #category : 'tests - shadowing' }
OCCompilerTest >> testPseudoVariableShadowing [

Expand Down

0 comments on commit c3c5c9b

Please sign in to comment.