veryl-analyzer 0.22.0

A modern hardware description language
Documentation
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
// Incomplete-effect boundary coverage for comb-loop analysis.
use super::*;

#[test]
fn instance_source_guard_limit_preserves_independent_cycles() {
    for selector in [false, true] {
        for stages in [4, 64] {
            let ports = if selector {
                "i: i, o: o[gate(flags, i)]"
            } else {
                "i: gate(flags, i), o: o"
            };
            let width = if selector { 2 } else { 1 };
            let code = format!(
                "module Child (i: input logic, o: output logic) {{ assign o = i; }}
                 module Top (flags: input logic<{stages}>, i: input logic,
                             o: output logic<{width}>, independent: output logic) {{
                    function gate (s: input logic<{stages}>, x: input logic) -> logic {{
                        var v: logic;
                        v = x;
                        for index in 0..{stages} {{
                            if s[index] {{ v = !v; }} else {{ v = 0; }}
                        }}
                        return v;
                    }}
                    inst child: Child ({ports});
                    assign independent = independent;
                 }}"
            );
            // Each assignment has only one guard, so construction fits in
            // either case. Walking back from the result accumulates a growing
            // prefix and must share the guard budget with its caller.
            crate::comb_loop_detect::with_procedure_guard_limit(1024, || {
                let case = format!("selector={selector}, stages={stages}");
                assert_eq!(comb_loop_analysis_is_complete(&code), stages == 4, "{case}");
                let errors = analyze(&code);
                assert!(
                    errors.iter().all(|error| match error {
                        AnalyzerError::CombinationalLoop { .. } => true,
                        AnalyzerError::UnassignVariable { identifier, .. } =>
                            identifier == "independent",
                        _ => false,
                    }),
                    "{case}: {errors:?}"
                );
                let loops = errors
                    .iter()
                    .filter_map(|error| match error {
                        AnalyzerError::CombinationalLoop { identifier, .. } => {
                            Some(identifier.as_str())
                        }
                        _ => None,
                    })
                    .collect::<Vec<_>>();
                assert_eq!(loops, ["independent"], "{case}: {errors:?}");
            });
        }
    }
}

#[test]
fn nested_runtime_loop_copy_limit_preserves_independent_cycles() {
    for imported in [false, true] {
        for kind in ["block", "overwritten", "function"] {
            for depth in [1, 16] {
                let destination = if kind == "function" {
                    "_discarded"
                } else {
                    "o"
                };
                let statements = if imported {
                    format!("{destination} = gate(1'b1, i);")
                } else {
                    format!(
                        "{destination} = i; {}",
                        format!("{destination} = !{destination};").repeat(32)
                    )
                };
                let loops = (0..depth).rev().fold(statements, |body, index| {
                    format!("for _iteration{index} in 0..n {{ {body} }}")
                });
                let body = if kind == "function" {
                    format!(
                        "function run () -> logic {{
                            var _discarded: logic;
                            _discarded = 0; {loops} return 0;
                         }}
                         assign o = run();"
                    )
                } else {
                    let overwrite = if kind == "overwritten" { "o = 0;" } else { "" };
                    format!("always_comb {{ o = 0; {loops} {overwrite} }}")
                };
                let function = if imported {
                    "function gate (s: input logic, x: input logic) -> logic {
                        var v: logic;
                        v = x;
                        for _index in 0..8 { if s { v = !v; } else { v = 0; } }
                        return v;
                     }"
                } else {
                    ""
                };
                let code = format!(
                    "module Top (i: input logic, n: input u32,
                                 o: output logic, independent: output logic) {{
                        {function} {body}
                        assign independent = independent;
                     }}"
                );
                // The inner loop fits. Enclosing loops must also charge for
                // copying its generated SSA, even after imports are condensed
                // or when the final result is overwritten or discarded.
                crate::comb_loop_detect::with_procedure_import_limit(1024, || {
                    let case = format!("imported={imported}, {kind}, depth={depth}");
                    assert_eq!(comb_loop_analysis_is_complete(&code), depth == 1, "{case}");
                    let errors = analyze(&code);
                    assert!(
                        errors.iter().all(|error| match error {
                            AnalyzerError::CombinationalLoop { .. } => true,
                            AnalyzerError::UnassignVariable { identifier, .. } =>
                                identifier == "independent",
                            _ => false,
                        }),
                        "{case}: {errors:?}"
                    );
                    let loops = errors
                        .iter()
                        .filter_map(|error| match error {
                            AnalyzerError::CombinationalLoop { identifier, .. } => {
                                Some(identifier.as_str())
                            }
                            _ => None,
                        })
                        .collect::<Vec<_>>();
                    assert_eq!(loops, ["independent"], "{case}: {errors:?}");
                });
            }
        }
    }
}

#[test]
fn runtime_loop_import_limit_preserves_independent_cycles() {
    for kind in ["block", "overwritten", "separate", "function"] {
        for calls in [1, 16] {
            let destination = if kind == "function" {
                "_discarded"
            } else {
                "o"
            };
            let assignments = (0..calls)
                .map(|index| format!("{destination}[{index}] = gate(1'b1, i);"))
                .collect::<Vec<_>>();
            let loops = if kind == "separate" {
                assignments
                    .iter()
                    .map(|assign| format!("for _iteration in 0..n {{ {assign} }}"))
                    .collect::<String>()
            } else {
                format!("for _iteration in 0..n {{ {} }}", assignments.join("\n"))
            };
            let body = if kind == "function" {
                format!(
                    "function run () -> logic {{
                        var _discarded: logic<{calls}>;
                        _discarded = 0; {loops} return 0;
                     }}
                     assign o = run();"
                )
            } else {
                let overwrite = if kind == "overwritten" { "o = 0;" } else { "" };
                format!("always_comb {{ o = 0; {loops} {overwrite} }}")
            };
            let code = format!(
                r#"
                module Top (i: input logic, n: input u32,
                            o: output logic<{calls}>, independent: output logic) {{
                    function gate (s: input logic, x: input logic) -> logic {{
                        var v: logic;
                        v = x;
                        for _index in 0..8 {{ if s {{ v = !v; }} else {{ v = 0; }} }}
                        return v;
                    }}
                    {body}
                    assign independent = independent;
                }}
                "#
            );
            crate::comb_loop_detect::with_procedure_import_limit(1024, || {
                assert_eq!(
                    comb_loop_analysis_is_complete(&code),
                    calls == 1,
                    "{kind}, calls={calls}"
                );
                let errors = analyze(&code);
                let loops = errors
                    .iter()
                    .filter_map(|error| match error {
                        AnalyzerError::CombinationalLoop { identifier, .. } => {
                            Some(identifier.as_str())
                        }
                        _ => None,
                    })
                    .collect::<Vec<_>>();
                assert_eq!(loops, ["independent"], "{kind}, calls={calls}: {errors:?}");
            });
        }
    }
}

#[test]
fn procedural_import_limit_preserves_independent_cycles() {
    for kind in ["procedure", "instance_side_effect"] {
        for calls in [1, 16] {
            let body = if kind == "procedure" {
                let assignments = (0..calls)
                    .map(|index| format!("o[{index}] = gate(1'b1, i);"))
                    .collect::<String>();
                format!("always_comb {{ {assignments} }}")
            } else {
                let functions = (0..calls)
                    .map(|index| format!(
                        "function sample{index} () -> logic {{ o[{index}] = gate(1'b1, i); return 0; }}"
                    ))
                    .collect::<String>();
                let actual = (0..calls)
                    .map(|index| format!("sample{index}()"))
                    .collect::<Vec<_>>()
                    .join(",");
                format!("{functions} inst sink: Sink (i: {{{actual}}});")
            };
            let code = format!(
                r#"
                module Sink (i: input logic<{calls}>) {{}}
                module Top (i: input logic, o: output logic<{calls}>, independent: output logic) {{
                    function gate (s: input logic, x: input logic) -> logic {{
                        var v: logic;
                        v = x;
                        for _index in 0..8 {{ if s {{ v = !v; }} else {{ v = 0; }} }}
                        return v;
                    }}
                    {body}
                    assign independent = independent;
                }}
                "#
            );
            crate::comb_loop_detect::with_procedure_import_limit(1024, || {
                assert_eq!(
                    comb_loop_analysis_is_complete(&code),
                    calls == 1,
                    "{kind}, calls={calls}"
                );
                let errors = analyze(&code);
                assert!(
                    errors.iter().all(|error| match error {
                        AnalyzerError::CombinationalLoop { .. }
                        | AnalyzerError::UnusedVariable { .. } => true,
                        AnalyzerError::UnassignVariable { identifier, .. } =>
                            identifier == "independent"
                                || (kind == "instance_side_effect" && identifier == "o"),
                        _ => false,
                    }),
                    "{kind}, calls={calls}: {errors:?}"
                );
                let loops = errors
                    .iter()
                    .filter_map(|error| match error {
                        AnalyzerError::CombinationalLoop { identifier, .. } => {
                            Some(identifier.as_str())
                        }
                        _ => None,
                    })
                    .collect::<Vec<_>>();
                assert_eq!(loops, ["independent"], "{kind}, calls={calls}: {errors:?}");
            });
        }
    }
}

#[test]
fn procedural_import_limit_does_not_limit_direct_assignments() {
    let assignments = (0..1024)
        .map(|index| format!("o[{index}] = i;"))
        .collect::<String>();
    for runtime_loop in [false, true] {
        let body = if runtime_loop {
            format!("o = 0; for _iteration in 0..n {{ {assignments} }}")
        } else {
            assignments.clone()
        };
        let code = format!(
            "module Top (i: input logic, n: input u32, o: output logic<1024>) {{ always_comb {{ {body} }} }}"
        );
        crate::comb_loop_detect::with_procedure_import_limit(0, || {
            assert!(comb_loop_analysis_is_complete(&code));
            assert!(analyze(&code).is_empty());
        });
    }
}

#[test]
fn instance_actual_expansion_limit_keeps_independent_cycles() {
    for stages in [4, 64] {
        let stages_code = "y = !y;".repeat(stages);
        let code = format!(
            r#"
            module Child (i: input logic, o: output logic) {{ assign o = i; }}
            module Top (i: input logic, o: output logic, independent: output logic) {{
                function chain (x: input logic) -> logic {{
                    var y: logic;
                    y = x;
                    {stages_code}
                    return y;
                }}
                inst child: Child (i: chain(i), o: o);
                assign independent = independent;
            }}
        "#
        );
        crate::comb_loop_detect::with_module_summary_limit(128, || {
            assert_eq!(comb_loop_analysis_is_complete(&code), stages == 4);
            let errors = analyze(&code);
            let loops = errors
                .iter()
                .filter_map(|error| match error {
                    AnalyzerError::CombinationalLoop { identifier, .. } => {
                        Some(identifier.as_str())
                    }
                    _ => None,
                })
                .collect::<Vec<_>>();
            assert_eq!(loops, ["independent"], "stages={stages}: {errors:?}");
        });
    }
}

#[test]
fn procedural_guard_limit_counts_fragmented_case_ranges() {
    for fragmented in [false, true] {
        for iterations in [0, 64] {
            let arms = (0..64)
                .map(|index| {
                    let returns = if fragmented {
                        index % 2 == 0
                    } else {
                        index < 32
                    };
                    let body = if returns { "return 0;" } else { "" };
                    format!("{index}: {{ {body} }}")
                })
                .collect::<String>();
            let code = format!(
                "module Top (sel: input u32, s: input logic, x: input logic,
                             o: output logic, independent: output logic) {{
                    function gate (sel: input u32, s: input logic, x: input logic) -> logic {{
                        case sel {{ {arms} default: {{}} }}
                        var v: logic;
                        v = x;
                        for _i in 0..{iterations} {{
                            if s {{ v = !v; }} else {{ v = 0; }}
                        }}
                        return v;
                    }}
                    assign o = gate(sel, s, x);
                    assign independent = independent;
                 }}"
            );
            // Both continuations constrain only one case branch. Alternating
            // returns leave many disjoint ranges that each later if must copy;
            // contiguous returns leave just one range. The case join alone fits.
            crate::comb_loop_detect::with_procedure_guard_limit(4096, || {
                let case = format!("fragmented={fragmented}, iterations={iterations}");
                assert_eq!(
                    comb_loop_analysis_is_complete(&code),
                    !fragmented || iterations == 0,
                    "{case}"
                );
                let errors = analyze(&code);
                assert!(
                    errors.iter().all(|error| match error {
                        AnalyzerError::CombinationalLoop { .. } => true,
                        AnalyzerError::UnassignVariable { identifier, .. } =>
                            identifier == "independent",
                        _ => false,
                    }),
                    "{case}: {errors:?}"
                );
                let loops = errors
                    .iter()
                    .filter_map(|error| match error {
                        AnalyzerError::CombinationalLoop { identifier, .. } => {
                            Some(identifier.as_str())
                        }
                        _ => None,
                    })
                    .collect::<Vec<_>>();
                assert_eq!(loops, ["independent"], "{case}: {errors:?}");
            });
        }
    }
}

#[test]
fn procedural_guard_limit_bounds_early_exits_and_preserves_independent_cycles() {
    for kind in ["return", "break", "runtime_break"] {
        for size in [2, 64, 256] {
            let exits = (0..size)
                .map(|index| {
                    if kind == "return" {
                        format!("if flags[{index}] {{ return data; }}")
                    } else {
                        format!("if flags[{index}] {{ break; }}")
                    }
                })
                .collect::<String>();
            let body = if kind == "return" {
                format!(
                    r#"
                    function first (flags: input logic<{size}>, data: input logic) -> logic {{
                        {exits}
                        return 0;
                    }}
                    assign o = first(flags, data);
                "#
                )
            } else {
                let bound = if kind == "break" { "1" } else { "n" };
                format!(
                    r#"
                    always_comb {{
                        o = o;
                        for _index in 0..{bound} {{ {exits} o = data; }}
                        o = 0;
                    }}
                "#
                )
            };
            let code = format!(
                r#"
                module Top (flags: input logic<{size}>, data: input logic,
                            n: input logic<32>, o: output logic, independent: output logic) {{
                    {body}
                    assign independent = independent;
                }}
            "#
            );
            crate::comb_loop_detect::with_procedure_guard_limit(128, || {
                assert_eq!(
                    comb_loop_analysis_is_complete(&code),
                    size == 2,
                    "{kind}, {size}"
                );
                let errors = analyze(&code);
                let loops = errors
                    .iter()
                    .filter_map(|error| match error {
                        AnalyzerError::CombinationalLoop { identifier, .. } => {
                            Some(identifier.as_str())
                        }
                        _ => None,
                    })
                    .collect::<Vec<_>>();
                assert_eq!(loops, ["independent"], "{kind}, {size}: {errors:?}");
            });
        }
    }
}

#[test]
fn comb_loop_malformed_effect_is_a_causal_barrier() {
    // Why this case exists: a rejected statement may have unknown side
    // effects, so a cycle which crosses it is not proven. The malformed
    // procedure must not suppress a separate exact cycle in another procedure.
    let errors = analyze(
        r#"
        module Top (
            o: output logic,
        ) {
            var a: logic;
            var b: logic;
            var c: logic;
            var d: logic;
            always_comb {
                a = b;
                missing_function();
                b = a;
            }
            always_comb {
                c = d;
                d = c;
                o = d;
            }
        }
        "#,
    );
    let loops = errors
        .iter()
        .filter_map(|error| match error {
            AnalyzerError::CombinationalLoop { identifier, .. } => Some(identifier.as_str()),
            _ => None,
        })
        .collect::<Vec<_>>();
    assert!(
        loops.len() == 1 && matches!(loops[0], "c" | "d"),
        "only the cycle independent of the malformed barrier is proven: {errors:#?}"
    );
}

#[test]
fn comb_loop_inout_boundary_does_not_prove_hard_feedback() {
    let errors = analyze(
        r#"
        module Top (
            io: inout  tri logic,
            o : output     logic,
        ) {
            assign io = o;
            assign o = io;
        }
        "#,
    );
    assert!(
        errors
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "an externally driven inout boundary cannot prove a hard loop: {errors:#?}"
    );
}

#[test]
fn comb_loop_dynamic_for_bound_over_known_regions_is_complete_and_loop_free() {
    let code = r#"
        module Top (
            n   : input  logic<32>,
            data: input  logic,
            o   : output logic,
        ) {
            var value: logic;
            always_comb {
                value = 0;
                for _index in 0..n {
                    value = data;
                }
                o = value;
            }
        }
    "#;
    assert!(comb_loop_analysis_is_complete(code));
    assert!(
        analyze(code)
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. }))
    );
}

#[test]
fn comb_loop_dynamic_for_bound_over_known_regions_detects_feedback() {
    // False-negative guard: the loop may execute zero times, but an
    // unconstrained runtime bound may also execute the feedback body. Treating
    // the existence of the zero-trip path as proof that the body is unreachable
    // would miss a realizable combinational loop.
    let code = r#"
        module Top (
            n: input  logic<32>,
            o: output logic,
        ) {
            var a: logic;
            var b: logic;
            always_comb {
                for _index in 0..n {
                    a = b;
                    b = a;
                }
                o = b;
            }
        }
    "#;
    assert!(comb_loop_analysis_is_complete(code));
    assert!(
        analyze(code)
            .iter()
            .any(|error| matches!(error, AnalyzerError::CombinationalLoop { .. }))
    );
}

#[test]
fn comb_loop_const_zero_for_bound_skips_unreachable_feedback() {
    // True negative: unlike the runtime-bound case above, this range is proven
    // empty, so the feedback-shaped body is unreachable.
    let code = r#"
        module Top (
            o: output logic,
        ) {
            var a: logic;
            var b: logic;
            always_comb {
                for _index in 0..0 {
                    a = b;
                    b = a;
                }
                o = b;
            }
        }
    "#;
    assert!(comb_loop_analysis_is_complete(code));
    assert!(
        analyze(code)
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. }))
    );
}

#[test]
fn comb_loop_dynamic_for_bound_with_unknown_effect_is_incomplete() {
    let code = r#"
        module Top (
            n: input  logic<32>,
            o: output logic,
        ) {
            var a: logic;
            var b: logic;
            always_comb {
                for _index in 0..n {
                    a = b;
                    missing_function();
                    b = a;
                }
                o = b;
            }
        }
    "#;
    assert!(!comb_loop_analysis_is_complete(code));
    assert!(
        analyze(code)
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. }))
    );
}

#[test]
fn comb_loop_oversized_constant_range_is_incomplete_without_false_feedback() {
    let evaluate_size_limit = Metadata::create_default("prj")
        .unwrap()
        .build
        .evaluate_size_limit;
    let code = format!(
        r#"
        module Top (
            o: output logic,
        ) {{
            var value   : logic [3];
            var feedback: logic;
            assign feedback = value[2];
            always_comb {{
                value = '{{default: 0}};
                value[1] = feedback;
                for index in 0..{} {{
                    value[index + 1] = value[index];
                }}
                o = feedback;
            }}
        }}
        "#,
        evaluate_size_limit + 1
    );

    assert!(!comb_loop_analysis_is_complete(&code));
    let errors = analyze(&code);
    assert!(
        errors
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "the expansion limit must not create a combinational-loop diagnostic: {errors:#?}"
    );
}

#[test]
fn comb_loop_dynamic_for_incomplete_effect_keeps_an_independent_cycle() {
    let code = r#"
        module Top (
            n: input  logic<32>,
            o: output logic,
        ) {
            var a: logic;
            var b: logic;
            var c: logic;
            var d: logic;
            always_comb {
                for _index in 0..n {
                    a = b;
                    missing_function();
                    b = a;
                }
            }
            always_comb {
                c = d;
                d = c;
                o = d;
            }
        }
    "#;
    assert!(!comb_loop_analysis_is_complete(code));
    let loops = analyze(code)
        .into_iter()
        .filter(|error| matches!(error, AnalyzerError::CombinationalLoop { .. }))
        .count();
    assert_eq!(loops, 1);
}

#[test]
fn comb_loop_modport_members_do_not_gain_cross_member_feedthrough() {
    let errors = analyze(
        r#"
        interface Bus {
            var request : logic;
            var response: logic;
            modport port {
                request : input,
                response: output,
            }
        }
        module Child (
            bus: modport Bus::port,
        ) {
            assign bus.response = 0;
        }
        module Top (
            o: output logic,
        ) {
            inst bus: Bus;
            inst child: Child (
                bus: bus,
            );
            assign bus.request = bus.response;
            assign o = bus.request;
        }
        "#,
    );
    assert!(
        errors
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "disjoint modport members must not acquire an invented return edge: {errors:#?}"
    );
}

#[test]
fn comb_loop_recursive_module_is_incomplete_not_hard_feedback() {
    let errors = analyze_with_large_stack(
        r#"
        module Recursive {
            inst next: Recursive;
        }
        "#,
    );
    assert!(
        errors
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "recursive hierarchy is incomplete, not proof of a hard loop: {errors:#?}"
    );
}

#[test]
fn comb_loop_unresolved_hierarchy_is_incomplete_not_hard_feedback() {
    let errors = analyze(
        r#"
        module Top (
            o: output logic,
        ) {
            inst missing: Missing;
            assign o = 0;
        }
        "#,
    );
    assert!(
        errors
            .iter()
            .all(|error| !matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "unresolved hierarchy is incomplete, not proof of a hard loop: {errors:#?}"
    );
}

#[test]
fn comb_loop_opaque_component_does_not_hide_independent_cycle() {
    let errors = analyze(
        r#"
        module Top (
            o: output logic,
        ) {
            var opaque_in : logic;
            var opaque_out: logic;
            var a: logic;
            var b: logic;
            inst ext: $sv::Ext (
                i_data: opaque_in,
                o_data: opaque_out,
            );
            assign opaque_in = opaque_out;
            assign a = b;
            assign b = a;
            assign o = a;
        }
        "#,
    );
    assert!(
        errors
            .iter()
            .any(|error| matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "an opaque component must not suppress a separate proven loop: {errors:#?}"
    );
}

#[test]
fn comb_loop_unresolved_hierarchy_does_not_hide_independent_cycle() {
    let errors = analyze(
        r#"
        module Top (
            o: output logic,
        ) {
            var a: logic;
            var b: logic;
            inst missing: Missing;
            assign a = b;
            assign b = a;
            assign o = a;
        }
        "#,
    );
    assert!(
        errors
            .iter()
            .any(|error| matches!(error, AnalyzerError::CombinationalLoop { .. })),
        "unresolved hierarchy must not suppress a separate proven loop: {errors:#?}"
    );
}

#[test]
fn partition_sweep_keeps_fragmented_modules_complete_and_parent_cycles() {
    for count in [8, 64] {
        let assignments = (0..count)
            .map(|index| {
                format!("assign o[{index}] = mem[{index}][{index}] ^ mem[index][{index}];")
            })
            .collect::<Vec<_>>()
            .join("\n");
        let code = format!(
            "module Fragmented (index: input u32, mem: input logic<{count}>[{count}],
                                o: output logic<{count}>) {{
                {assignments}
             }}
             module Top (index: input u32, mem: input logic<{count}>[{count}],
                         o: output logic<{count}>, independent: output logic) {{
                inst child: Fragmented (index: index, mem: mem, o: o);
                assign independent = independent;
             }}"
        );
        assert!(comb_loop_analysis_is_complete(&code), "count={count}");
        let errors = analyze(&code);
        assert!(
            errors.iter().all(|error| match error {
                AnalyzerError::CombinationalLoop { identifier, .. }
                | AnalyzerError::UnassignVariable { identifier, .. } => identifier == "independent",
                _ => false,
            }),
            "{errors:?}"
        );
        assert!(
            errors.iter().any(|error| matches!(error,
                AnalyzerError::CombinationalLoop { identifier, .. } if identifier == "independent"
            )),
            "{errors:?}"
        );
    }
}