rumoca 0.7.28

Modelica compiler written in RUST
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
//! MLS Chapter 6: High-Priority Edge Case Tests
//!
//! This module contains critical tests for edge cases and advanced scenarios
//! in type system behavior that are essential for MLS conformance.
//!
//! Reference: https://specification.modelica.org/master/interface-or-type.html

use crate::spec::{expect_failure, expect_parse_success, expect_success};

// ============================================================================
// CRITICAL: SUBTYPE RELATIONSHIP TESTS
// ============================================================================

/// Critical subtype relationship edge cases
mod subtype_critical {
    use super::*;

    /// Critical: Component subset rule
    #[test]
    fn critical_subtype_component_subset() {
        expect_success(
            r#"
            model Base
                Real a;
                Real b;
            equation
            end Base;

            model Extended
                extends Base;
                Real c;
            equation
                a = 1;
                b = 2;
                c = 3;
            end Extended;
            "#,
            "Extended",
        );
    }

    /// Critical: Deep inheritance chain
    #[test]
    fn critical_deep_inheritance() {
        expect_success(
            r#"
            model L1
                Real a;
            equation
            end L1;

            model L2
                extends L1;
                Real b;
            equation
            end L2;

            model L3
                extends L2;
                Real c;
            equation
            end L3;

            model L4
                extends L3;
                Real d;
            equation
            end L4;

            model L5
                extends L4;
            equation
                a = 1;
                b = 2;
                c = 3;
                d = 4;
            end L5;
            "#,
            "L5",
        );
    }

    /// Critical: Diamond inheritance pattern
    #[test]
    fn critical_diamond_inheritance() {
        expect_parse_success(
            r#"
            model Base
                Real x;
            equation
            end Base;

            model Left
                extends Base;
                Real left;
            equation
            end Left;

            model Right
                extends Base;
                Real right;
            equation
            end Right;

            model Diamond
                extends Left;
                extends Right;
            equation
                x = 1;
                left = 2;
                right = 3;
            end Diamond;
            "#,
        );
    }

    /// Critical: Component with subtype variability
    #[test]
    fn critical_variability_subtype() {
        expect_success(
            r#"
            model Test
                constant Real c = 1;
                parameter Real p = c;
                Real x = p;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// CRITICAL: PLUG COMPATIBILITY EDGE CASES
// ============================================================================

/// Critical plug compatibility edge cases
mod plug_critical {
    use super::*;

    /// Critical: Plug-compatible with additional non-flow
    #[test]
    fn critical_plug_additional_nonflow() {
        expect_parse_success(
            r#"
            connector Base
                Real e;
                flow Real f;
            end Base;

            connector Extended
                extends Base;
                Real extra;
            end Extended;

            model Test
                Extended a, b;
            equation
                connect(a, b);
            end Test;
            "#,
        );
    }

    /// Critical: Stream connector plug compatibility
    #[test]
    fn critical_stream_plug() {
        expect_parse_success(
            r#"
            connector FluidPort
                Real p;
                flow Real m_flow;
                stream Real h_outflow;
            end FluidPort;

            model FluidComponent
                FluidPort a, b;
            equation
                connect(a, b);
            end FluidComponent;
            "#,
        );
    }

    /// Critical: Hierarchical connector matching
    #[test]
    fn critical_hierarchical_connector() {
        expect_parse_success(
            r#"
            connector Pin
                Real v;
                flow Real i;
            end Pin;

            connector TwoPin
                Pin p;
                Pin n;
            end TwoPin;

            model Test
                TwoPin a, b;
            equation
                connect(a.p, b.p);
                connect(a.n, b.n);
            end Test;
            "#,
        );
    }

    /// Critical: Array connector connection
    #[test]
    fn critical_array_connector() {
        expect_parse_success(
            r#"
            connector Pin
                Real v;
                flow Real i;
            end Pin;

            model Test
                Pin p[3];
                Pin n[3];
            equation
                for i in 1:3 loop
                    connect(p[i], n[i]);
                end for;
            end Test;
            "#,
        );
    }
}

// ============================================================================
// CRITICAL: ARRAY TYPE EDGE CASES
// ============================================================================

/// Critical array type edge cases
mod array_critical {
    use super::*;

    /// Critical: Unknown array size (colon)
    #[test]
    fn critical_unknown_array_size() {
        expect_parse_success(
            r#"
            function SumVector
                input Real v[:];
                output Real s;
            algorithm
                s := sum(v);
            end SumVector;

            model Test
                Real s = SumVector({1, 2, 3, 4, 5});
            end Test;
            "#,
        );
    }

    /// Critical: Size-dependent array size
    #[test]
    fn critical_size_dependent() {
        expect_parse_success(
            r#"
            function ProcessPair
                input Real a[:];
                input Real b[size(a, 1)];
                output Real c[size(a, 1)];
            algorithm
                for i in 1:size(a, 1) loop
                    c[i] := a[i] + b[i];
                end for;
            end ProcessPair;
            "#,
        );
    }

    /// Critical: Multi-dimensional flexible arrays
    #[test]
    fn critical_multidim_flexible() {
        expect_parse_success(
            r#"
            function MatrixOp
                input Real A[:, :];
                output Real B[size(A, 1), size(A, 2)];
            algorithm
                for i in 1:size(A, 1) loop
                    for j in 1:size(A, 2) loop
                        B[i, j] := 2 * A[i, j];
                    end for;
                end for;
            end MatrixOp;
            "#,
        );
    }

    /// Critical: Empty array handling
    #[test]
    fn critical_empty_array() {
        expect_parse_success(
            r#"
            model Test
                parameter Integer n = 0;
                Real x[n];
            equation
            end Test;
            "#,
        );
    }

    /// Critical: Array slicing
    #[test]
    fn critical_array_slicing() {
        expect_parse_success(
            r#"
            model Test
                Real x[5] = {1, 2, 3, 4, 5};
                Real y[3] = x[2:4];
            end Test;
            "#,
        );
    }
}

// ============================================================================
// CRITICAL: FUNCTION TYPE EDGE CASES
// ============================================================================

/// Critical function type edge cases
mod function_critical {
    use super::*;

    /// Critical: Recursive function type
    #[test]
    fn critical_recursive_function() {
        expect_parse_success(
            r#"
            function Factorial
                input Integer n;
                output Integer result;
            algorithm
                if n <= 1 then
                    result := 1;
                else
                    result := n * Factorial(n - 1);
                end if;
            end Factorial;

            model Test
                Integer f = Factorial(5);
            end Test;
            "#,
        );
    }

    /// Critical: Function with protected variables
    #[test]
    fn critical_function_protected() {
        expect_success(
            r#"
            function Compute
                input Real x;
                output Real y;
            protected
                Real temp;
            algorithm
                temp := x * 2;
                y := temp + 1;
            end Compute;

            model Test
                Real y = Compute(5);
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Higher-order function (partial function)
    #[test]
    fn critical_higher_order() {
        expect_parse_success(
            r#"
            partial function UnaryOp
                input Real x;
                output Real y;
            end UnaryOp;

            function ApplyTwice
                input UnaryOp f;
                input Real x;
                output Real y;
            algorithm
                y := f(f(x));
            end ApplyTwice;

            function Double
                extends UnaryOp;
            algorithm
                y := 2 * x;
            end Double;

            model Test
                Real x = ApplyTwice(Double, 3);
            end Test;
            "#,
        );
    }

    /// Critical: External function type
    #[test]
    fn critical_external_function() {
        expect_parse_success(
            r#"
            function ExternalSin
                input Real x;
                output Real y;
            external "C" y = sin(x);
            end ExternalSin;
            "#,
        );
    }
}

// ============================================================================
// CRITICAL: TYPE COERCION EDGE CASES
// ============================================================================

/// Critical type coercion edge cases
mod coercion_critical {
    use super::*;

    /// Critical: Integer to Real in if-expression
    #[test]
    fn critical_coercion_if_expr() {
        expect_success(
            r#"
            model Test
                Boolean cond = true;
                Real x = if cond then 1 else 2.5;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Integer to Real in array construction
    #[test]
    fn critical_coercion_array() {
        expect_success(
            r#"
            model Test
                Real x[4] = {1, 2.0, 3, 4.5};
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Integer to Real in function call
    #[test]
    fn critical_coercion_function() {
        expect_success(
            r#"
            function F
                input Real x;
                output Real y;
            algorithm
                y := x * 2;
            end F;

            model Test
                Real y = F(5);
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Coercion with parameter expressions
    #[test]
    fn critical_coercion_parameter() {
        expect_success(
            r#"
            model Test
                parameter Integer n = 5;
                parameter Real x = n + 0.5;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: No Real to Integer coercion
    #[test]
    #[ignore = "Real to Integer type check not implemented"]
    fn critical_no_real_to_int() {
        expect_failure(
            r#"
            model Test
                Integer n = 5.5;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// CRITICAL: ENUMERATION TYPE EDGE CASES
// ============================================================================

/// Critical enumeration type edge cases
mod enum_critical {
    use super::*;

    /// Critical: Enumeration in if-expression
    #[test]
    fn critical_enum_if_expr() {
        expect_success(
            r#"
            type State = enumeration(Off, On);

            model Test
                Boolean active = true;
                State s = if active then State.On else State.Off;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Enumeration comparison
    #[test]
    fn critical_enum_comparison() {
        expect_success(
            r#"
            type Level = enumeration(Low, Medium, High);

            model Test
                Level current = Level.Medium;
                Boolean isHigh;
            equation
                isHigh = current == Level.High;
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Enumeration with descriptions
    #[test]
    fn critical_enum_descriptions() {
        expect_parse_success(
            r#"
            type Mode = enumeration(
                Off "System is off",
                Idle "System is idle",
                Running "System is running",
                Error "System has error"
            );

            model Test
                Mode m = Mode.Idle;
            end Test;
            "#,
        );
    }

    /// Critical: Enumeration in array
    #[test]
    fn critical_enum_array() {
        expect_parse_success(
            r#"
            type Color = enumeration(Red, Green, Blue);

            model Test
                Color palette[3] = {Color.Red, Color.Green, Color.Blue};
            end Test;
            "#,
        );
    }
}

// ============================================================================
// CRITICAL: COMPLEX TYPE SCENARIOS
// ============================================================================

/// Critical complex type scenarios
mod complex_critical {
    use super::*;

    /// Critical: Type alias chain
    #[test]
    fn critical_type_alias_chain() {
        expect_success(
            r#"
            type Level1 = Real;
            type Level2 = Level1(min = 0);
            type Level3 = Level2(max = 100);

            model Test
                Level3 x = 50;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Package-qualified types
    #[test]
    fn critical_package_types() {
        expect_parse_success(
            r#"
            package Units
                type Voltage = Real(unit = "V");
                type Current = Real(unit = "A");
                type Resistance = Real(unit = "Ohm");
            end Units;

            model Resistor
                Units.Voltage v;
                Units.Current i;
                parameter Units.Resistance R = 1;
            equation
                v = R * i;
                i = 1;
            end Resistor;
            "#,
        );
    }

    /// Critical: Nested package types
    #[test]
    fn critical_nested_package_types() {
        expect_parse_success(
            r#"
            package Outer
                package Inner
                    type Value = Real;
                end Inner;
            end Outer;

            model Test
                Outer.Inner.Value x = 1;
            end Test;
            "#,
        );
    }

    /// Critical: Import with type usage
    #[test]
    fn critical_import_types() {
        expect_parse_success(
            r#"
            package Math
                type Vector3 = Real[3];

                function Magnitude
                    input Vector3 v;
                    output Real m;
                algorithm
                    m := sqrt(v[1]^2 + v[2]^2 + v[3]^2);
                end Magnitude;
            end Math;

            model Test
                import Math.Vector3;
                import Math.Magnitude;

                Vector3 v = {3, 4, 0};
                Real m = Magnitude(v);
            end Test;
            "#,
        );
    }

    /// Critical: Generic-like pattern with replaceable
    #[test]
    fn critical_replaceable_type() {
        expect_parse_success(
            r#"
            model GenericFilter
                replaceable type DataType = Real;
                DataType input_val;
                DataType output_val;
            equation
                output_val = input_val;
            end GenericFilter;

            model IntFilter
                extends GenericFilter(redeclare type DataType = Integer);
            end IntFilter;
            "#,
        );
    }
}

// ============================================================================
// CRITICAL: TYPE ERROR DETECTION
// ============================================================================

/// Critical type error detection
mod error_critical {
    use super::*;

    /// Critical: Detect undefined type early
    #[test]
    fn error_undefined_type_early() {
        expect_failure(
            r#"
            model Test
                NonExistentType x;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Detect circular type definition
    #[test]
    #[ignore = "Circular type detection not implemented"]
    fn error_circular_type() {
        expect_failure(
            r#"
            type A = B;
            type B = A;

            model Test
                A x;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Detect incompatible array operations
    #[test]
    #[ignore = "Array dimension validation in equations not yet implemented"]
    fn error_incompatible_array_ops() {
        expect_failure(
            r#"
            model Test
                Real x[3] = {1, 2, 3};
                Real y[4] = {1, 2, 3, 4};
                Real z[3];
            equation
                z = x + y;
            end Test;
            "#,
            "Test",
        );
    }

    /// Critical: Detect wrong enumeration literal
    #[test]
    #[ignore = "Enumeration literal validation not yet implemented"]
    fn error_wrong_enum_literal() {
        expect_failure(
            r#"
            type Color = enumeration(Red, Green, Blue);

            model Test
                Color c = Color.Yellow;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}