Skip to content

Commit 51e0e6a

Browse files
committed
Hint to add : type to enum decl
when adding a backed value to a case without adding a backing type to the enum.
1 parent 87b6740 commit 51e0e6a

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Non-scalar enum errors when case has int value
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar = 1;
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Case Bar of non-scalar enum Foo must not have a value, try adding ": int" to the enum declaration in %s on line %d

Zend/tests/enum/non-scalar-enum-with-value.phpt renamed to Zend/tests/enum/non-scalar-enum-with-invalid-value.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
--TEST--
2-
Non-scalar enum errors when case has value
2+
Non-scalar enum errors when case has invalid value
33
--FILE--
44
<?php
55

66
enum Foo {
7-
case Bar = 1;
7+
case Bar = 3.141;
88
}
99

1010
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Non-scalar enum errors when case has string value
3+
--FILE--
4+
<?php
5+
6+
enum Foo {
7+
case Bar = 'Bar';
8+
}
9+
10+
?>
11+
--EXPECTF--
12+
Fatal error: Case Bar of non-scalar enum Foo must not have a value, try adding ": string" to the enum declaration in %s on line %d

Zend/zend_compile.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7699,9 +7699,18 @@ static void zend_compile_enum_case(zend_ast *ast)
76997699
}
77007700
if (case_value_ast != NULL) {
77017701
if (enum_class->enum_scalar_type == IS_UNDEF) {
7702-
zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-scalar enum %s must not have a value",
7703-
ZSTR_VAL(enum_case_name),
7704-
ZSTR_VAL(enum_class_name));
7702+
zval case_value_zv;
7703+
ZVAL_COPY(&case_value_zv, zend_ast_get_zval(case_value_ast));
7704+
if (Z_TYPE(case_value_zv) == IS_LONG || Z_TYPE(case_value_zv) == IS_STRING) {
7705+
zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-scalar enum %s must not have a value, try adding \": %s\" to the enum declaration",
7706+
ZSTR_VAL(enum_case_name),
7707+
ZSTR_VAL(enum_class_name),
7708+
zend_zval_type_name(&case_value_zv));
7709+
} else {
7710+
zend_error_noreturn(E_COMPILE_ERROR, "Case %s of non-scalar enum %s must not have a value",
7711+
ZSTR_VAL(enum_case_name),
7712+
ZSTR_VAL(enum_class_name));
7713+
}
77057714
}
77067715

77077716
zend_eval_const_expr(&ast->child[1]);

0 commit comments

Comments
 (0)