Skip to content

Make missing trait error recoverable #17960

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 2 commits 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
5 changes: 4 additions & 1 deletion Zend/tests/traits/bugs/missing-trait.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ $test = new TraitsTest();

?>
--EXPECTF--
Fatal error: Trait "THello" not found in %s on line %d
Fatal error: Uncaught Error: Trait "THello" not found in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
5 changes: 4 additions & 1 deletion Zend/tests/traits/error_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ class A {

?>
--EXPECTF--
Fatal error: Trait "abc" not found in %s on line %d
Fatal error: Uncaught Error: Trait "abc" not found in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
5 changes: 4 additions & 1 deletion Zend/tests/traits/error_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class A {

?>
--EXPECTF--
Fatal error: A cannot use abc - it is not a trait in %s on line %d
Fatal error: Uncaught Error: A cannot use abc - it is not a trait in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
5 changes: 4 additions & 1 deletion Zend/tests/traits/error_004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class A {

?>
--EXPECTF--
Fatal error: A cannot use abc - it is not a trait in %s on line %d
Fatal error: Uncaught Error: A cannot use abc - it is not a trait in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
5 changes: 4 additions & 1 deletion Zend/tests/traits/error_005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class A {

?>
--EXPECTF--
Fatal error: A cannot use abc - it is not a trait in %s on line %d
Fatal error: Uncaught Error: A cannot use abc - it is not a trait in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
5 changes: 4 additions & 1 deletion Zend/tests/traits/error_006.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ class A {

?>
--EXPECTF--
Fatal error: A cannot use abc - it is not a trait in %s on line %d
Fatal error: Uncaught Error: A cannot use abc - it is not a trait in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
18 changes: 18 additions & 0 deletions Zend/tests/traits/gh17959.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
GH-17959: Missing trait error is recoverable
--FILE--
<?php

try {
class C {
use MissingTrait;
}
} catch (Error $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}

?>
===DONE===
--EXPECT--
Error: Trait "MissingTrait" not found
===DONE===
4 changes: 2 additions & 2 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -3535,13 +3535,13 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string

for (i = 0; i < ce->num_traits; i++) {
zend_class_entry *trait = zend_fetch_class_by_name(ce->trait_names[i].name,
ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT | ZEND_FETCH_CLASS_EXCEPTION);
if (UNEXPECTED(trait == NULL)) {
free_alloca(traits_and_interfaces, use_heap);
return NULL;
}
if (UNEXPECTED(!(trait->ce_flags & ZEND_ACC_TRAIT))) {
zend_error_noreturn(E_ERROR, "%s cannot use %s - it is not a trait", ZSTR_VAL(ce->name), ZSTR_VAL(trait->name));
zend_throw_error(NULL, "%s cannot use %s - it is not a trait", ZSTR_VAL(ce->name), ZSTR_VAL(trait->name));
free_alloca(traits_and_interfaces, use_heap);
return NULL;
}
Expand Down
Loading