Skip to content

Commit

Permalink
Fixup trait methods even if no traits are used
Browse files Browse the repository at this point in the history
Trait methods might be non-trivially inherited, in which case we
may have to perform fixup in classes that do not directly use any
traits.
  • Loading branch information
nikic committed Nov 5, 2020
1 parent 33969c2 commit 2effbfd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 2 additions & 6 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -4190,16 +4190,12 @@ static int preload_optimize(zend_persistent_script *script)
}

ZEND_HASH_FOREACH_PTR(&script->script.class_table, ce) {
if (ce->num_traits) {
preload_fix_trait_methods(ce);
}
preload_fix_trait_methods(ce);
} ZEND_HASH_FOREACH_END();

ZEND_HASH_FOREACH_PTR(preload_scripts, script) {
ZEND_HASH_FOREACH_PTR(&script->script.class_table, ce) {
if (ce->num_traits) {
preload_fix_trait_methods(ce);
}
preload_fix_trait_methods(ce);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FOREACH_END();

Expand Down
14 changes: 14 additions & 0 deletions ext/opcache/tests/preload_trait_multiple_fixup.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ class C1 {
class C2 extends C1 {
use T2;
}

trait T3 {
public function method() {
// Prevent trivial inheritance.
static $x;
// Needs to be optimized somehow.
$str = "Foo";
echo "$str\n";
}
}
class C3 {
use T3;
}
class C4 extends C3 {}
2 changes: 2 additions & 0 deletions ext/opcache/tests/preload_trait_multiple_fixup.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ if (PHP_OS_FAMILY == 'Windows') die('skip Preloading is not supported on Windows
--FILE--
<?php
(new C2)->method();
(new C4)->method();
?>
--EXPECT--
Foo
Foo

0 comments on commit 2effbfd

Please sign in to comment.