Skip to content

Commit 1ae3ccb

Browse files
committed
Fix empty match expressions and match expressions with default case only in PHP 7
1 parent c60c1e2 commit 1ae3ccb

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

ChangeLog.md

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ XP Compiler ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
## 9.3.2 / 2024-11-02
7+
8+
* Fixed empty match expressions and match expressions with default
9+
case only in PHP 7
10+
(@thekid)
11+
* Added tests verifying closures are supported in constant expressions
12+
https://wiki.php.net/rfc/closures_in_const_expr
13+
(@thekid)
14+
615
## 9.3.1 / 2024-10-05
716

817
* Fixed `private(set)` not being implicitely marked as *final*, see

src/main/php/lang/ast/emit/MatchAsTernaries.class.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ protected function emitMatch($result, $match) {
3030
}
3131
}
3232

33+
// Match without cases => create something that will never match
34+
$b || $result->out->write('===NAN?:');
35+
3336
// Emit IIFE for raising an error until we have throw expressions
3437
if (null === $match->default) {
35-
$result->out->write('function() use('.$t.') { throw new \\Error("Unhandled match value of type ".gettype('.$t.')); })(');
38+
$result->out->write('(function() use('.$t.') { throw new \\Error("Unhandled match value of type ".gettype('.$t.')); })()');
3639
} else {
3740
$this->emitAsExpression($result, $match->default);
3841
}

src/test/php/lang/ast/unittest/emit/ControlStructuresTest.class.php

+24
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ public function run($arg) {
8686
Assert::equals($expected, $r);
8787
}
8888

89+
#[Test]
90+
public function match_with_default_only() {
91+
$r= $this->run('class %T {
92+
public function run() {
93+
return match (true) {
94+
default => "Test",
95+
};
96+
}
97+
}');
98+
99+
Assert::equals('Test', $r);
100+
}
101+
89102
#[Test, Values([[200, 'OK'], [302, 'Redirect'], [404, 'Error #404']])]
90103
public function match_with_multiple_cases($input, $expected) {
91104
$r= $this->run('class %T {
@@ -154,6 +167,17 @@ public function run($arg) {
154167
Assert::equals('10+ items', $r);
155168
}
156169

170+
#[Test, Expect(class: Throwable::class, message: '/Unhandled match (value of type .+|case .+)/')]
171+
public function empty_match() {
172+
$r= $this->run('class %T {
173+
public function run() {
174+
return match (true) { };
175+
}
176+
}');
177+
178+
Assert::equals('Test', $r);
179+
}
180+
157181
#[Test, Expect(class: Throwable::class, message: '/Unhandled match (value of type .+|case .+)/')]
158182
public function unhandled_match() {
159183
$this->run('class %T {

0 commit comments

Comments
 (0)