Skip to content

Commit

Permalink
Fix promotion for by-reference arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Apr 4, 2020
1 parent b959e97 commit 285086e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ XP Compiler ChangeLog

## ?.?.? / ????-??-??

## 5.1.2 / 2020-04-04

* Fixed promotion for by-reference arguments - @thekid

## 5.1.1 / 2020-03-29

* Fixed ternary and instanceof operators' precedence - @thekid
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/lang/ast/emit/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ protected function emitMethod($result, $method) {
foreach ($method->signature->parameters as $param) {
if (isset($param->promote)) {
$declare.= $param->promote.' $'.$param->name.';';
$promote.= '$this->'.$param->name.'= $'.$param->name.';';
$promote.= '$this->'.$param->name.'= '.($param->reference ? '&$' : '$').$param->name.';';
$result->meta[0][self::PROPERTY][$param->name]= [
DETAIL_RETURNS => $param->type ? $param->type->name() : 'var',
DETAIL_ANNOTATIONS => [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ public function run() {
Assert::equals('tested', $r);
}

#[@test]
public function parameter_accessible() {
$r= $this->run('class <T> {
public function __construct(private $id= "test") {
if (null === $id) {
throw new \\lang\\IllegalArgumentException("ID not set");
}
}
public function run() {
return $this->id;
}
}');
Assert::equals('test', $r);
}

#[@test]
public function in_method() {
$r= $this->run('class <T> {
Expand Down Expand Up @@ -85,4 +101,20 @@ public function __construct(public string $name, string $initial= null) {
Assert::equals(['name'], array_map(function($f) { return $f->getName(); }, $t->getFields()));
Assert::equals('Timm J.', $t->newInstance('Timm', 'J')->name);
}

#[@test]
public function promoted_by_reference_argument() {
$t= $this->type('class <T> {
public function __construct(public array &$list) { }
public static function test() {
$list= [1, 2, 3];
$self= new self($list);
$list[]= 4;
return $self->list;
}
}');

Assert::equals([1, 2, 3, 4], $t->getMethod('test')->invoke(null, []));
}
}

0 comments on commit 285086e

Please sign in to comment.