-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Show enum cases in errors #14496
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
Show enum cases in errors #14496
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--TEST-- | ||
Enum case is shown in stack trace | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
} | ||
|
||
function test($enum) { | ||
throw new Exception(); | ||
} | ||
|
||
test(Foo::Bar); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught Exception in %s:%d | ||
Stack trace: | ||
#0 %s(%d): test(Foo::Bar) | ||
#1 {main} | ||
thrown in %s on line %d |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Match errors show unmatched enum case | ||
--FILE-- | ||
<?php | ||
|
||
enum Foo { | ||
case Bar; | ||
case Baz; | ||
} | ||
|
||
try { | ||
match (Foo::Bar) { | ||
Foo::Baz => 42, | ||
}; | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
Unhandled match case Foo::Bar |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -873,14 +873,12 @@ ZEND_COLD zend_never_inline void zend_magic_get_property_type_inconsistency_erro | |
ZEND_COLD void zend_match_unhandled_error(const zval *value) | ||
{ | ||
smart_str msg = {0}; | ||
|
||
if (Z_TYPE_P(value) <= IS_STRING) { | ||
smart_str_append_scalar(&msg, value, EG(exception_string_param_max_len)); | ||
if (smart_str_append_zval(&msg, value, EG(exception_string_param_max_len)) == SUCCESS) { | ||
/* Nothing to do. */ | ||
} else { | ||
smart_str_appendl(&msg, "of type ", sizeof("of type ")-1); | ||
smart_str_appends(&msg, zend_zval_type_name(value)); | ||
} | ||
|
||
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. Line should be reverted |
||
smart_str_0(&msg); | ||
|
||
zend_throw_exception_ex( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -647,8 +647,8 @@ static zval *get_default_from_recv(zend_op_array *op_array, uint32_t offset) { | |
} | ||
|
||
static int format_default_value(smart_str *str, zval *value) { | ||
if (Z_TYPE_P(value) <= IS_STRING) { | ||
smart_str_append_scalar(str, value, SIZE_MAX); | ||
if (smart_str_append_zval(str, value, SIZE_MAX) == SUCCESS) { | ||
/* Nothing to do. */ | ||
} else if (Z_TYPE_P(value) == IS_ARRAY) { | ||
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 this branch still be reached? 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. Yes, |
||
zend_string *str_key; | ||
zend_long num_key; | ||
|
@@ -675,14 +675,6 @@ static int format_default_value(smart_str *str, zval *value) { | |
format_default_value(str, zv); | ||
} ZEND_HASH_FOREACH_END(); | ||
smart_str_appendc(str, ']'); | ||
} else if (Z_TYPE_P(value) == IS_OBJECT) { | ||
/* This branch may only be reached for default properties, which don't support arbitrary objects. */ | ||
zend_object *obj = Z_OBJ_P(value); | ||
zend_class_entry *class = obj->ce; | ||
ZEND_ASSERT(class->ce_flags & ZEND_ACC_ENUM); | ||
smart_str_append(str, class->name); | ||
smart_str_appends(str, "::"); | ||
smart_str_append(str, Z_STR_P(zend_enum_fetch_case_name(obj))); | ||
} else { | ||
ZEND_ASSERT(Z_TYPE_P(value) == IS_CONSTANT_AST); | ||
zend_string *ast_str = zend_ast_export("", Z_ASTVAL_P(value), ""); | ||
|
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.
Maybe?
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.
Nope, that looks correct. We only want to emit "of type ..." if
smart_str_append_zval()
can't handle the value. Note that it intentionally does not handle some values like arrays, because they seem to be handled differently depending on the context. It would be nice if it could be unified in the future, but my intention is not to break BC here.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.
Oh, sorry. I misread your suggestion. :) Yeah, I'll change it!