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
//! MLS §7: High Priority Inheritance Tests
//!
//! This module tests high priority normative requirements:
//! - §7.2.6: final modifier enforcement
//! - §7.3.2: constrainedby type checking
//! - §7.4: break modifier semantics
//! - §7.1: Extends restrictions
//!
//! Reference: https://specification.modelica.org/master/inheritance-modification-redeclaration.html

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

// ============================================================================
// §7.2.6 FINAL MODIFIER ENFORCEMENT
// ============================================================================

/// MLS §7.2.6: Final modifier prevents further modification
mod final_modifier {
    use super::*;

    /// MLS: "A final modification cannot be further modified"
    #[test]
    #[ignore = "Final modifier enforcement not yet implemented"]
    fn mls_7_2_6_final_cannot_be_modified() {
        expect_failure(
            r#"
            model Base
                final parameter Real k = 1;
            equation
            end Base;

            model Derived
                extends Base(k = 2);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// MLS: "A final modification cannot be further modified" (nested)
    #[test]
    #[ignore = "Final modifier enforcement not yet implemented"]
    fn mls_7_2_6_final_nested_modification() {
        expect_failure(
            r#"
            model Inner
                parameter Real x = 1;
            equation
            end Inner;

            model Base
                final Inner i(x = 2);
            equation
            end Base;

            model Derived
                extends Base(i(x = 3));
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// MLS: "A final element cannot be redeclared"
    #[test]
    #[ignore = "Final redeclare restriction not yet implemented"]
    fn mls_7_2_6_final_cannot_redeclare() {
        expect_failure(
            r#"
            model Base
                final replaceable model M = Integer;
            equation
            end Base;

            model Derived
                extends Base(redeclare model M = Real);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// Valid: Non-final can be modified
    #[test]
    fn mls_7_2_6_non_final_modification() {
        expect_success(
            r#"
            model Base
                parameter Real k = 1;
            equation
            end Base;

            model Derived
                extends Base(k = 2);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// Valid: Using final in base class
    #[test]
    fn mls_7_2_6_final_declaration() {
        expect_success(
            r#"
            model Base
                final parameter Real k = 1;
            equation
            end Base;

            model Derived
                extends Base;
            equation
            end Derived;
            "#,
            "Derived",
        );
    }
}

// ============================================================================
// §7.3.2 CONSTRAINEDBY TYPE CHECKING
// ============================================================================

/// MLS §7.3.2: constrainedby restricts redeclaration types
mod constrainedby_checking {
    use super::*;

    /// MLS: "Redeclaration must be subtype of constraining type"
    #[test]
    #[ignore = "constrainedby type checking not yet implemented"]
    fn mls_7_3_2_constrainedby_violation() {
        expect_failure(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

            model Extended
                extends Base;
                Real y = 2;
            equation
            end Extended;

            model Container
                replaceable Base b constrainedby Base;
            equation
            end Container;

            connector C
                Real v;
            end C;

            model Test
                extends Container(redeclare C b);
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Redeclaration compatible with constraining type
    #[test]
    fn mls_7_3_2_constrainedby_satisfied() {
        expect_success(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

            model Extended
                extends Base;
                Real y = 2;
            equation
            end Extended;

            model Container
                replaceable Base b constrainedby Base;
            equation
            end Container;

            model Test
                extends Container(redeclare Extended b);
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Default constraining type is declared type"
    #[test]
    fn mls_7_3_2_default_constraining_type() {
        expect_success(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

            model Extended
                extends Base;
                Real y = 2;
            equation
            end Extended;

            model Container
                replaceable Base b;
            equation
            end Container;

            model Test
                extends Container(redeclare Extended b);
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §7.4 BREAK MODIFIER
// ============================================================================

/// MLS §7.4: break modifier for selective model extension
mod break_modifier {
    use super::*;

    /// MLS: "break removes inherited element"
    #[test]
    fn mls_7_4_break_removes_element() {
        expect_success(
            r#"
            model Base
                Real x = 1;
                Real y = 2;
            equation
            end Base;

            model Derived
                extends Base(break x);
                Real x = 10;
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// MLS: "break can remove equations"
    #[test]
    #[ignore = "break equation syntax not yet supported"]
    fn mls_7_4_break_equation() {
        expect_parse_success(
            r#"
            model Base
                Real x;
            equation
                x = 1;
            end Base;

            model Derived
                extends Base(break x = 1);
            equation
                x = 2;
            end Derived;
            "#,
        );
    }

    /// MLS: "break cannot be used with non-inherited"
    #[test]
    #[ignore = "break on non-inherited element not yet checked"]
    fn mls_7_4_break_non_inherited() {
        expect_failure(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

            model Derived
                extends Base(break nonexistent);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// Valid: Multiple breaks
    #[test]
    fn mls_7_4_multiple_breaks() {
        expect_success(
            r#"
            model Base
                Real x = 1;
                Real y = 2;
                Real z = 3;
            equation
            end Base;

            model Derived
                extends Base(break x, break y);
                Real x = 10;
                Real y = 20;
            equation
            end Derived;
            "#,
            "Derived",
        );
    }
}

// ============================================================================
// §7.1 EXTENDS RESTRICTIONS
// ============================================================================

/// MLS §7.1: Restrictions on extends clause
mod extends_restrictions {
    use super::*;

    /// MLS: "Cannot extend a connector as a model"
    #[test]
    #[ignore = "Extends class category check not yet implemented"]
    fn mls_7_1_model_extends_connector() {
        expect_failure(
            r#"
            connector C
                Real v;
                flow Real i;
            end C;

            model Test
                extends C;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Cannot extend a function as a model"
    #[test]
    #[ignore = "Extends class category check not yet implemented"]
    fn mls_7_1_model_extends_function() {
        expect_failure(
            r#"
            function F
                input Real x;
                output Real y;
            algorithm
                y := x;
            end F;

            model Test
                extends F;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Model extends model
    #[test]
    fn mls_7_1_model_extends_model() {
        expect_success(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

            model Derived
                extends Base;
                Real y = 2;
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// Valid: Block extends block
    #[test]
    fn mls_7_1_block_extends_block() {
        expect_parse_success(
            r#"
            block Base
                input Real u;
                output Real y;
            equation
                y = u;
            end Base;

            block Derived
                extends Base;
            end Derived;
            "#,
        );
    }

    /// Valid: Connector extends connector
    #[test]
    fn mls_7_1_connector_extends_connector() {
        expect_parse_success(
            r#"
            connector Base
                Real v;
            end Base;

            connector Extended
                extends Base;
                flow Real i;
            end Extended;
            "#,
        );
    }

    /// Valid: Record extends record
    #[test]
    fn mls_7_1_record_extends_record() {
        expect_parse_success(
            r#"
            record Base
                Real x;
            end Base;

            record Extended
                extends Base;
                Real y;
            end Extended;
            "#,
        );
    }
}

// ============================================================================
// §7.2 MODIFICATION RESTRICTIONS
// ============================================================================

/// MLS §7.2: Modification restrictions
mod modification_restrictions {
    use super::*;

    /// MLS: "Modification must refer to existing element"
    #[test]
    #[ignore = "Modification target validation not yet implemented"]
    fn mls_7_2_modify_nonexistent() {
        expect_failure(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

            model Derived
                extends Base(nonexistent = 2);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// MLS: "each modifier requires array element"
    #[test]
    #[ignore = "each modifier validation not yet implemented"]
    fn mls_7_2_each_on_scalar() {
        expect_failure(
            r#"
            model Inner
                Real x = 1;
            equation
            end Inner;

            model Container
                Inner i;
            equation
            end Container;

            model Test
                extends Container(each i(x = 2));
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: each on array component
    #[test]
    fn mls_7_2_each_on_array() {
        expect_success(
            r#"
            model Container
                Real x[3](each start = 0);
            equation
                for i in 1:3 loop
                    x[i] = i;
                end for;
            end Container;
            "#,
            "Container",
        );
    }

    /// Valid: Nested modification
    #[test]
    fn mls_7_2_nested_modification() {
        expect_success(
            r#"
            model Inner
                parameter Real k = 1;
            equation
            end Inner;

            model Outer
                Inner i;
            equation
            end Outer;

            model Test
                extends Outer(i(k = 2));
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §7.3 REPLACEABLE AND REDECLARE
// ============================================================================

/// MLS §7.3: Replaceable and redeclare semantics
mod replaceable_redeclare {
    use super::*;

    /// MLS: "Cannot redeclare non-replaceable"
    #[test]
    #[ignore = "Non-replaceable redeclare check not yet implemented"]
    fn mls_7_3_redeclare_non_replaceable() {
        expect_failure(
            r#"
            model Base
                parameter Real k = 1;
            equation
            end Base;

            model Derived
                extends Base(redeclare parameter Integer k = 2);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// Valid: Redeclare replaceable
    #[test]
    fn mls_7_3_redeclare_replaceable() {
        expect_success(
            r#"
            model Base
                replaceable parameter Real k = 1;
            equation
            end Base;

            model Derived
                extends Base(redeclare parameter Real k = 2);
            equation
            end Derived;
            "#,
            "Derived",
        );
    }

    /// Valid: Replaceable model component
    #[test]
    fn mls_7_3_replaceable_model() {
        expect_success(
            r#"
            model DefaultModel
                Real x = 1;
            equation
            end DefaultModel;

            model AltModel
                Real x = 2;
            equation
            end AltModel;

            model Container
                replaceable DefaultModel m;
            equation
            end Container;

            model Test
                extends Container(redeclare AltModel m);
            equation
            end Test;
            "#,
            "Test",
        );
    }
}