-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfeature.patch
More file actions
2673 lines (2649 loc) · 84 KB
/
Copy pathfeature.patch
File metadata and controls
2673 lines (2649 loc) · 84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/Zend/tests/autoload/bug42798.phpt b/Zend/tests/autoload/bug42798.phpt
index 5f19fe30..18efcaa1 100644
--- a/Zend/tests/autoload/bug42798.phpt
+++ b/Zend/tests/autoload/bug42798.phpt
@@ -7,10 +7,10 @@ spl_autoload_register(function ($className) {
exit();
});
-function foo($c = ok::constant) {
+function foo($c = undefinedclass::constant) {
}
foo();
?>
--EXPECT--
-ok
+undefinedclass
diff --git a/Zend/tests/coalesce_option.phpt b/Zend/tests/coalesce_option.phpt
new file mode 100644
index 00000000..940cf851
--- /dev/null
+++ b/Zend/tests/coalesce_option.phpt
@@ -0,0 +1,31 @@
+--TEST--
+`??` unwraps Some/Ok and falls back on None/Err (Propagatable-aware coalesce)
+--FILE--
+<?php
+var_dump(Some(5) ?? 0); // unwraps
+var_dump(None() ?? 0); // falls back
+var_dump(Ok(7) ?? -1); // unwraps
+var_dump(Err("x") ?? -1); // falls back
+
+// Plain values and null are unchanged.
+var_dump(42 ?? 0);
+var_dump(null ?? "d");
+
+// The default is lazy: not evaluated when the left side is present.
+function boom() { throw new \Exception("should not run"); }
+var_dump(Some("ok") ?? boom());
+
+// Chaining works left-to-right; first present-and-success wins.
+var_dump(None() ?? None() ?? "last");
+var_dump(None() ?? Some("mid") ?? "last");
+?>
+--EXPECT--
+int(5)
+int(0)
+int(7)
+int(-1)
+int(42)
+string(1) "d"
+string(2) "ok"
+string(4) "last"
+string(3) "mid"
diff --git a/Zend/tests/comprehension.phpt b/Zend/tests/comprehension.phpt
new file mode 100644
index 00000000..9401f185
--- /dev/null
+++ b/Zend/tests/comprehension.phpt
@@ -0,0 +1,70 @@
+--TEST--
+for {} yield comprehensions — unified over arrays and Option/Result
+--FILE--
+<?php
+// Arrays: cartesian product.
+print_r(for { $x = [1, 2, 3]; $y = [10, 20] } yield $x + $y);
+
+// Arrays: single generator is a map.
+print_r(for { $x = [1, 2, 3] } yield $x * 2);
+
+// Arrays: guard filters.
+print_r(for { $x = [1, 2, 3, 4]; if $x % 2 === 0 } yield $x * $x);
+
+// Arrays: a later generator may depend on an earlier binding.
+print_r(for { $x = [1, 2, 3]; $y = range(1, $x) } yield "$x:$y");
+
+// Option: all-Some binds; a None short-circuits.
+echo for { $x = Some(1); $y = Some(2) } yield $x + $y, "\n";
+echo for { $x = Some(1); $y = None() } yield $x + $y, "\n";
+
+// Result: Ok chains; an Err short-circuits to that Err.
+echo for { $x = Ok(4); $y = Ok(5) } yield $x * $y, "\n";
+echo for { $x = Ok(1); $y = Err("boom") } yield $x + $y, "\n";
+
+// A guard on an Option uses its filter() -> can turn Some into None.
+echo for { $x = Some(4); if $x > 10 } yield $x, "\n";
+
+// Mixing array and Option in one comprehension is an error.
+try {
+ for { $x = [1, 2]; $y = Some(3) } yield $x + $y;
+} catch (\Error $e) {
+ echo "type error\n";
+}
+?>
+--EXPECT--
+Array
+(
+ [0] => 11
+ [1] => 21
+ [2] => 12
+ [3] => 22
+ [4] => 13
+ [5] => 23
+)
+Array
+(
+ [0] => 2
+ [1] => 4
+ [2] => 6
+)
+Array
+(
+ [0] => 4
+ [1] => 16
+)
+Array
+(
+ [0] => 1:1
+ [1] => 2:1
+ [2] => 2:2
+ [3] => 3:1
+ [4] => 3:2
+ [5] => 3:3
+)
+Some(3)
+None
+Ok(20)
+Err("boom")
+None
+type error
diff --git a/Zend/tests/comprehension_dict.phpt b/Zend/tests/comprehension_dict.phpt
new file mode 100644
index 00000000..3e9b500c
--- /dev/null
+++ b/Zend/tests/comprehension_dict.phpt
@@ -0,0 +1,49 @@
+--TEST--
+for {} yield $k => $v — dict comprehensions
+--FILE--
+<?php
+// Basic dict comprehension over an array generator.
+$users = [["id" => 10, "name" => "ann"], ["id" => 20, "name" => "bob"]];
+var_export(for { $u = $users } yield $u["id"] => $u["name"]);
+echo "\n";
+
+// A guard filters before the pair is produced.
+var_export(for { $x = [1, 2, 3, 4]; if $x % 2 === 0 } yield $x => $x * $x);
+echo "\n";
+
+// Computed keys; later keys win on collision.
+var_export(for { $x = [1, 2, 3] } yield "k" => $x);
+echo "\n";
+
+// Two generators (cartesian) with a composed key.
+var_export(for { $a = [1, 2]; $b = [10, 20] } yield "$a-$b" => $a + $b);
+echo "\n";
+
+// The plain list comprehension is unchanged.
+print_r(for { $x = [1, 2, 3] } yield $x * 10);
+
+?>
+--EXPECT--
+array (
+ 10 => 'ann',
+ 20 => 'bob',
+)
+array (
+ 2 => 4,
+ 4 => 16,
+)
+array (
+ 'k' => 3,
+)
+array (
+ '1-10' => 11,
+ '1-20' => 21,
+ '2-10' => 12,
+ '2-20' => 22,
+)
+Array
+(
+ [0] => 10
+ [1] => 20
+ [2] => 30
+)
diff --git a/Zend/tests/comprehensions_comprehensive.phpt b/Zend/tests/comprehensions_comprehensive.phpt
new file mode 100644
index 00000000..330a7feb
--- /dev/null
+++ b/Zend/tests/comprehensions_comprehensive.phpt
@@ -0,0 +1,57 @@
+--TEST--
+comprehensions — cartesian, guard, dependent, Option/Result, dict
+--FILE--
+<?php
+// array: cartesian product
+print_r(for { $x = [1,2,3]; $y = [10,20] } yield $x + $y);
+// guard
+print_r(for { $x = range(1,9); if $x % 2 } yield $x * $x);
+// dependent generator
+print_r(for { $x = [1,2,3]; $y = range(1, $x) } yield "$x:$y");
+// Option: all Some
+var_dump(for { $x = Some(1); $y = Some(2) } yield $x + $y);
+// Option short-circuit
+var_dump(for { $x = Some(1); $y = None() } yield $x + $y);
+// Result short-circuit on Err
+var_dump((for { $x = Ok(1); $y = Err("e") } yield $x + $y)->getError());
+// dict comprehension
+print_r(for { $x = [1,2,3,4]; if $x % 2 === 0 } yield $x => $x * $x);
+--EXPECT--
+Array
+(
+ [0] => 11
+ [1] => 21
+ [2] => 12
+ [3] => 22
+ [4] => 13
+ [5] => 23
+)
+Array
+(
+ [0] => 1
+ [1] => 9
+ [2] => 25
+ [3] => 49
+ [4] => 81
+)
+Array
+(
+ [0] => 1:1
+ [1] => 2:1
+ [2] => 2:2
+ [3] => 3:1
+ [4] => 3:2
+ [5] => 3:3
+)
+object(Some)#5 (1) {
+ ["value"]=>
+ int(3)
+}
+object(None)#1 (0) {
+}
+string(1) "e"
+Array
+(
+ [2] => 4
+ [4] => 16
+)
diff --git a/Zend/tests/inheritance/bug60444.phpt b/Zend/tests/inheritance/bug60444.phpt
index f450b9e4..1348c8a9 100644
--- a/Zend/tests/inheritance/bug60444.phpt
+++ b/Zend/tests/inheritance/bug60444.phpt
@@ -5,10 +5,10 @@ Bug #60444 (Segmentation fault with include & class extending)
class Foo {
public function __construct() {
eval("class Bar extends Foo {}");
- Some::foo($this);
+ SomeClass::foo($this);
}
}
-class Some {
+class SomeClass {
public static function foo(Foo $foo) {
}
}
diff --git a/Zend/tests/instanceof_bind.phpt b/Zend/tests/instanceof_bind.phpt
new file mode 100644
index 00000000..422094c0
--- /dev/null
+++ b/Zend/tests/instanceof_bind.phpt
@@ -0,0 +1,49 @@
+--TEST--
+`instanceof Some($x)` binds the unwrapped payload (if-let style)
+--FILE--
+<?php
+// Bind on a true test; the expression still evaluates to the bool.
+if (Some(7) instanceof Some($x)) {
+ echo "some: $x\n";
+} else {
+ echo "none\n";
+}
+
+if (None() instanceof Some($x)) {
+ echo "some\n";
+} else {
+ echo "else\n";
+}
+
+// Ok binds getValue(), Err binds getError().
+$r = Err("boom");
+if ($r instanceof Ok($v)) {
+ echo "ok: $v\n";
+} elseif ($r instanceof Err($e)) {
+ echo "err: $e\n";
+}
+
+// while-let: drain a list of Options.
+$xs = [Some(1), Some(2), None()];
+while (array_shift($xs) instanceof Some($v)) {
+ echo "drain $v\n";
+}
+
+// The whole thing is a boolean expression.
+var_dump(Some(1) instanceof Some($a));
+var_dump(None() instanceof Some($b));
+
+// Plain instanceof (no capture) is unchanged.
+var_dump(Some(1) instanceof Some);
+var_dump(Some(1) instanceof None);
+?>
+--EXPECT--
+some: 7
+else
+err: boom
+drain 1
+drain 2
+bool(true)
+bool(false)
+bool(true)
+bool(false)
diff --git a/Zend/tests/match_patterns.phpt b/Zend/tests/match_patterns.phpt
new file mode 100644
index 00000000..9acb88c3
--- /dev/null
+++ b/Zend/tests/match_patterns.phpt
@@ -0,0 +1,38 @@
+--TEST--
+match destructuring patterns for built-in Option/Result
+--FILE--
+<?php
+// Binding: Some($x) / Ok($v) / Err($e) bind the unwrapped payload.
+echo match (Some(5)) { Some($x) => $x * 2, None => -1 }, "\n"; // 10
+echo match (None()) { Some($x) => $x, None => "none" }, "\n"; // none
+echo match (Ok(7)) { Ok($v) => $v, Err($e) => "e:$e" }, "\n"; // 7
+echo match (Err("bad")) { Ok($v) => $v, Err($e) => "e:$e" }, "\n"; // e:bad
+
+// default arm.
+echo match (Some(1)) { None => "n", default => "d" }, "\n"; // d
+
+// No arm matches and no default => UnhandledMatchError.
+try {
+ match (Some(1)) { None => "n" };
+} catch (\UnhandledMatchError $e) {
+ echo "unhandled\n";
+}
+
+// Plain match (no patterns) is unaffected.
+echo match (2) { 1 => "a", 2 => "b", default => "c" }, "\n"; // b
+
+// Nested: a pattern body may itself be a match.
+echo match (Ok(3)) {
+ Ok($v) => match ($v) { 3 => "three", default => "?" },
+ Err($e) => "err",
+}, "\n"; // three
+?>
+--EXPECT--
+10
+none
+7
+e:bad
+d
+unhandled
+b
+three
diff --git a/Zend/tests/option_result_combinators.phpt b/Zend/tests/option_result_combinators.phpt
new file mode 100644
index 00000000..af0ee737
--- /dev/null
+++ b/Zend/tests/option_result_combinators.phpt
@@ -0,0 +1,64 @@
+--TEST--
+Option/Result combinators: flatMap, filter, orElse, unwrap, expect, mapErr, conversions
+--FILE--
+<?php
+// Option
+echo Some(3)->flatMap(fn($x) => Some($x + 5))->unwrap(), "\n"; // 8
+echo None()->flatMap(fn($x) => Some($x))->unwrapOr("n"), "\n"; // n
+echo Some(4)->filter(fn($x) => $x % 2 === 0)->unwrapOr("no"), "\n"; // 4
+echo Some(3)->filter(fn($x) => $x % 2 === 0)->unwrapOr("no"), "\n"; // no
+echo None()->orElse(fn() => Some(9))->unwrap(), "\n"; // 9
+echo Some(7)->toNullable(), "\n"; // 7
+var_dump(None()->toNullable()); // NULL
+
+// Option -> Result
+echo match (Some(1)->okOr("e")) { Ok($v) => "ok:$v", Err($x) => "err:$x" }, "\n"; // ok:1
+echo match (None()->okOr("e")) { Ok($v) => "ok:$v", Err($x) => "err:$x" }, "\n"; // err:e
+
+// Result
+echo Ok(10)->flatMap(fn($x) => Ok($x * 2))->unwrap(), "\n"; // 20
+echo Err("bad")->flatMap(fn($x) => Ok($x))->unwrapOr("kept"), "\n"; // kept
+echo Err("bad")->mapErr(fn($e) => strtoupper($e))->getError(), "\n"; // BAD
+echo Err("x")->orElse(fn($e) => Ok("recovered"))->unwrap(), "\n"; // recovered
+echo Ok(5)->toOption()->unwrap(), "\n"; // 5
+var_dump(Err("x")->toNullable()); // NULL
+
+// expect() throws with the given message on the empty side.
+try {
+ None()->expect("no value");
+} catch (\Error $e) {
+ echo $e->getMessage(), "\n"; // no value
+}
+
+// flatMap enforces that the callback returns the right wrapper.
+try {
+ Some(1)->flatMap(fn($x) => $x);
+} catch (\Error $e) {
+ echo "type-guarded\n";
+}
+
+// A fluent pipeline.
+echo Some(2)
+ ->map(fn($x) => $x * 3)
+ ->filter(fn($x) => $x > 5)
+ ->unwrapOr("none"), "\n"; // 6
+?>
+--EXPECT--
+8
+n
+4
+no
+9
+7
+NULL
+ok:1
+err:e
+20
+kept
+BAD
+recovered
+5
+NULL
+no value
+type-guarded
+6
diff --git a/Zend/tests/option_result_factories.phpt b/Zend/tests/option_result_factories.phpt
new file mode 100644
index 00000000..5a1ea1ec
--- /dev/null
+++ b/Zend/tests/option_result_factories.phpt
@@ -0,0 +1,47 @@
+--TEST--
+Option/Result: try, fromNullable, all (sequence), and __toString
+--FILE--
+<?php
+// Result::try captures the return value or any thrown Throwable.
+var_dump(Result::try(fn() => 21 * 2)->unwrap()); // int(42)
+echo Result::try(fn() => throw new RuntimeException("boom"))
+ ->getError()->getMessage(), "\n"; // boom
+
+// Option::fromNullable bridges PHP's null world.
+var_dump(Option::fromNullable(null) instanceof None); // true
+echo Option::fromNullable(7)->unwrap(), "\n"; // 7
+
+// Sequence: all present -> wrapper of the list (keys preserved); else short-circuit.
+print_r(Option::all([Some(1), Some(2), Some(3)])->unwrap()); // [1,2,3]
+var_dump(Option::all([Some(1), None(), Some(3)]) instanceof None); // true
+print_r(Result::all(['a' => Ok(1), 'b' => Ok(2)])->unwrap()); // [a=>1,b=>2]
+echo Result::all([Ok(1), Err("e2"), Err("e3")])->getError(), "\n"; // e2
+
+// __toString / Stringable.
+echo (string) Some(42), "\n"; // Some(42)
+echo (string) None(), "\n"; // None
+echo (string) Ok("hi"), "\n"; // Ok("hi")
+echo "result is " . Err("bad"), "\n"; // result is Err("bad")
+?>
+--EXPECT--
+int(42)
+boom
+bool(true)
+7
+Array
+(
+ [0] => 1
+ [1] => 2
+ [2] => 3
+)
+bool(true)
+Array
+(
+ [a] => 1
+ [b] => 2
+)
+e2
+Some(42)
+None
+Ok("hi")
+result is Err("bad")
diff --git a/Zend/tests/propagate.phpt b/Zend/tests/propagate.phpt
new file mode 100644
index 00000000..fdeb6625
--- /dev/null
+++ b/Zend/tests/propagate.phpt
@@ -0,0 +1,66 @@
+--TEST--
+Postfix `?` propagation operator with built-in Option/Result
+--FILE--
+<?php
+// None short-circuits the enclosing function.
+function firstNone(): Option {
+ $x = None()?; // returns None here
+ echo "unreachable\n";
+ return Some($x);
+}
+var_dump(firstNone() instanceof None);
+
+// Some unwraps to the inner value.
+function addOne(): Option {
+ $x = Some(41)?;
+ return Some($x + 1);
+}
+var_dump(addOne()->value);
+
+// Result: Ok unwraps, Err propagates.
+function okPath(): Result {
+ $x = Ok(10)?;
+ return Ok($x * 2);
+}
+function errPath(): Result {
+ $x = Err("boom")?;
+ return Ok($x);
+}
+var_dump(okPath()->value);
+$e = errPath();
+var_dump($e instanceof Err, $e->getError());
+
+// Chaining: first failure wins.
+function chain(): Option {
+ $a = Some(2)?;
+ $b = None()?;
+ return Some($a + $b);
+}
+var_dump(chain() instanceof None);
+
+// finally runs on short-circuit.
+function withFinally(): Option {
+ try {
+ $x = None()?;
+ return Some($x);
+ } finally {
+ echo "finally\n";
+ }
+}
+var_dump(withFinally() instanceof None);
+
+// Ternary / coalesce still parse and run normally.
+$t = true ? "y" : "n";
+$c = null ?? "d";
+echo $t, $c, "\n";
+?>
+--EXPECT--
+bool(true)
+int(42)
+int(20)
+bool(true)
+string(4) "boom"
+bool(true)
+finally
+bool(true)
+yd
diff --git a/Zend/tests/propagate_return_type.phpt b/Zend/tests/propagate_return_type.phpt
new file mode 100644
index 00000000..20404046
--- /dev/null
+++ b/Zend/tests/propagate_return_type.phpt
@@ -0,0 +1,25 @@
+--TEST--
+Propagated `?` value is checked against the enclosing return type
+--FILE--
+<?php
+// Short-circuit returns a None, which is incompatible with `: int`.
+function bad(): int {
+ $x = None()?;
+ return $x;
+}
+try {
+ bad();
+} catch (\TypeError $e) {
+ echo "TypeError\n";
+}
+
+// Compatible return type works.
+function good(): Option {
+ $x = None()?;
+ return Some($x);
+}
+var_dump(good() instanceof None);
+?>
+--EXPECT--
+TypeError
+bool(true)
diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c
index 9cb3c7aa..42c9b40f 100644
--- a/Zend/zend_ast.c
+++ b/Zend/zend_ast.c
@@ -2375,6 +2375,8 @@ simple_list:
POSTFIX_OP("++", 240, 241);
case ZEND_AST_POST_DEC:
POSTFIX_OP("--", 240, 241);
+ case ZEND_AST_PROPAGATE:
+ POSTFIX_OP("?", 240, 241);
case ZEND_AST_GLOBAL:
APPEND_NODE_1("global");
@@ -2556,6 +2558,14 @@ simple_list:
smart_str_appends(str, " instanceof ");
zend_ast_export_ns_name(str, ast->child[1], 0, indent);
break;
+ case ZEND_AST_INSTANCEOF_BIND:
+ zend_ast_export_ex(str, ast->child[0], 0, indent);
+ smart_str_appends(str, " instanceof ");
+ zend_ast_export_ns_name(str, ast->child[1], 0, indent);
+ smart_str_appendc(str, '(');
+ zend_ast_export_ex(str, ast->child[2], 0, indent);
+ smart_str_appendc(str, ')');
+ break;
case ZEND_AST_YIELD:
if (priority > 70) smart_str_appendc(str, '(');
smart_str_appends(str, "yield ");
@@ -2643,6 +2653,27 @@ simple_list:
zend_ast_export_ex(str, ast->child[1], 0, 0);
smart_str_appends(str, ",\n");
break;
+ case ZEND_AST_COMPREHENSION: {
+ zend_ast_list *clauses = zend_ast_get_list(ast->child[0]);
+ smart_str_appends(str, "for { ");
+ for (uint32_t i = 0; i < clauses->children; i++) {
+ zend_ast *c = clauses->child[i];
+ if (i) {
+ smart_str_appends(str, "; ");
+ }
+ if (c->kind == ZEND_AST_COMPREHENSION_GEN) {
+ zend_ast_export_ex(str, c->child[0], 0, indent);
+ smart_str_appends(str, " = ");
+ zend_ast_export_ex(str, c->child[1], 0, indent);
+ } else {
+ smart_str_appends(str, "if ");
+ zend_ast_export_ex(str, c->child[0], 0, indent);
+ }
+ }
+ smart_str_appends(str, " } yield ");
+ zend_ast_export_ex(str, ast->child[1], 0, indent);
+ break;
+ }
case ZEND_AST_DECLARE:
smart_str_appends(str, "declare(");
ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL);
diff --git a/Zend/zend_ast.h b/Zend/zend_ast.h
index fb48b187..0cde6632 100644
--- a/Zend/zend_ast.h
+++ b/Zend/zend_ast.h
@@ -70,6 +70,7 @@ enum _zend_ast_kind {
ZEND_AST_ATTRIBUTE_GROUP,
ZEND_AST_MATCH_ARM_LIST,
ZEND_AST_MODIFIER_LIST,
+ ZEND_AST_COMPREHENSION_CLAUSES,
/* 0 child nodes */
ZEND_AST_MAGIC_CONST = 0 << ZEND_AST_NUM_CHILDREN_SHIFT,
@@ -98,6 +99,8 @@ enum _zend_ast_kind {
ZEND_AST_POST_DEC,
ZEND_AST_YIELD_FROM,
ZEND_AST_CLASS_NAME,
+ ZEND_AST_PROPAGATE,
+ ZEND_AST_COMPREHENSION_FILTER,
ZEND_AST_GLOBAL,
ZEND_AST_UNSET,
@@ -154,12 +157,15 @@ enum _zend_ast_kind {
ZEND_AST_NAMED_ARG,
ZEND_AST_PARENT_PROPERTY_HOOK_CALL,
ZEND_AST_PIPE,
+ ZEND_AST_COMPREHENSION,
+ ZEND_AST_COMPREHENSION_GEN,
/* 3 child nodes */
ZEND_AST_METHOD_CALL = 3 << ZEND_AST_NUM_CHILDREN_SHIFT,
ZEND_AST_NULLSAFE_METHOD_CALL,
ZEND_AST_STATIC_CALL,
ZEND_AST_CONDITIONAL,
+ ZEND_AST_INSTANCEOF_BIND,
ZEND_AST_TRY,
ZEND_AST_CATCH,
diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c
index 8be1ee14..daa29e07 100644
--- a/Zend/zend_compile.c
+++ b/Zend/zend_compile.c
@@ -100,6 +100,7 @@ static zend_op *zend_compile_var(znode *result, zend_ast *ast, uint32_t type, bo
static zend_op *zend_delayed_compile_var(znode *result, zend_ast *ast, uint32_t type, bool by_ref);
static void zend_compile_expr(znode *result, zend_ast *ast);
static void zend_compile_stmt(zend_ast *ast);
+static void zend_emit_propagate_method(znode *result, znode *obj_node, const char *name);
static void zend_compile_assign(znode *result, zend_ast *ast);
#ifdef ZEND_CHECK_STACK_LIMIT
@@ -6543,6 +6544,196 @@ static void zend_compile_pipe(znode *result, zend_ast *ast)
zend_compile_expr(result, fcall_ast);
}
+/* Recognise a destructuring match-arm condition for the built-in Option/Result
+ * types: `Some($x)` / `Ok($v)` / `Err($e)` (binding) or bare `Some`/`Ok`/`Err`/
+ * `None` (no binding). These parse as an ordinary call / constant; we reinterpret
+ * them in match-arm position (comparing a subject against a freshly-built
+ * Some(...) via === is never meaningful, so nothing useful is shadowed). */
+static bool zend_match_pattern_info(
+ zend_ast *cond, const char **type_name, zend_ast **bind_var, bool *use_error)
+{
+ *bind_var = NULL;
+ *use_error = 0;
+ if (cond->kind == ZEND_AST_CALL) {
+ zend_ast *name_ast = cond->child[0];
+ zend_ast *args_ast = cond->child[1];
+ if (name_ast->kind != ZEND_AST_ZVAL
+ || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING
+ || args_ast->kind != ZEND_AST_ARG_LIST) {
+ return 0;
+ }
+ zend_string *n = Z_STR_P(zend_ast_get_zval(name_ast));
+ if (zend_string_equals_literal_ci(n, "Some")) { *type_name = "Some"; }
+ else if (zend_string_equals_literal_ci(n, "Ok")) { *type_name = "Ok"; }
+ else if (zend_string_equals_literal_ci(n, "Err")) { *type_name = "Err"; *use_error = 1; }
+ else return 0;
+ zend_ast_list *args = zend_ast_get_list(args_ast);
+ if (args->children != 1 || args->child[0]->kind != ZEND_AST_VAR) {
+ return 0;
+ }
+ *bind_var = args->child[0];
+ return 1;
+ } else if (cond->kind == ZEND_AST_CONST) {
+ zend_ast *name_ast = cond->child[0];
+ if (name_ast->kind != ZEND_AST_ZVAL || Z_TYPE_P(zend_ast_get_zval(name_ast)) != IS_STRING) {
+ return 0;
+ }
+ zend_string *n = Z_STR_P(zend_ast_get_zval(name_ast));
+ if (zend_string_equals_literal_ci(n, "Some")) { *type_name = "Some"; }
+ else if (zend_string_equals_literal_ci(n, "None")) { *type_name = "None"; }
+ else if (zend_string_equals_literal_ci(n, "Ok")) { *type_name = "Ok"; }
+ else if (zend_string_equals_literal_ci(n, "Err")) { *type_name = "Err"; }
+ else return 0;
+ return 1;
+ }
+ return 0;
+}
+
+static bool zend_match_has_patterns(zend_ast_list *arms)
+{
+ for (uint32_t i = 0; i < arms->children; i++) {
+ zend_ast *arm = arms->child[i];
+ if (!arm->child[0]) {
+ continue;
+ }
+ zend_ast_list *conds = zend_ast_get_list(arm->child[0]);
+ for (uint32_t j = 0; j < conds->children; j++) {
+ const char *t; zend_ast *b; bool e;
+ if (zend_match_pattern_info(conds->child[j], &t, &b, &e)) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+/* Emit `result = obj instanceof <type_name>` (obj consumed). */
+static void zend_emit_pattern_instanceof(znode *result, znode *obj, const char *type_name)
+{
+ zend_op *opline = zend_emit_op_tmp(result, ZEND_INSTANCEOF, obj, NULL);
+ opline->op2_type = IS_CONST;
+ opline->op2.constant = zend_add_class_name_literal(
+ zend_string_init(type_name, strlen(type_name), 0));
+ opline->extended_value = zend_alloc_cache_slot();
+}
+
+/* Sequential compilation of a `match` whose arms include Option/Result patterns.
+ * Each arm tests `subject instanceof <Type>` (or === for plain conditions) and,
+ * for a single binding pattern, binds the unwrapped payload before the body. */
+static void zend_compile_match_patterns(znode *result, zend_ast *ast)
+{
+ zend_ast *expr_ast = ast->child[0];
+ zend_ast_list *arms = zend_ast_get_list(ast->child[1]);
+ bool has_default_arm = 0;
+
+ for (uint32_t i = 0; i < arms->children; i++) {
+ if (!arms->child[i]->child[0]) {
+ if (has_default_arm) {
+ CG(zend_lineno) = arms->child[i]->lineno;
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "Match expressions may only contain one default arm");
+ }
+ has_default_arm = 1;
+ }
+ }
+
+ /* Evaluate the subject once into a TMP we own. */
+ znode subj_tmp, subj;
+ zend_compile_expr(&subj_tmp, expr_ast);
+ zend_emit_op_tmp(&subj, ZEND_QM_ASSIGN, &subj_tmp, NULL);
+
+ uint32_t total_conds = 0;
+ for (uint32_t i = 0; i < arms->children; i++) {
+ if (arms->child[i]->child[0]) {
+ total_conds += zend_ast_get_list(arms->child[i]->child[0])->children;
+ }
+ }
+ uint32_t *cond_jmp = safe_emalloc(sizeof(uint32_t), total_conds ? total_conds : 1, 0);
+ uint32_t *end_jmp = safe_emalloc(sizeof(uint32_t), arms->children, 0);
+
+ /* Test phase: each condition jumps to its arm body on a match. */
+ uint32_t cc = 0;
+ for (uint32_t i = 0; i < arms->children; i++) {
+ zend_ast *arm = arms->child[i];
+ if (!arm->child[0]) {
+ continue;
+ }
+ zend_ast_list *conds = zend_ast_get_list(arm->child[0]);
+ for (uint32_t j = 0; j < conds->children; j++) {
+ zend_ast *cond = conds->child[j];
+ const char *tname; zend_ast *bvar; bool uerr;
+ znode test;
+ if (zend_match_pattern_info(cond, &tname, &bvar, &uerr)) {
+ znode copy;
+ zend_emit_op_tmp(©, ZEND_COPY_TMP, &subj, NULL);
+ zend_emit_pattern_instanceof(&test, ©, tname);
+ } else {
+ znode cond_node, case_node;
+ zend_compile_expr(&cond_node, cond);
+ case_node.op_type = IS_TMP_VAR;
+ case_node.u.op.var = get_temporary_variable();
+ zend_op *op = zend_emit_op(NULL, ZEND_CASE_STRICT, &subj, &cond_node);
+ SET_NODE(op->result, &case_node);
+ test = case_node;
+ }
+ cond_jmp[cc++] = zend_emit_cond_jump(ZEND_JMPNZ, &test, 0);
+ }
+ }
+
+ /* No arm matched: fall to the default body, or raise UnhandledMatchError. */
+ uint32_t default_jmp = zend_emit_jump(0);
+
+ /* Body phase. */
+ bool is_first = 1;
+ cc = 0;
+ for (uint32_t i = 0; i < arms->children; i++) {
+ zend_ast *arm = arms->child[i];
+ if (arm->child[0]) {
+ zend_ast_list *conds = zend_ast_get_list(arm->child[0]);
+ for (uint32_t j = 0; j < conds->children; j++) {
+ zend_update_jump_target_to_next(cond_jmp[cc++]);
+ }
+ /* Bind the payload for a single binding pattern. */
+ if (conds->children == 1) {
+ const char *tname; zend_ast *bvar; bool uerr;
+ if (zend_match_pattern_info(conds->child[0], &tname, &bvar, &uerr) && bvar) {
+ znode copy, val, var_node;
+ zend_emit_op_tmp(©, ZEND_COPY_TMP, &subj, NULL);
+ zend_emit_propagate_method(&val, ©, uerr ? "getError" : "getValue");
+ zend_compile_var(&var_node, bvar, BP_VAR_W, 0);
+ zend_emit_op(NULL, ZEND_ASSIGN, &var_node, &val);
+ }
+ }
+ } else {
+ zend_update_jump_target_to_next(default_jmp);
+ }
+
+ znode body_node;
+ zend_compile_expr(&body_node, arm->child[1]);
+ if (is_first) {
+ zend_emit_op_tmp(result, ZEND_QM_ASSIGN, &body_node, NULL);
+ is_first = 0;
+ } else {
+ zend_op *op = zend_emit_op(NULL, ZEND_QM_ASSIGN, &body_node, NULL);
+ SET_NODE(op->result, result);
+ }
+ end_jmp[i] = zend_emit_jump(0);
+ }
+
+ if (!has_default_arm) {
+ zend_update_jump_target_to_next(default_jmp);
+ zend_emit_op(NULL, ZEND_MATCH_ERROR, &subj, NULL);
+ }
+
+ for (uint32_t i = 0; i < arms->children; i++) {
+ zend_update_jump_target_to_next(end_jmp[i]);
+ }
+ zend_emit_op(NULL, ZEND_FREE, &subj, NULL);
+
+ efree(cond_jmp);
+ efree(end_jmp);
+}
+
static void zend_compile_match(znode *result, zend_ast *ast)
{
zend_ast *expr_ast = ast->child[0];
@@ -6550,6 +6741,11 @@ static void zend_compile_match(znode *result, zend_ast *ast)
bool has_default_arm = 0;
uint32_t opnum_match = (uint32_t)-1;
+ if (zend_match_has_patterns(arms)) {
+ zend_compile_match_patterns(result, ast);
+ return;
+ }
+
znode expr_node;
zend_compile_expr(&expr_node, expr_ast);
@@ -6729,6 +6925,93 @@ static void zend_compile_match(znode *result, zend_ast *ast)
efree(jmp_end_opnums);
}
+/* --- `for {} yield` comprehension desugar (arrays + Option/Result) --- */
+
+/* Build a call to the global function `name` with two args. */
+static zend_ast *fc_call(const char *name, zend_ast *arg1, zend_ast *arg2) /* {{{ */
+{
+ zend_ast *fname = zend_ast_create_zval_from_str(zend_string_init(name, strlen(name), 0));
+ fname->attr = ZEND_NAME_FQ;
+ zend_ast *args = zend_ast_create_list(2, ZEND_AST_ARG_LIST, arg1, arg2);
+ return zend_ast_create(ZEND_AST_CALL, fname, args);
+}
+/* }}} */
+
+/* Build the auto-capturing arrow function `fn ($<var>) => <body>`. */
+static zend_ast *fc_arrow(zend_string *var_name, zend_ast *body) /* {{{ */
+{
+ zend_ast *param = zend_ast_create_ex(ZEND_AST_PARAM, 0,
+ NULL, zend_ast_create_zval_from_str(zend_string_copy(var_name)),
+ NULL, NULL, NULL, NULL);
+ zend_ast *params = zend_ast_create_list(1, ZEND_AST_PARAM_LIST, param);
+ return zend_ast_create_decl(ZEND_AST_ARROW_FUNC, 0, CG(zend_lineno), NULL, NULL,
+ params, NULL, body, NULL, NULL);
+}
+/* }}} */
+
+/* Desugar `for { $x = A; if G; $y = B; ... } yield R` to nested flatMap/map/filter
+ * over the internal dispatch functions, so arrays and Option/Result share one path.
+ * Reused subtrees are detached from the source AST and freed via the synthesized
+ * tree after compilation. */
+static void zend_compile_comprehension(znode *result, zend_ast *ast) /* {{{ */
+{
+ zend_ast_list *clauses = zend_ast_get_list(ast->child[0]);
+ zend_ast *gen_var[64];
+ zend_ast *gen_src[64];
+ uint32_t n = 0;
+
+ for (uint32_t i = 0; i < clauses->children; i++) {
+ zend_ast *c = clauses->child[i];
+ if (c->kind == ZEND_AST_COMPREHENSION_GEN) {
+ if (n >= 64) {
+ zend_error_noreturn(E_COMPILE_ERROR, "Too many generators in for-comprehension");
+ }
+ gen_var[n] = c->child[0]; /* ZEND_AST_VAR (left attached) */
+ gen_src[n] = c->child[1]; /* source expr — detach */
+ c->child[1] = NULL;
+ n++;
+ } else { /* ZEND_AST_COMPREHENSION_FILTER */
+ if (n == 0) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "for-comprehension guard must follow a generator");
+ }
+ zend_string *vn = zend_ast_get_str(gen_var[n - 1]->child[0]);
+ zend_ast *guard = c->child[0];
+ c->child[0] = NULL;
+ gen_src[n - 1] = fc_call("__fc_filter", gen_src[n - 1], fc_arrow(vn, guard));
+ }
+ }
+
+ if (n == 0) {
+ zend_error_noreturn(E_COMPILE_ERROR,
+ "for-comprehension requires at least one generator");
+ }
+
+ zend_ast *body = ast->child[1]; /* yield expr — detach */
+ ast->child[1] = NULL;
+
+ zend_ast *acc = fc_call("__fc_map", gen_src[n - 1],
+ fc_arrow(zend_ast_get_str(gen_var[n - 1]->child[0]), body));
+ for (int32_t i = (int32_t) n - 2; i >= 0; i--) {
+ acc = fc_call("__fc_flatMap", gen_src[i],
+ fc_arrow(zend_ast_get_str(gen_var[i]->child[0]), acc));
+ }
+
+ /* A dict comprehension (`yield $k => $v`) yields a flat list of [key, value]
+ * pairs; fold it into an associative array (later keys win). */
+ if (ast->attr) {
+ zend_ast *fname = zend_ast_create_zval_from_str(
+ zend_string_init("__fc_pairs_to_map", sizeof("__fc_pairs_to_map") - 1, 0));
+ fname->attr = ZEND_NAME_FQ;
+ zend_ast *args = zend_ast_create_list(1, ZEND_AST_ARG_LIST, acc);
+ acc = zend_ast_create(ZEND_AST_CALL, fname, args);
+ }
+
+ zend_compile_expr(result, acc);
+ zend_ast_destroy(acc);
+}
+/* }}} */
+
static void zend_compile_try(zend_ast *ast) /* {{{ */
{
zend_ast *try_ast = ast->child[0];
@@ -10559,27 +10842,121 @@ static void zend_compile_conditional(znode *result, zend_ast *ast) /* {{{ */
}