Skip to content

Require non-absolute trait method refs to be unambiguous #5232

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Zend/tests/bug62069.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
Bug #62069: binding wrong traits if they have same name methods
--FILE--
<?php

trait T1 {
public function func() {
echo "From T1\n";
}
}

trait T2 {
public function func() {
echo "From T2\n";
}
}

class Bar {
public function func() {
echo "From Bar\n";
}
use T1, T2 {
func as f1;
}
}

$b = new Bar();
$b->f2();

?>
--EXPECTF--
Fatal error: An alias was defined for method func(), which exists in both T1 and T2. Use T1::func or T2::func to resolve the ambiguity in %s on line %d
35 changes: 35 additions & 0 deletions Zend/tests/bug62069_2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Bug #62069: binding wrong traits if they have same name methods (variation 2)
--FILE--
<?php

trait T1 {
public function func() {
echo "From T1\n";
}
}

trait T2 {
public function func() {
echo "From T2\n";
}
}

class Bar {
public function func() {
echo "From Bar\n";
}
use T1 {
func as f1;
}
use T2 {
func as f2;
}
}

$b = new Bar();
$b->f2();

?>
--EXPECTF--
Fatal error: An alias was defined for method func(), which exists in both T1 and T2. Use T1::func or T2::func to resolve the ambiguity in %s on line %d
2 changes: 1 addition & 1 deletion Zend/tests/traits/language011.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait World {


class MyClass {
use Hello, World { sayHello as sayWorld; }
use Hello, World { World::sayHello as sayWorld; }
}

$o = new MyClass();
Expand Down
8 changes: 6 additions & 2 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1870,8 +1870,12 @@ static void zend_traits_init_trait_structures(zend_class_entry *ce, zend_class_e
continue;
}

// TODO: This is ambiguous! The first trait is assumed.
break;
zend_error_noreturn(E_COMPILE_ERROR,
"An alias was defined for method %s(), which exists in both %s and %s. Use %s::%s or %s::%s to resolve the ambiguity",
ZSTR_VAL(cur_method_ref->method_name),
ZSTR_VAL(trait->name), ZSTR_VAL(traits[j]->name),
ZSTR_VAL(trait->name), ZSTR_VAL(cur_method_ref->method_name),
ZSTR_VAL(traits[j]->name), ZSTR_VAL(cur_method_ref->method_name));
}
}
}
Expand Down