Skip to content

Commit

Permalink
Search all enclosing scopes inside lookup()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 14, 2021
1 parent e84cd47 commit 1f9e415
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/php/lang/ast/Result.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public function temp() {
* @return lang.ast.emit.Type
*/
public function lookup($type) {
if ('self' === $type || 'static' === $type || $type === $this->type[0]->name) {
if ('self' === $type || 'static' === $type) {
return new Declaration($this->type[0], $this);
} else if ('parent' === $type) {
return $this->lookup($this->type[0]->parent);
} else {
return new Reflection($type);
}

foreach ($this->type as $enclosing) {
if ($type === $enclosing->name) return new Declaration($enclosing, $this);
}

return new Reflection($type);
}
}
14 changes: 14 additions & 0 deletions src/test/php/lang/ast/unittest/emit/AnonymousClassTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,18 @@ public static function run() {
}');
Assert::instance($t, $t->getMethod('run')->invoke(null));
}

#[Test]
public function referencing_enclosing_class() {
$t= $this->type('class <T> {
const ID = 6100;
public static function run() {
return new class() extends self {
public static $id = <T>::ID;
};
}
}');
Assert::instance($t, $t->getMethod('run')->invoke(null));
}
}

0 comments on commit 1f9e415

Please sign in to comment.