-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #74872 - Disambiguate the source of a trait alias when possible #2616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,59 @@ | ||
--TEST-- | ||
Bug #74872 (First Trait wins on importing same methods with diff alias) | ||
--FILE-- | ||
<?php | ||
|
||
trait Trait1 | ||
{ | ||
public function init() | ||
{ | ||
echo("Trait1 - init\n"); | ||
} | ||
} | ||
|
||
trait Trait2 | ||
{ | ||
public function init() | ||
{ | ||
echo("Trait2 - init\n"); | ||
} | ||
} | ||
|
||
class Test | ||
{ | ||
use Trait1 { | ||
init as public method1trait1; | ||
} | ||
|
||
use Trait2 { | ||
init as public method1trait2; | ||
} | ||
|
||
final public function __construct() | ||
{ | ||
$this->init(); | ||
$this->method1trait1(); | ||
$this->method1trait2(); | ||
} | ||
|
||
public function init() | ||
{ | ||
echo("Test - init\n"); | ||
} | ||
} | ||
|
||
$test = new Test(); | ||
|
||
$reflection = new ReflectionClass( $test ); | ||
|
||
foreach($reflection->getTraitAliases() as $k => $v) | ||
{ | ||
echo $k.' => '.$v."\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
Test - init | ||
Trait1 - init | ||
Trait2 - init | ||
method1trait1 => Trait1::init | ||
method1trait2 => Trait2::init |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
|
@@ -1192,15 +1192,10 @@ static void zend_add_trait_method(zend_class_entry *ce, const char *name, zend_s | |
return; | ||
} else if (UNEXPECTED(existing_fn->common.scope->ce_flags & ZEND_ACC_TRAIT)) { | ||
/* two traits can't define the same non-abstract method */ | ||
#if 1 | ||
zend_error_noreturn(E_COMPILE_ERROR, "Trait method %s has not been applied, because there are collisions with other trait methods on %s", | ||
name, ZSTR_VAL(ce->name)); | ||
#else /* TODO: better error message */ | ||
zend_error_noreturn(E_COMPILE_ERROR, "Trait method %s::%s has not been applied as %s::%s, because of collision with %s::%s", | ||
ZSTR_VAL(fn->common.scope->name), ZSTR_VAL(fn->common.function_name), | ||
ZSTR_VAL(ce->name), name, | ||
ZSTR_VAL(existing_fn->common.scope->name), ZSTR_VAL(existing_fn->common.function_name)); | ||
#endif | ||
} else { | ||
/* inherited members are overridden by members inserted by traits */ | ||
/* check whether the trait method fulfills the inheritance requirements */ | ||
|
@@ -1245,7 +1240,7 @@ static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, ze | |
zend_string *lcname; | ||
zend_function fn_copy; | ||
|
||
/* apply aliases which are qualified with a class name, there should not be any ambiguity */ | ||
/* aliases which are qualified with a class name won't have any ambiguity. method names try to match with alias->trait_names */ | ||
if (ce->trait_aliases) { | ||
alias_ptr = ce->trait_aliases; | ||
alias = *alias_ptr; | ||
|
@@ -1255,20 +1250,38 @@ static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, ze | |
&& (!alias->trait_method->ce || fn->common.scope == alias->trait_method->ce) | ||
&& ZSTR_LEN(alias->trait_method->method_name) == ZSTR_LEN(fnname) | ||
&& (zend_binary_strcasecmp(ZSTR_VAL(alias->trait_method->method_name), ZSTR_LEN(alias->trait_method->method_name), ZSTR_VAL(fnname), ZSTR_LEN(fnname)) == 0)) { | ||
fn_copy = *fn; | ||
|
||
/* if it is 0, no modifieres has been changed */ | ||
if (alias->modifiers) { | ||
fn_copy.common.fn_flags = alias->modifiers | (fn->common.fn_flags ^ (fn->common.fn_flags & ZEND_ACC_PPP_MASK)); | ||
if (alias->trait_names) { | ||
size_t i = 0; | ||
while (alias->trait_names[i]) { | ||
if (zend_binary_strcasecmp( | ||
ZSTR_VAL(fn->common.scope->name), | ||
ZSTR_LEN(fn->common.scope->name), | ||
ZSTR_VAL(alias->trait_names[i]), | ||
ZSTR_LEN(alias->trait_names[i])) == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can use zend_string_equals_ci here. |
||
|
||
alias->trait_method->class_name = alias->trait_names[i]; | ||
} | ||
i++; | ||
} | ||
} | ||
|
||
lcname = zend_string_tolower(alias->alias); | ||
zend_add_trait_method(ce, ZSTR_VAL(alias->alias), lcname, &fn_copy, overriden); | ||
zend_string_release(lcname); | ||
if (alias->trait_method->ce || alias->trait_method->class_name) { | ||
fn_copy = *fn; | ||
|
||
/* Record the trait from which this alias was resolved. */ | ||
if (!alias->trait_method->ce) { | ||
alias->trait_method->ce = fn->common.scope; | ||
/* if it is 0, no modifiers have been changed */ | ||
if (alias->modifiers) { | ||
fn_copy.common.fn_flags = alias->modifiers | (fn->common.fn_flags ^ (fn->common.fn_flags & ZEND_ACC_PPP_MASK)); | ||
} | ||
|
||
lcname = zend_string_tolower(alias->alias); | ||
zend_add_trait_method(ce, ZSTR_VAL(alias->alias), lcname, &fn_copy, overriden); | ||
zend_string_release(lcname); | ||
|
||
/* Record the trait from which this alias was resolved. */ | ||
if (!alias->trait_method->ce) { | ||
alias->trait_method->ce = fn->common.scope; | ||
} | ||
} | ||
} | ||
alias_ptr++; | ||
|
@@ -1292,11 +1305,28 @@ static int zend_traits_copy_functions(zend_string *fnname, zend_function *fn, ze | |
&& (ZSTR_LEN(alias->trait_method->method_name) == ZSTR_LEN(fnname)) | ||
&& (zend_binary_strcasecmp(ZSTR_VAL(alias->trait_method->method_name), ZSTR_LEN(alias->trait_method->method_name), ZSTR_VAL(fnname), ZSTR_LEN(fnname)) == 0)) { | ||
|
||
fn_copy.common.fn_flags = alias->modifiers | (fn->common.fn_flags ^ (fn->common.fn_flags & ZEND_ACC_PPP_MASK)); | ||
if (alias->trait_names) { | ||
size_t i = 0; | ||
while (alias->trait_names[i]) { | ||
if (zend_binary_strcasecmp( | ||
ZSTR_VAL(fn->common.scope->name), | ||
ZSTR_LEN(fn->common.scope->name), | ||
ZSTR_VAL(alias->trait_names[i]), | ||
ZSTR_LEN(alias->trait_names[i])) == 0) { | ||
|
||
/** Record the trait from which this alias was resolved. */ | ||
if (!alias->trait_method->ce) { | ||
alias->trait_method->ce = fn->common.scope; | ||
alias->trait_method->class_name = alias->trait_names[i]; | ||
} | ||
i++; | ||
} | ||
} | ||
|
||
if (alias->trait_method->ce || alias->trait_method->class_name) { | ||
fn_copy.common.fn_flags = alias->modifiers | (fn->common.fn_flags ^ (fn->common.fn_flags & ZEND_ACC_PPP_MASK)); | ||
|
||
/** Record the trait from which this alias was resolved. */ | ||
if (!alias->trait_method->ce) { | ||
alias->trait_method->ce = fn->common.scope; | ||
} | ||
} | ||
} | ||
alias_ptr++; | ||
|
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we make this
use Trait1, Trait3
andTrait3
also has an init method? If I'm reading your code right, it's just going to pick one of them. But I think this should be an error.I'm wondering if an absolute trait method reference shouldn't be required when multiple traits are used in one block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now it still picks one of them in that case. The currrent behavior is to disambiguate when possible and keep the old behavior (pick one) when it's not possible to know. But yes, I think it's a good idea to error in those cases.
I've started porting this to 7.4 but still have to redo the opcache part. So I'll also rework that.