Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compiler assumptions about self/etc wrt closures
* Don't throw an error if self/parent/static are used in a closure (outside a class). * Don't propagate self:: constants into closures * Use runtime fetch for self::class in closures Fixes bug #66811.
- Loading branch information
Showing
5 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
Bug #66811: Cannot access static::class in lambda, writen outside of a class | ||
--FILE-- | ||
<?php | ||
class A { | ||
public static function f() { | ||
return function () { | ||
var_dump(self::class); | ||
var_dump(static::class); | ||
}; | ||
} | ||
} | ||
|
||
class B extends A {} | ||
|
||
$f = B::f(); | ||
$f(); | ||
|
||
$g = $f->bindTo(null, A::class); | ||
$g(); | ||
|
||
$foo = function () { | ||
var_dump(self::class); | ||
var_dump(static::class); | ||
}; | ||
|
||
$bar = $foo->bindTo(null, A::class); | ||
$bar(); | ||
|
||
?> | ||
--EXPECT-- | ||
string(1) "A" | ||
string(1) "B" | ||
string(1) "A" | ||
string(1) "A" | ||
string(1) "A" | ||
string(1) "A" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
self:: class constants should not be propagated into closures, due to scope rebinding | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
const C = 'A::C'; | ||
|
||
public function f() { | ||
return function() { | ||
return self::C; | ||
}; | ||
} | ||
} | ||
|
||
class B { | ||
const C = 'B::C'; | ||
} | ||
|
||
$f = (new A)->f(); | ||
var_dump($f->bindTo(null, 'B')()); | ||
|
||
?> | ||
--EXPECT-- | ||
string(4) "B::C" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
--TEST-- | ||
Return type of self is not allowed in closure | ||
Return type of self is allowed in closure | ||
|
||
--FILE-- | ||
<?php | ||
|
||
$c = function(): self {}; | ||
$c = function(): self { return $this; }; | ||
var_dump($c->call(new stdClass)); | ||
|
||
--EXPECTF-- | ||
Fatal error: Cannot use "self" when no class scope is active in %s on line 3 | ||
--EXPECT-- | ||
object(stdClass)#2 (0) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
--TEST-- | ||
Return type of parent is not allowed in closure | ||
Return type of parent is allowed in closure | ||
--FILE-- | ||
<?php | ||
|
||
$c = function(): parent {}; | ||
class A {} | ||
class B extends A {} | ||
|
||
$c = function(parent $x): parent { return $x; }; | ||
var_dump($c->bindTo(null, 'B')(new A)); | ||
|
||
--EXPECTF-- | ||
Fatal error: Cannot use "parent" when no class scope is active in %s on line 3 | ||
object(A)#%d (0) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters