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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#![allow(clippy::pattern_type_mismatch)]
//! `count` / `count.where` compilation and count-binding resolution.
use alloc::format;
use alloc::string::{String, ToString as _};
use alloc::vec::Vec;
use anyhow::{anyhow, bail, Result};
use crate::languages::azure_policy::ast::{
Condition, Constraint, CountNode, FieldKind, JsonValue, OperatorKind, ValueOrExpr,
};
use crate::rvm::instructions::{GuardMode, LoopMode, LoopStartParams, PolicyOp};
use crate::rvm::Instruction;
use crate::Value;
use super::core::{Compiler, CountBinding};
use super::utils::{split_count_wildcard_path, split_path_without_wildcards};
impl Compiler {
pub(super) fn compile_count(&mut self, count_node: &CountNode) -> Result<u8> {
self.observed_uses_count = true;
match count_node {
CountNode::Value {
span,
value,
name,
where_,
} => {
let collection_reg = self.compile_value_or_expr(value, span)?;
self.compile_count_loop(
collection_reg,
name.as_ref().map(|n| n.name.clone()),
None,
where_.as_deref(),
span,
)
}
CountNode::Field {
span,
field,
where_,
} => {
let field_path = self.extract_field_count_path(field, span)?;
let (_prefix, suffix) = split_count_wildcard_path(&field_path)
.map_err(|e| span.error(&e.to_string()))?;
// Multi-level wildcard (e.g. `A[*].B[*]`) → emit nested loops.
// If an outer count binding covers part of the path, start
// from the bound element instead of the resource root.
if suffix.as_ref().is_some_and(|s| s.contains("[*]")) {
if let Some(binding) = self.resolve_count_binding(&field_path)? {
if let Some(outer_prefix) = &binding.field_wildcard_prefix {
let lc_prefix = outer_prefix.to_ascii_lowercase();
let wildcard_dot = format!("{}[*].", lc_prefix);
if let Some(inner_path) =
field_path.to_ascii_lowercase().strip_prefix(&wildcard_dot)
{
let inner_path = inner_path.to_string();
return self.compile_count_nested(
Some(binding.current_reg),
&inner_path,
where_.as_deref(),
outer_prefix,
span,
);
}
}
}
return self.compile_count_nested(
None,
&field_path,
where_.as_deref(),
"",
span,
);
}
// Single wildcard → existing path via resolve + single count loop.
let (collection_reg, prefix) = self.resolve_count_field_collection(field, span)?;
self.compile_count_loop(collection_reg, None, Some(prefix), where_.as_deref(), span)
}
}
}
/// Resolve the collection register and wildcard prefix for a field-based
/// count node, handling nested count bindings.
fn resolve_count_field_collection(
&mut self,
field: &crate::languages::azure_policy::ast::FieldNode,
span: &crate::lexer::Span,
) -> Result<(u8, String)> {
let field_path = self.extract_field_count_path(field, span)?;
let (collection_prefix, suffix) =
split_count_wildcard_path(&field_path).map_err(|e| span.error(&e.to_string()))?;
// Check if this field path is relative to an outer count binding.
if let Some(binding) = self.resolve_count_binding(&field_path)? {
if let Some(outer_prefix) = &binding.field_wildcard_prefix {
let lc_prefix = outer_prefix.to_ascii_lowercase();
let wildcard_dot = format!("{}[*].", lc_prefix);
if let Some(inner_path) =
field_path.to_ascii_lowercase().strip_prefix(&wildcard_dot)
{
let inner_path = inner_path.to_string();
if inner_path.contains("[*]") {
let (inner_collection, _) = split_count_wildcard_path(&inner_path)
.map_err(|e| span.error(&e.to_string()))?;
let inner_collection = inner_collection.to_ascii_lowercase();
let parts = split_path_without_wildcards(&inner_collection)?;
let refs = parts.iter().map(String::as_str).collect::<Vec<_>>();
let collection_reg =
self.emit_chained_index_literal_path(binding.current_reg, &refs, span)?;
let inner_prefix = format!("{}[*].{}", lc_prefix, inner_collection);
return Ok((collection_reg, inner_prefix));
}
}
}
}
// Multi-level wildcard: now handled by compile_count_nested in compile_count.
// (Single-wildcard paths fall through to here.)
if suffix.as_ref().is_some_and(|s| s.contains("[*]")) {
bail!(span.error(&format!(
"multi-wildcard path should have been handled before resolve_count_field_collection: {}",
field_path
)));
}
let collection_reg = self.compile_resource_path_value(&collection_prefix, span)?;
Ok((collection_reg, collection_prefix))
}
/// Compile a multi-wildcard count path as nested loops.
///
/// Each intermediate `[*]` level emits a `ForEach` loop that accumulates
/// the inner count. The innermost `[*]` emits the real count loop with
/// the where clause and binding.
///
/// * `base_reg` — `None` for resource root, `Some` when inside an outer loop.
/// * `remaining_path` — the portion of the field path still to process;
/// must contain at least one `[*]`.
/// * `where_clause` — the optional where constraint (applied only at the
/// innermost level).
/// * `accumulated_prefix` — the path prefix accumulated from outer levels,
/// used to build binding prefixes.
fn compile_count_nested(
&mut self,
base_reg: Option<u8>,
remaining_path: &str,
where_clause: Option<&Constraint>,
accumulated_prefix: &str,
span: &crate::lexer::Span,
) -> Result<u8> {
let (collection_part, suffix) =
split_count_wildcard_path(remaining_path).map_err(|e| span.error(&e.to_string()))?;
let has_more_wildcards = suffix.as_ref().is_some_and(|s| s.contains("[*]"));
// Build the binding prefix for this level.
let binding_prefix = if accumulated_prefix.is_empty() {
collection_part.clone()
} else {
format!("{}[*].{}", accumulated_prefix, collection_part)
};
// Lowercase the collection path to match normalized resource keys.
let collection_lower = collection_part.to_ascii_lowercase();
// Navigate to the collection. `split_count_wildcard_path` guarantees
// the collection segment before `[*]` is non-empty.
let collection_reg = match base_reg {
Some(base) => {
let parts = split_path_without_wildcards(&collection_lower)?;
let refs = parts.iter().map(String::as_str).collect::<Vec<_>>();
self.emit_chained_index_literal_path(base, &refs, span)?
}
None => self.compile_resource_path_value(&collection_lower, span)?,
};
if !has_more_wildcards {
// Innermost wildcard → delegate to the regular count loop.
// Optimization: if no where clause, just emit Count instruction.
// Note: Count returns Undefined for non-iterable collections,
// which differs from LoopMode::Any (treats them as empty). The
// existence-pattern optimizer (`try_compile_count_as_any`) skips
// nested-wildcard no-where counts so this path is always taken
// for that case, preserving Undefined-propagation semantics.
if where_clause.is_none() {
let dest = self.alloc_register()?;
self.emit(
Instruction::Count {
dest,
collection: collection_reg,
},
span,
);
return Ok(dest);
}
return self.compile_count_loop(
collection_reg,
None,
Some(binding_prefix),
where_clause,
span,
);
}
// Intermediate wildcard → ForEach loop that accumulates inner counts.
let count_reg = self.load_literal(Value::from(0_i64), span)?;
let key_reg = self.alloc_register()?;
let current_reg = self.alloc_register()?;
let loop_result_reg = self.alloc_register()?;
let params_index = self.program.add_loop_params(LoopStartParams {
mode: LoopMode::ForEach,
collection: collection_reg,
key_reg,
value_reg: current_reg,
result_reg: loop_result_reg,
body_start: 0,
loop_end: 0,
});
self.emit(Instruction::LoopStart { params_index }, span);
let body_start = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
// Push binding for this level so inner where-clause field references
// can resolve through this wildcard level.
self.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some(binding_prefix.clone()),
current_reg,
});
// Recurse for the inner level(s).
let suffix_ref = suffix
.as_ref()
.ok_or_else(|| anyhow::anyhow!("suffix should be Some for nested count"))?;
let inner_count = self.compile_count_nested(
Some(current_reg),
suffix_ref,
where_clause,
&binding_prefix,
span,
)?;
// Accumulate inner count into outer count.
self.emit(
Instruction::Add {
dest: count_reg,
left: count_reg,
right: inner_count,
},
span,
);
self.count_bindings.pop();
self.emit(
Instruction::LoopNext {
body_start,
loop_end: 0,
},
span,
);
let loop_end = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
self.program.update_loop_params(params_index, |params| {
params.body_start = body_start;
params.loop_end = loop_end;
});
if let Some(Instruction::LoopNext { loop_end: le, .. }) =
self.program.instructions.last_mut()
{
*le = loop_end;
}
Ok(count_reg)
}
/// Compile a multi-wildcard count path as nested `Any` loops for the
/// `count > 0` / `count == 0` existence-pattern optimization.
///
/// Each intermediate `[*]` level emits an `Any` loop whose body is the
/// next level. The innermost `[*]` emits `compile_count_any_loop` with
/// the where clause. If `exists` is false the result is negated.
///
/// **Important:** This must only be called when `where_clause` is `Some`.
/// Without a where clause the non-optimized path (`compile_count_nested`)
/// uses `Instruction::Count` at the innermost level. That instruction
/// returns `Undefined` for missing/non-iterable collections, whereas the
/// `Any` loop treats them as empty (false). The difference changes the
/// semantics of `count == 0` from false (via Undefined propagation) to
/// true (via `Not(false)`). The caller (`try_compile_count_as_any`)
/// returns `None` for no-where nested counts so the generic count+compare
/// path is used instead.
fn compile_count_nested_any(
&mut self,
base_reg: Option<u8>,
remaining_path: &str,
where_clause: &Constraint,
accumulated_prefix: &str,
exists: bool,
span: &crate::lexer::Span,
) -> Result<Option<u8>> {
let (collection_part, suffix) =
split_count_wildcard_path(remaining_path).map_err(|e| span.error(&e.to_string()))?;
let has_more_wildcards = suffix.as_ref().is_some_and(|s| s.contains("[*]"));
let binding_prefix = if accumulated_prefix.is_empty() {
collection_part.clone()
} else {
format!("{}[*].{}", accumulated_prefix, collection_part)
};
// Lowercase the collection path to match normalized resource keys.
let collection_lower = collection_part.to_ascii_lowercase();
// Navigate to the collection. `split_count_wildcard_path` guarantees
// the collection segment before `[*]` is non-empty.
let collection_reg = match base_reg {
Some(base) => {
let parts = split_path_without_wildcards(&collection_lower)?;
let refs = parts.iter().map(String::as_str).collect::<Vec<_>>();
self.emit_chained_index_literal_path(base, &refs, span)?
}
None => self.compile_resource_path_value(&collection_lower, span)?,
};
if !has_more_wildcards {
// Innermost wildcard → regular Any loop.
let any_result = self.compile_count_any_loop(
collection_reg,
None,
Some(binding_prefix),
Some(where_clause),
span,
)?;
return if exists {
Ok(Some(any_result))
} else {
let dest = self.alloc_register()?;
self.emit(
Instruction::PolicyCondition {
dest,
left: any_result,
right: 0,
op: PolicyOp::Not,
},
span,
);
Ok(Some(dest))
};
}
// Intermediate wildcard → Any loop wrapping inner nested Any.
let key_reg = self.alloc_register()?;
let current_reg = self.alloc_register()?;
let result_reg = self.alloc_register()?;
let params_index = self.program.add_loop_params(LoopStartParams {
mode: LoopMode::Any,
collection: collection_reg,
key_reg,
value_reg: current_reg,
result_reg,
body_start: 0,
loop_end: 0,
});
self.emit(Instruction::LoopStart { params_index }, span);
let body_start = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
// Push binding for this level.
self.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some(binding_prefix.clone()),
current_reg,
});
// Recurse — the inner call returns Some(result_reg) with the final
// negation already applied at the innermost level. For the outer
// Any loop, we need "any inner satisfies" so we pass `exists = true`
// here and handle the overall negation at the end.
let suffix_ref = suffix
.as_ref()
.ok_or_else(|| anyhow::anyhow!("suffix should be Some for nested any"))?;
let inner = self
.compile_count_nested_any(
Some(current_reg),
suffix_ref,
where_clause,
&binding_prefix,
/* exists */ true,
span,
)?
.ok_or_else(|| anyhow::anyhow!("nested any should always return Some"))?;
// The outer Any body succeeds when the inner Any returned true.
self.emit(
Instruction::Guard {
register: inner,
mode: GuardMode::Condition,
},
span,
);
self.count_bindings.pop();
self.emit(
Instruction::LoopNext {
body_start,
loop_end: 0,
},
span,
);
let loop_end = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
self.program.update_loop_params(params_index, |params| {
params.body_start = body_start;
params.loop_end = loop_end;
});
if let Some(Instruction::LoopNext { loop_end: le, .. }) =
self.program.instructions.last_mut()
{
*le = loop_end;
}
// If !exists (count == 0), negate the Any result.
if exists {
Ok(Some(result_reg))
} else {
let dest = self.alloc_register()?;
self.emit(
Instruction::PolicyCondition {
dest,
left: result_reg,
right: 0,
op: PolicyOp::Not,
},
span,
);
Ok(Some(dest))
}
}
/// Map a [`FieldNode`] to the dotted property path used for count
/// resolution. Built-in field kinds (`type`, `id`, …) are returned
/// as-is; aliases go through [`resolve_alias_path`] which normalises
/// and lowercases when the alias catalog is loaded.
fn extract_field_count_path(
&self,
field: &crate::languages::azure_policy::ast::FieldNode,
span: &crate::lexer::Span,
) -> Result<String> {
match &field.kind {
FieldKind::Type => Ok("type".to_string()),
FieldKind::Id => Ok("id".to_string()),
FieldKind::Kind => Ok("kind".to_string()),
FieldKind::Name => Ok("name".to_string()),
FieldKind::Location => Ok("location".to_string()),
FieldKind::FullName => Ok("fullName".to_string()),
FieldKind::IdentityType => Ok("identity.type".to_string()),
FieldKind::IdentityField(subpath) => {
Ok(format!("identity.{}", subpath.to_ascii_lowercase()))
}
FieldKind::ApiVersion => Ok("apiVersion".to_string()),
FieldKind::Tags => Ok("tags".to_string()),
FieldKind::Tag(tag) => Ok(format!("tags.{}", tag)),
FieldKind::Alias(path) => self.resolve_alias_path(path, span),
FieldKind::Expr(_) => {
bail!(span.error("count over expression field is not supported in core subset",))
}
}
}
/// Emit a single-level `ForEach` count loop.
///
/// Iterates `collection_reg`, pushes a [`CountBinding`] for the duration
/// of the loop body (so nested `current()` / field references resolve),
/// optionally guards with the where clause, and increments a counter
/// register on each passing iteration.
///
/// Returns the register holding the final count.
fn compile_count_loop(
&mut self,
collection_reg: u8,
binding_name: Option<String>,
field_wildcard_prefix: Option<String>,
where_constraint: Option<&Constraint>,
span: &crate::lexer::Span,
) -> Result<u8> {
let count_reg = self.load_literal(Value::from(0_i64), span)?;
// Hoist the increment constant above the loop.
let one_reg = self.load_literal(Value::from(1_i64), span)?;
let key_reg = self.alloc_register()?;
let current_reg = self.alloc_register()?;
let loop_result_reg = self.alloc_register()?;
let params_index = self.program.add_loop_params(LoopStartParams {
mode: LoopMode::ForEach,
collection: collection_reg,
key_reg,
value_reg: current_reg,
result_reg: loop_result_reg,
body_start: 0,
loop_end: 0,
});
self.emit(Instruction::LoopStart { params_index }, span);
let body_start_u16 = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
self.count_bindings.push(CountBinding {
name: binding_name,
field_wildcard_prefix,
current_reg,
});
// Compile where clause body (if present) as a conditional increment.
if let Some(where_clause) = where_constraint {
let where_reg = self.compile_constraint(where_clause)?;
self.emit(
Instruction::Guard {
register: where_reg,
mode: GuardMode::Condition,
},
span,
);
}
self.emit(
Instruction::Add {
dest: count_reg,
left: count_reg,
right: one_reg,
},
span,
);
self.count_bindings.pop();
self.emit(
Instruction::LoopNext {
body_start: body_start_u16,
loop_end: 0,
},
span,
);
let loop_end_u16 = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
self.program.update_loop_params(params_index, |params| {
params.body_start = body_start_u16;
params.loop_end = loop_end_u16;
});
if let Some(Instruction::LoopNext { loop_end, .. }) = self.program.instructions.last_mut() {
*loop_end = loop_end_u16;
}
Ok(count_reg)
}
// -- count existence optimization (Any mode) ---------------------------
/// Try to compile a count condition as a `LoopMode::Any` loop when the
/// operator + RHS form an existence check (e.g., `count > 0`).
///
/// Returns `Some(result_reg)` if optimized, `None` to fall back to the
/// generic count + compare path.
pub(super) fn try_compile_count_as_any(
&mut self,
count_node: &CountNode,
condition: &Condition,
) -> Result<Option<u8>> {
// Determine whether the operator+RHS is an existence pattern.
let exists = match Self::classify_existence_pattern(condition) {
Some(e) => e,
None => return Ok(None),
};
// Keep the where clause optional so plain `count(field: 'a[*]') > 0`
// can also use the early-exit Any lowering.
let where_constraint = match count_node {
CountNode::Field { where_, .. } | CountNode::Value { where_, .. } => where_.as_deref(),
};
self.observed_uses_count = true;
// Resolve collection and compile as Any loop.
let any_result = match count_node {
CountNode::Value {
span, value, name, ..
} => {
let collection_reg = self.compile_value_or_expr(value, span)?;
self.compile_count_any_loop(
collection_reg,
name.as_ref().map(|n| n.name.clone()),
None,
where_constraint,
span,
)?
}
CountNode::Field { span, field, .. } => {
// Multi-wildcard field paths use nested Any loops.
// Resolve outer bindings so we start from the bound element.
let field_path = self.extract_field_count_path(field, span)?;
let (_, suffix) = split_count_wildcard_path(&field_path)
.map_err(|e| span.error(&e.to_string()))?;
if suffix.as_ref().is_some_and(|s| s.contains("[*]")) {
// Skip the nested Any optimization when there is no where
// clause. The non-optimized path in `compile_count_nested`
// uses `Instruction::Count` for the innermost level, which
// returns `Undefined` when the collection is missing or
// non-iterable. The Any-based lowering instead treats a
// missing collection as empty (Any → false), so
// `Not(false)` → true, changing `count == 0` from false to
// true. Falling back to the generic count+compare path
// preserves the Undefined-propagation semantics.
let Some(wc) = where_constraint else {
return Ok(None);
};
if let Some(binding) = self.resolve_count_binding(&field_path)? {
if let Some(outer_prefix) = &binding.field_wildcard_prefix {
let lc_prefix = outer_prefix.to_ascii_lowercase();
let expected_prefix = format!("{}[*].", lc_prefix);
if let Some(inner_path) = field_path
.to_ascii_lowercase()
.strip_prefix(&expected_prefix)
{
let inner_path = inner_path.to_string();
return self.compile_count_nested_any(
Some(binding.current_reg),
&inner_path,
wc,
outer_prefix,
exists,
span,
);
}
}
}
return self.compile_count_nested_any(None, &field_path, wc, "", exists, span);
}
let (collection_reg, prefix) = self.resolve_count_field_collection(field, span)?;
self.compile_count_any_loop(
collection_reg,
None,
Some(prefix),
where_constraint,
span,
)?
}
};
if exists {
Ok(Some(any_result))
} else {
let dest = self.alloc_register()?;
self.emit(
Instruction::PolicyCondition {
dest,
left: any_result,
right: 0,
op: PolicyOp::Not,
},
&condition.span,
);
Ok(Some(dest))
}
}
/// Check whether a count condition's operator + RHS form an existence
/// pattern. Returns `Some(true)` for "at least one" semantics,
/// `Some(false)` for "none" semantics, or `None` if not applicable.
fn classify_existence_pattern(condition: &Condition) -> Option<bool> {
let n = match &condition.rhs {
ValueOrExpr::Value(JsonValue::Number(_, s)) => s.parse::<i64>().ok()?,
_ => return None,
};
match (&condition.operator.kind, n) {
(OperatorKind::Greater, 0)
| (OperatorKind::GreaterOrEquals, 1)
| (OperatorKind::NotEquals, 0) => Some(true),
(OperatorKind::Equals, 0)
| (OperatorKind::Less, 1)
| (OperatorKind::LessOrEquals, 0) => Some(false),
_ => None,
}
}
/// Compile a count's where clause as a `LoopMode::Any` loop.
///
/// The result register is `true` if any element satisfies the where
/// constraint (or simply exists when `where_constraint` is `None`),
/// `false` otherwise. The loop exits on the first match.
fn compile_count_any_loop(
&mut self,
collection_reg: u8,
binding_name: Option<String>,
field_wildcard_prefix: Option<String>,
where_constraint: Option<&Constraint>,
span: &crate::lexer::Span,
) -> Result<u8> {
let key_reg = self.alloc_register()?;
let current_reg = self.alloc_register()?;
let result_reg = self.alloc_register()?;
let params_index = self.program.add_loop_params(LoopStartParams {
mode: LoopMode::Any,
collection: collection_reg,
key_reg,
value_reg: current_reg,
result_reg,
body_start: 0,
loop_end: 0,
});
self.emit(Instruction::LoopStart { params_index }, span);
let body_start = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
self.count_bindings.push(CountBinding {
name: binding_name,
field_wildcard_prefix,
current_reg,
});
if let Some(wc) = where_constraint {
let where_reg = self.compile_constraint(wc)?;
self.emit(
Instruction::Guard {
register: where_reg,
mode: GuardMode::Condition,
},
span,
);
}
self.count_bindings.pop();
self.emit(
Instruction::LoopNext {
body_start,
loop_end: 0,
},
span,
);
let loop_end = u16::try_from(self.program.instructions.len())
.map_err(|_| anyhow!("instruction index overflow"))?;
self.program.update_loop_params(params_index, |params| {
params.body_start = body_start;
params.loop_end = loop_end;
});
if let Some(Instruction::LoopNext { loop_end: le, .. }) =
self.program.instructions.last_mut()
{
*le = loop_end;
}
Ok(result_reg)
}
/// Find the innermost active count binding that covers `field_path`.
///
/// Matching rules (all case-insensitive):
/// 1. **Named binding** — `field_path` equals the binding's `name`.
/// 2. **Wildcard prefix** — `field_path` matches the binding's prefix,
/// its `prefix[*]` form, or starts with `prefix.` / `prefix[*].`.
///
/// Bindings are searched innermost-first (reverse stack order) so a
/// nested count's binding shadows an outer one for the same prefix.
pub(super) fn resolve_count_binding(&self, field_path: &str) -> Result<Option<CountBinding>> {
let fp = field_path.to_ascii_lowercase();
for binding in self.count_bindings.iter().rev() {
if let Some(name) = &binding.name {
if fp.eq_ignore_ascii_case(name) {
return Ok(Some(binding.clone()));
}
}
if let Some(prefix) = &binding.field_wildcard_prefix {
let lc_prefix = prefix.to_ascii_lowercase();
let wildcard_prefix = format!("{}[*]", lc_prefix);
let prefix_dot = format!("{}.", lc_prefix);
let wildcard_dot = format!("{}.", wildcard_prefix);
if fp == lc_prefix
|| fp.starts_with(&prefix_dot)
|| fp == wildcard_prefix
|| fp.starts_with(&wildcard_dot)
{
return Ok(Some(binding.clone()));
}
}
}
Ok(None)
}
/// Compile a field reference relative to an active count binding.
///
/// If `field_path` matches the binding exactly (name or prefix),
/// emits a `Move` from the binding's current-element register.
/// If `field_path` extends past the binding (e.g. `prefix.sub.key`),
/// navigates the suffix via chained index lookups. All comparisons
/// are case-insensitive.
pub(super) fn compile_from_binding(
&mut self,
binding: &CountBinding,
field_path: &str,
span: &crate::lexer::Span,
) -> Result<u8> {
let fp = field_path.to_ascii_lowercase();
if let Some(name) = &binding.name {
if fp.eq_ignore_ascii_case(name) {
let dest = self.alloc_register()?;
self.emit(
Instruction::Move {
dest,
src: binding.current_reg,
},
span,
);
return Ok(dest);
}
}
if let Some(prefix) = &binding.field_wildcard_prefix {
let lc_prefix = prefix.to_ascii_lowercase();
let wildcard_prefix = format!("{}[*]", lc_prefix);
if fp == lc_prefix || fp == wildcard_prefix {
let dest = self.alloc_register()?;
self.emit(
Instruction::Move {
dest,
src: binding.current_reg,
},
span,
);
return Ok(dest);
}
let prefix_dot = format!("{}.", lc_prefix);
if let Some(suffix) = fp.strip_prefix(&prefix_dot) {
return self.compile_suffix_from_binding(binding.current_reg, suffix, span);
}
let wildcard_dot = format!("{}[*].", lc_prefix);
if let Some(suffix) = fp.strip_prefix(&wildcard_dot) {
return self.compile_suffix_from_binding(binding.current_reg, suffix, span);
}
}
bail!(span.error(&format!(
"invalid current count binding for field path '{}'",
field_path
)))
}
/// Compile a suffix path from a binding's current register.
///
/// If the suffix contains `[*]` (from a nested count context), only the
/// portion before the first `[*]` is used for navigation. The inner
/// count's loop will handle the iteration.
fn compile_suffix_from_binding(
&mut self,
base_reg: u8,
suffix: &str,
span: &crate::lexer::Span,
) -> Result<u8> {
// Strip any trailing [*] or [*].suffix — we only navigate to the
// array itself; the count loop iterates its elements.
let nav_path = suffix
.split_once("[*]")
.map_or(suffix, |(prefix, _)| prefix);
// Lowercase to match normalizer-lowercased keys.
let nav_path = nav_path.to_ascii_lowercase();
let parts = split_path_without_wildcards(&nav_path)?;
let refs = parts.iter().map(String::as_str).collect::<Vec<_>>();
self.emit_chained_index_literal_path(base_reg, &refs, span)
}
/// Compile a `current('key')` reference inside a count's where clause.
///
/// Resolution is two-phase:
/// 1. Try matching `key` directly against the active binding stack
/// (case-insensitive). This handles literal alias paths and
/// named value-count bindings.
/// 2. If no direct match, resolve `key` through the alias catalog
/// and retry. When the catalog is loaded and fallback is disabled,
/// alias-resolution errors propagate so the caller sees "unknown
/// alias" rather than a generic scope error.
///
/// Bails with a "used outside an active count scope" error if neither
/// phase finds a matching binding.
pub(super) fn compile_current_reference(
&mut self,
key: &str,
span: &crate::lexer::Span,
) -> Result<u8> {
let resolve_for_key = |compiler: &mut Self, candidate: &str| -> Result<Option<u8>> {
let lc_candidate = candidate.to_ascii_lowercase();
for binding in compiler.count_bindings.iter().rev() {
if let Some(name) = &binding.name {
let lc_name = name.to_ascii_lowercase();
if lc_candidate == lc_name {
let current_reg = binding.current_reg;
let dest = compiler.alloc_register()?;
compiler.emit(
Instruction::Move {
dest,
src: current_reg,
},
span,
);
return Ok(Some(dest));
}
let name_dot = format!("{}.", lc_name);
if let Some(suffix) = lc_candidate.strip_prefix(&name_dot) {
let parts = split_path_without_wildcards(suffix)?;
let refs = parts.iter().map(String::as_str).collect::<Vec<_>>();
return compiler
.emit_chained_index_literal_path(binding.current_reg, &refs, span)
.map(Some);
}
}
if let Some(prefix) = &binding.field_wildcard_prefix {
let lc_prefix = prefix.to_ascii_lowercase();
if lc_candidate == lc_prefix || lc_candidate == format!("{}[*]", lc_prefix) {
let current_reg = binding.current_reg;
let dest = compiler.alloc_register()?;
compiler.emit(
Instruction::Move {
dest,
src: current_reg,
},
span,
);
return Ok(Some(dest));
}
let prefix_dot = format!("{}.", lc_prefix);
if let Some(suffix) = lc_candidate.strip_prefix(&prefix_dot) {
return compiler
.compile_suffix_from_binding(binding.current_reg, suffix, span)
.map(Some);
}
let prefix_wildcard_dot = format!("{}[*].", lc_prefix);
if let Some(suffix) = lc_candidate.strip_prefix(&prefix_wildcard_dot) {
return compiler
.compile_suffix_from_binding(binding.current_reg, suffix, span)
.map(Some);
}
}
}
Ok(None)
};
if let Some(result) = resolve_for_key(self, key)? {
return Ok(result);
}
// Try resolving via the alias catalog. When the catalog is loaded
// and fallback is disabled, propagate alias-resolution errors so the
// caller sees "unknown alias" instead of the generic "outside an
// active count scope" message.
match self.resolve_alias_path(key, span) {
Ok(normalized_key) if normalized_key != key => {
if let Some(result) = resolve_for_key(self, &normalized_key)? {
return Ok(result);
}
}
Err(e)
if self
.alias_registry
.as_ref()
.is_some_and(|r| !r.alias_map().is_empty())
&& !self.alias_fallback_to_raw =>
{
return Err(e);
}
_ => {}
}
bail!(span.error(&format!(
"current('{}') is used outside an active count scope",
key
)))
}
}
// ===========================================================================
// Tests
// ===========================================================================
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
mod tests {
use alloc::string::ToString as _;
use alloc::vec;
use alloc::vec::Vec;
use crate::languages::azure_policy::ast::{
Condition, Constraint, CountNode, FieldKind, FieldNode, JsonValue, OperatorKind,
OperatorNode, ValueOrExpr,
};
use crate::languages::azure_policy::compiler::core::{Compiler, CountBinding};
use crate::lexer::Source;
use crate::rvm::instructions::{GuardMode, LoopMode, PolicyOp};
use crate::rvm::Instruction;
fn dummy_span() -> crate::lexer::Span {
let source = Source::from_contents("test".into(), " ".into()).unwrap();
crate::lexer::Span {
source,
line: 1,
col: 1,
start: 0,
end: 0,
}
}
// -----------------------------------------------------------------------
// resolve_count_binding
// -----------------------------------------------------------------------
#[test]
fn resolve_binding_empty_stack() {
let c = Compiler::new();
assert!(c.resolve_count_binding("a[*].b").unwrap().is_none());
}
#[test]
fn resolve_binding_by_field_prefix() {
let mut c = Compiler::new();
c.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some("a".to_string()),
current_reg: 5,
});
let binding = c.resolve_count_binding("a[*].b").unwrap().unwrap();
assert_eq!(binding.current_reg, 5);
assert_eq!(binding.field_wildcard_prefix.as_deref(), Some("a"));
}
#[test]
fn resolve_binding_by_name() {
let mut c = Compiler::new();
c.count_bindings.push(CountBinding {
name: Some("myCollection".to_string()),
field_wildcard_prefix: None,
current_reg: 3,
});
let binding = c.resolve_count_binding("myCollection").unwrap().unwrap();
assert_eq!(binding.current_reg, 3);
}
#[test]
fn resolve_binding_case_insensitive() {
let mut c = Compiler::new();
c.count_bindings.push(CountBinding {
name: Some("MyCollection".to_string()),
field_wildcard_prefix: None,
current_reg: 4,
});
// Lookup with different casing should still match.
let binding = c.resolve_count_binding("mycollection").unwrap().unwrap();
assert_eq!(binding.current_reg, 4);
let binding_upper = c.resolve_count_binding("MYCOLLECTION").unwrap().unwrap();
assert_eq!(binding_upper.current_reg, 4);
}
#[test]
fn resolve_binding_field_prefix_case_insensitive() {
let mut c = Compiler::new();
c.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some("Microsoft.Test/resource".to_string()),
current_reg: 6,
});
// Mixed-case lookup against the prefix.
let binding = c
.resolve_count_binding("microsoft.test/resource[*].prop")
.unwrap()
.unwrap();
assert_eq!(binding.current_reg, 6);
}
#[test]
fn resolve_binding_innermost_wins() {
let mut c = Compiler::new();
c.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some("a".to_string()),
current_reg: 1,
});
c.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some("a[*].b".to_string()),
current_reg: 2,
});
// The inner binding (a[*].b) matches a[*].b[*].c, and since we
// iterate in reverse, it wins.
let binding = c.resolve_count_binding("a[*].b[*].c").unwrap().unwrap();
assert_eq!(binding.current_reg, 2);
}
#[test]
fn resolve_binding_no_match() {
let mut c = Compiler::new();
c.count_bindings.push(CountBinding {
name: None,
field_wildcard_prefix: Some("x".to_string()),
current_reg: 1,
});
assert!(c.resolve_count_binding("y[*].z").unwrap().is_none());
}
// -----------------------------------------------------------------------
// compile_count_nested — instruction shape for multi-wildcard paths
// -----------------------------------------------------------------------
#[test]
fn nested_count_no_where_emits_foreach_and_count() {
let mut c = Compiler::new();
let span = dummy_span();
// Compile a[*].b[*] (no where clause) starting from resource root.
let result_reg = c
.compile_count_nested(None, "a[*].b[*]", None, "", &span)
.unwrap();
// The outer loop should be ForEach (accumulating inner counts).
// Find the first LoopStart and check its mode.
let first_loop_idx = c
.program
.instructions
.iter()
.position(|i| matches!(i, Instruction::LoopStart { .. }))
.expect("should have a LoopStart");
if let Instruction::LoopStart { params_index } = c.program.instructions[first_loop_idx] {
let params = c
.program
.instruction_data
.get_loop_params(params_index)
.unwrap();
assert_eq!(
params.mode,
LoopMode::ForEach,
"outer loop should be ForEach"
);
}
// The innermost level has no where clause, so it should use Count
// instruction (direct count, no loop).
assert!(
c.program
.instructions
.iter()
.any(|i| matches!(i, Instruction::Count { .. })),
"innermost level without where should emit Count"
);
// Should also have an Add instruction to accumulate.
assert!(
c.program
.instructions
.iter()
.any(|i| matches!(i, Instruction::Add { .. })),
"should accumulate inner counts via Add"
);
// The result register should be valid.
assert!(result_reg < c.register_counter);
}
#[test]
fn nested_count_with_where_emits_foreach_loops() {
let mut c = Compiler::new();
let span = dummy_span();
// A simple where clause: { field: "type", equals: "someType" }
let where_clause = Constraint::Condition(alloc::boxed::Box::new(Condition {
span: dummy_span(),
lhs: crate::languages::azure_policy::ast::Lhs::Field(FieldNode {
span: dummy_span(),
kind: FieldKind::Type,
}),
operator: OperatorNode {
span: dummy_span(),
kind: OperatorKind::Equals,
},
rhs: ValueOrExpr::Value(JsonValue::Str(dummy_span(), "someType".to_string())),
}));
let _result_reg = c
.compile_count_nested(None, "a[*].b[*]", Some(&where_clause), "", &span)
.unwrap();
// With a where clause the innermost level should emit a loop (not
// a bare Count instruction).
let loop_starts: Vec<_> = c
.program
.instructions
.iter()
.filter(|i| matches!(i, Instruction::LoopStart { .. }))
.collect();
assert!(
loop_starts.len() >= 2,
"nested count with where should emit at least 2 LoopStart instructions, got {}",
loop_starts.len()
);
}
// -----------------------------------------------------------------------
// compile_count_nested_any — existence-pattern optimization for nested paths
// -----------------------------------------------------------------------
#[test]
fn nested_any_exists_true_emits_any_loops() {
let mut c = Compiler::new();
let span = dummy_span();
let where_clause = Constraint::Condition(alloc::boxed::Box::new(Condition {
span: dummy_span(),
lhs: crate::languages::azure_policy::ast::Lhs::Field(FieldNode {
span: dummy_span(),
kind: FieldKind::Type,
}),
operator: OperatorNode {
span: dummy_span(),
kind: OperatorKind::Equals,
},
rhs: ValueOrExpr::Value(JsonValue::Str(dummy_span(), "someType".to_string())),
}));
let result = c
.compile_count_nested_any(
None,
"a[*].b[*]",
&where_clause,
"",
true, // exists = true → count > 0
&span,
)
.unwrap();
assert!(result.is_some(), "nested any should return Some");
// All loops should be LoopMode::Any for the existence optimization.
for instr in &c.program.instructions {
if let Instruction::LoopStart { params_index } = instr {
let params = c
.program
.instruction_data
.get_loop_params(*params_index)
.unwrap();
assert_eq!(
params.mode,
LoopMode::Any,
"existence pattern should use Any loops"
);
}
}
}
#[test]
fn nested_any_exists_false_emits_not() {
let mut c = Compiler::new();
let span = dummy_span();
let where_clause = Constraint::Condition(alloc::boxed::Box::new(Condition {
span: dummy_span(),
lhs: crate::languages::azure_policy::ast::Lhs::Field(FieldNode {
span: dummy_span(),
kind: FieldKind::Type,
}),
operator: OperatorNode {
span: dummy_span(),
kind: OperatorKind::Equals,
},
rhs: ValueOrExpr::Value(JsonValue::Str(dummy_span(), "someType".to_string())),
}));
let result = c
.compile_count_nested_any(
None,
"a[*].b[*]",
&where_clause,
"",
false, // exists = false → count == 0
&span,
)
.unwrap();
assert!(result.is_some());
// Should have a PolicyCondition with Not op for the negation.
assert!(
c.program.instructions.iter().any(|i| matches!(
i,
Instruction::PolicyCondition { op, .. } if *op == PolicyOp::Not
)),
"count == 0 pattern should negate with PolicyCondition::Not"
);
}
// -----------------------------------------------------------------------
// try_compile_count_as_any — existence detection via operator + RHS
// -----------------------------------------------------------------------
/// Helper: build a Condition with count LHS, given operator and numeric RHS.
fn make_count_condition(count_node: CountNode, op: OperatorKind, rhs_number: i64) -> Condition {
Condition {
span: dummy_span(),
lhs: crate::languages::azure_policy::ast::Lhs::Count(count_node),
operator: OperatorNode {
span: dummy_span(),
kind: op,
},
rhs: ValueOrExpr::Value(JsonValue::Number(dummy_span(), rhs_number.to_string())),
}
}
fn make_value_count_with_where() -> CountNode {
CountNode::Value {
span: dummy_span(),
value: ValueOrExpr::Value(JsonValue::Array(
dummy_span(),
vec![
JsonValue::Number(dummy_span(), "1".to_string()),
JsonValue::Number(dummy_span(), "2".to_string()),
JsonValue::Number(dummy_span(), "3".to_string()),
],
)),
name: None,
where_: Some(alloc::boxed::Box::new(Constraint::Condition(
alloc::boxed::Box::new(Condition {
span: dummy_span(),
lhs: crate::languages::azure_policy::ast::Lhs::Value {
key_span: dummy_span(),
value: ValueOrExpr::Value(JsonValue::Number(dummy_span(), "1".to_string())),
},
operator: OperatorNode {
span: dummy_span(),
kind: OperatorKind::Equals,
},
rhs: ValueOrExpr::Value(JsonValue::Number(dummy_span(), "1".to_string())),
}),
))),
}
}
#[test]
fn any_optimization_greater_zero() {
let mut c = Compiler::new();
let count_node = make_value_count_with_where();
let condition = make_count_condition(count_node.clone(), OperatorKind::Greater, 0);
let result = c.try_compile_count_as_any(&count_node, &condition).unwrap();
assert!(
result.is_some(),
"count > 0 should trigger Any optimization"
);
// The loop should use LoopMode::Any.
for instr in &c.program.instructions {
if let Instruction::LoopStart { params_index } = instr {
let params = c
.program
.instruction_data
.get_loop_params(*params_index)
.unwrap();
assert_eq!(params.mode, LoopMode::Any);
}
}
}
#[test]
fn any_optimization_equals_zero_negates() {
let mut c = Compiler::new();
let count_node = make_value_count_with_where();
let condition = make_count_condition(count_node.clone(), OperatorKind::Equals, 0);
let result = c.try_compile_count_as_any(&count_node, &condition).unwrap();
assert!(
result.is_some(),
"count == 0 should trigger Any optimization"
);
// Should negate: PolicyCondition with Not.
assert!(
c.program.instructions.iter().any(|i| matches!(
i,
Instruction::PolicyCondition { op, .. } if *op == PolicyOp::Not
)),
"count == 0 should negate"
);
}
#[test]
fn any_optimization_not_triggered_for_equals_two() {
let mut c = Compiler::new();
let count_node = make_value_count_with_where();
let condition = make_count_condition(count_node.clone(), OperatorKind::Equals, 2);
let result = c.try_compile_count_as_any(&count_node, &condition).unwrap();
assert!(
result.is_none(),
"count == 2 is not an existence pattern, should return None"
);
}
#[test]
fn any_optimization_no_where_uses_any_loop() {
let mut c = Compiler::new();
let count_node = CountNode::Value {
span: dummy_span(),
value: ValueOrExpr::Value(JsonValue::Array(dummy_span(), vec![])),
name: None,
where_: None,
};
let condition = make_count_condition(count_node.clone(), OperatorKind::Greater, 0);
let result = c.try_compile_count_as_any(&count_node, &condition).unwrap();
assert!(
result.is_some(),
"without where clause, Any optimization should still apply for existence patterns"
);
// Verify it emitted an Any loop.
let has_any_loop = c.program.instructions.iter().any(|instr| {
if let Instruction::LoopStart { params_index } = instr {
let params = c
.program
.instruction_data
.get_loop_params(*params_index)
.unwrap();
params.mode == LoopMode::Any
} else {
false
}
});
assert!(has_any_loop, "should emit a LoopMode::Any loop");
}
// -----------------------------------------------------------------------
// compile_count — value-based count loop
// -----------------------------------------------------------------------
#[test]
fn compile_value_count_without_where() {
let mut c = Compiler::new();
let count_node = CountNode::Value {
span: dummy_span(),
value: ValueOrExpr::Value(JsonValue::Array(
dummy_span(),
vec![
JsonValue::Number(dummy_span(), "1".to_string()),
JsonValue::Number(dummy_span(), "2".to_string()),
],
)),
name: None,
where_: None,
};
let result_reg = c.compile_count(&count_node).unwrap();
assert!(result_reg < c.register_counter);
// Should emit a ForEach loop with Add to increment count.
let has_loop = c
.program
.instructions
.iter()
.any(|i| matches!(i, Instruction::LoopStart { .. }));
let has_add = c
.program
.instructions
.iter()
.any(|i| matches!(i, Instruction::Add { .. }));
assert!(has_loop, "value count should emit a loop");
assert!(has_add, "value count should emit Add to increment");
}
#[test]
fn compile_value_count_with_where() {
let mut c = Compiler::new();
let count_node = make_value_count_with_where();
let result_reg = c.compile_count(&count_node).unwrap();
assert!(result_reg < c.register_counter);
// Should have Guard instruction for the where clause.
assert!(
c.program.instructions.iter().any(|i| matches!(
i,
Instruction::Guard {
mode: GuardMode::Condition,
..
}
)),
"count with where should emit Guard for where condition"
);
}
// -----------------------------------------------------------------------
// classify_existence_pattern — direct coverage of all recognized patterns
// -----------------------------------------------------------------------
/// Helper to build a Condition with the given operator and numeric RHS
/// (LHS is irrelevant for classify_existence_pattern).
fn make_condition_for_classify(op: OperatorKind, rhs: i64) -> Condition {
Condition {
span: dummy_span(),
lhs: crate::languages::azure_policy::ast::Lhs::Field(FieldNode {
span: dummy_span(),
kind: FieldKind::Type,
}),
operator: OperatorNode {
span: dummy_span(),
kind: op,
},
rhs: ValueOrExpr::Value(JsonValue::Number(dummy_span(), rhs.to_string())),
}
}
#[test]
fn classify_existence_all_patterns() {
// "at least one" patterns → Some(true)
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::Greater,
0
)),
Some(true),
"> 0"
);
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::GreaterOrEquals,
1
)),
Some(true),
">= 1"
);
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::NotEquals,
0
)),
Some(true),
"!= 0"
);
// "none" patterns → Some(false)
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::Equals,
0
)),
Some(false),
"== 0"
);
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::Less,
1
)),
Some(false),
"< 1"
);
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::LessOrEquals,
0
)),
Some(false),
"<= 0"
);
// Non-existence patterns → None
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::Equals,
2
)),
None,
"== 2"
);
assert_eq!(
Compiler::classify_existence_pattern(&make_condition_for_classify(
OperatorKind::Greater,
1
)),
None,
"> 1"
);
}
// -----------------------------------------------------------------------
// try_compile_count_as_any — nested no-where field count is skipped
// -----------------------------------------------------------------------
#[test]
fn any_optimization_skips_nested_no_where_field_count() {
// A nested wildcard field path without a where clause should NOT be
// optimised into Any loops because of Undefined-propagation semantics.
let mut c = Compiler::new();
let count_node = CountNode::Field {
span: dummy_span(),
field: FieldNode {
span: dummy_span(),
kind: FieldKind::Alias("a[*].b[*]".to_string()),
},
where_: None,
};
let condition = make_count_condition(count_node.clone(), OperatorKind::Equals, 0);
let result = c.try_compile_count_as_any(&count_node, &condition).unwrap();
assert!(
result.is_none(),
"nested no-where field count should fall back to generic path"
);
}
}