Skip to content

Commit ddc0b49

Browse files
committed
Allow arbitrary const expressions in backed enums
Closes GH-7821 Closes GH-8190 Closes GH-8418
1 parent 5a855ee commit ddc0b49

23 files changed

+287
-144
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PHP NEWS
77
references). (Nicolas Grekas)
88
. Fixed bug GH-8661 (Nullsafe in coalesce triggers undefined variable
99
warning). (ilutov)
10+
. Fixed bug GH-7821 and GH-8418 (Allow arbitrary const expressions in backed
11+
enums). (ilutov)
1012

1113
- MBString:
1214
. Backwards-compatible mappings for 0x5C/0x7E in Shift-JIS are restored,

Zend/tests/enum/backed-duplicate-int.phpt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ enum Foo: int {
88
case Baz = 0;
99
}
1010

11+
try {
12+
var_dump(Foo::Bar);
13+
} catch (Error $e) {
14+
echo $e->getMessage(), "\n";
15+
}
16+
17+
try {
18+
var_dump(Foo::Bar);
19+
} catch (Error $e) {
20+
echo $e->getMessage(), "\n";
21+
}
22+
23+
try {
24+
var_dump(Foo::from(42));
25+
} catch (Error $e) {
26+
echo $e->getMessage(), "\n";
27+
}
28+
29+
try {
30+
var_dump(Foo::tryFrom('bar'));
31+
} catch (Error $e) {
32+
echo $e->getMessage(), "\n";
33+
}
34+
1135
?>
12-
--EXPECTF--
13-
Fatal error: Duplicate value in enum Foo for cases Bar and Baz in %s on line %s
36+
--EXPECT--
37+
Duplicate value in enum Foo for cases Bar and Baz
38+
Duplicate value in enum Foo for cases Bar and Baz
39+
Duplicate value in enum Foo for cases Bar and Baz
40+
Foo::tryFrom(): Argument #1 ($value) must be of type int, string given

Zend/tests/enum/backed-duplicate-string.phpt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ enum Suit: string {
1010
case Spades = 'H';
1111
}
1212

13+
try {
14+
var_dump(Suit::Hearts);
15+
} catch (Error $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
try {
20+
var_dump(Suit::Hearts);
21+
} catch (Error $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
25+
try {
26+
var_dump(Suit::from(42));
27+
} catch (Error $e) {
28+
echo $e->getMessage(), "\n";
29+
}
30+
31+
try {
32+
var_dump(Suit::tryFrom('bar'));
33+
} catch (Error $e) {
34+
echo $e->getMessage(), "\n";
35+
}
36+
1337
?>
14-
--EXPECTF--
15-
Fatal error: Duplicate value in enum Suit for cases Hearts and Spades in %s on line %s
38+
--EXPECT--
39+
Duplicate value in enum Suit for cases Hearts and Spades
40+
Duplicate value in enum Suit for cases Hearts and Spades
41+
Duplicate value in enum Suit for cases Hearts and Spades
42+
Duplicate value in enum Suit for cases Hearts and Spades

Zend/tests/enum/backed-int-const-invalid-expr.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ var_dump(Foo::Bar->value);
1111

1212
?>
1313
--EXPECTF--
14-
Fatal error: Enum case value must be compile-time evaluatable in %s on line %d
14+
Fatal error: Constant expression contains invalid operations in %s on line %d

Zend/tests/enum/backed-mismatch.phpt

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ enum Foo: int {
77
case Bar = 'bar';
88
}
99

10+
try {
11+
var_dump(Foo::Bar);
12+
} catch (Error $e) {
13+
echo get_class($e), ': ', $e->getMessage(), "\n";
14+
}
15+
16+
try {
17+
var_dump(Foo::Bar);
18+
} catch (Error $e) {
19+
echo get_class($e), ': ', $e->getMessage(), "\n";
20+
}
21+
22+
try {
23+
var_dump(Foo::from(42));
24+
} catch (Error $e) {
25+
echo get_class($e), ': ', $e->getMessage(), "\n";
26+
}
27+
28+
try {
29+
var_dump(Foo::from('bar'));
30+
} catch (Error $e) {
31+
echo get_class($e), ': ', $e->getMessage(), "\n";
32+
}
33+
1034
?>
11-
--EXPECTF--
12-
Fatal error: Enum case type string does not match enum backing type int in %s on line %d
35+
--EXPECT--
36+
TypeError: Enum case type string does not match enum backing type int
37+
TypeError: Enum case type string does not match enum backing type int
38+
TypeError: Enum case type string does not match enum backing type int
39+
TypeError: Foo::from(): Argument #1 ($value) must be of type int, string given

Zend/tests/enum/gh7821.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-7821: Can't use arbitrary constant expressions in enum cases
3+
--FILE--
4+
<?php
5+
6+
interface I {
7+
public const A = 'A';
8+
public const B = 'B';
9+
}
10+
11+
enum B: string implements I {
12+
case C = I::A;
13+
case D = self::B;
14+
}
15+
16+
var_dump(B::A);
17+
var_dump(B::B);
18+
var_dump(B::C->value);
19+
var_dump(B::D->value);
20+
21+
?>
22+
--EXPECT--
23+
string(1) "A"
24+
string(1) "B"
25+
string(1) "A"
26+
string(1) "B"

Zend/tests/enum/gh8418.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-8418: Enum constant expression evaluation doesn't work with warmed cache
3+
--FILE--
4+
<?php
5+
6+
class ExampleClass
7+
{
8+
public const EXAMPLE_CONST = 42;
9+
}
10+
11+
enum ExampleEnum: int
12+
{
13+
case ENUM_CASE = ExampleClass::EXAMPLE_CONST;
14+
}
15+
16+
var_dump(ExampleEnum::ENUM_CASE->value);
17+
18+
?>
19+
--EXPECT--
20+
int(42)

Zend/tests/enum/non-backed-enum-with-expr-value.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ enum Foo {
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Case Bar of non-backed enum Foo must not have a value, try adding ": int" to the enum declaration in %s on line %d
12+
Fatal error: Case Bar of non-backed enum Foo must not have a value in %s on line %d

Zend/tests/enum/non-backed-enum-with-int-value.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ enum Foo {
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Case Bar of non-backed enum Foo must not have a value, try adding ": int" to the enum declaration in %s on line %d
12+
Fatal error: Case Bar of non-backed enum Foo must not have a value in %s on line %d

Zend/tests/enum/non-backed-enum-with-string-value.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ enum Foo {
99

1010
?>
1111
--EXPECTF--
12-
Fatal error: Case Bar of non-backed enum Foo must not have a value, try adding ": string" to the enum declaration in %s on line %d
12+
Fatal error: Case Bar of non-backed enum Foo must not have a value in %s on line %d

0 commit comments

Comments
 (0)