Skip to content

Commit 10c4ef6

Browse files
committed
Fix closures not being able to use by reference
1 parent 2cdf57b commit 10c4ef6

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
* Fixed closures not being able to use by reference - @thekid
67
* Implemented parameter annotations via `$param: inject` - @thekid
78

89
## 0.6.0 / 2017-10-15

src/main/php/lang/ast/Parse.class.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,12 @@ private function func($name, $modifiers) {
731731
$this->token= $this->advance();
732732
$use= [];
733733
while (')' !== $this->token->symbol->id) {
734-
$use[]= '$'.$this->token->value;
734+
if ('&' === $this->token->value) {
735+
$this->token= $this->advance();
736+
$use[]= '&$'.$this->token->value;
737+
} else {
738+
$use[]= '$'.$this->token->value;
739+
}
735740
$this->token= $this->advance();
736741
if (')' === $this->token->symbol->id) break;
737742
$this->token= $this->expect(',');

src/test/php/lang/ast/unittest/parse/FunctionsTest.class.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,23 @@ public function default_closure() {
9292
}
9393

9494
#[@test]
95-
public function default_closure_with_use() {
95+
public function default_closure_with_use_by_vaue() {
9696
$block= ['+' => [['(variable)' => 'a'], ['(literal)' => '1']]];
9797
$this->assertNodes(
9898
[['(' => [null, [], [], [['return' => $block]], null, ['$a', '$b']]]],
9999
$this->parse('function() use($a, $b) { return $a + 1; };')
100100
);
101101
}
102102

103+
#[@test]
104+
public function default_closure_with_use_by_reference() {
105+
$block= ['+' => [['(variable)' => 'a'], ['(literal)' => '1']]];
106+
$this->assertNodes(
107+
[['(' => [null, [], [], [['return' => $block]], null, ['$a', '&$b']]]],
108+
$this->parse('function() use($a, &$b) { return $a + 1; };')
109+
);
110+
}
111+
103112
#[@test]
104113
public function short_closure() {
105114
$block= ['+' => [['(variable)' => 'a'], ['(literal)' => '1']]];

0 commit comments

Comments
 (0)