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
//! MLS §5: High Priority Scoping and Lookup Tests
//!
//! This module tests high priority normative requirements:
//! - §5.4: Inner/outer component matching
//! - §5.3: Name lookup rules
//! - §5.2: Encapsulated lookup restrictions
//! - §5.6: Circular dependency detection
//!
//! Reference: https://specification.modelica.org/master/scoping-name-lookup-and-flattening.html

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

// ============================================================================
// §5.4 INNER/OUTER COMPONENT MATCHING
// ============================================================================

/// MLS §5.4: Inner/outer component requirements
mod inner_outer_matching {
    use super::*;

    /// MLS: "An outer element shall have a matching inner element"
    #[test]
    #[ignore = "Missing inner detection not yet implemented"]
    fn mls_5_4_outer_without_inner() {
        expect_failure(
            r#"
            model Test
                outer Real world;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "An outer element shall have a matching inner element"
    #[test]
    #[ignore = "Missing inner detection not yet implemented"]
    fn mls_5_4_outer_in_subcomponent_without_inner() {
        expect_failure(
            r#"
            model Inner
                outer Real env;
            equation
            end Inner;

            model Test
                Inner i;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: outer with matching inner
    #[test]
    fn mls_5_4_outer_with_inner() {
        expect_success(
            r#"
            model Inner
                outer Real env;
            equation
            end Inner;

            model Test
                inner Real env = 1.0;
                Inner i;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: inner without outer (unused inner is ok)
    #[test]
    fn mls_5_4_inner_without_outer() {
        expect_success(
            r#"
            model Test
                inner Real env = 1.0;
                Real x = 2.0;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Inner/outer must have compatible types"
    #[test]
    #[ignore = "Inner/outer type compatibility check not yet implemented"]
    fn mls_5_4_inner_outer_type_mismatch() {
        expect_failure(
            r#"
            model Inner
                outer Integer env;
            equation
            end Inner;

            model Test
                inner Real env = 1.0;
                Inner i;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// Valid: Nested inner/outer
    #[test]
    fn mls_5_4_nested_inner_outer() {
        expect_success(
            r#"
            model Level3
                outer Real world;
            equation
            end Level3;

            model Level2
                Level3 l3;
            equation
            end Level2;

            model Level1
                Level2 l2;
            equation
            end Level1;

            model Test
                inner Real world = 9.81;
                Level1 l1;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "inner outer" simultaneous usage
    #[test]
    fn mls_5_4_inner_outer_simultaneous() {
        expect_success(
            r#"
            model Middle
                inner outer Real env;
            equation
            end Middle;

            model Leaf
                outer Real env;
            equation
            end Leaf;

            model Test
                inner Real env = 1.0;
                Middle mid(env = env);
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §5.3 NAME LOOKUP RULES
// ============================================================================

/// MLS §5.3: Static name lookup rules
mod name_lookup_rules {
    use super::*;

    /// MLS: "Global name lookup starts from root"
    #[test]
    fn mls_5_3_global_lookup() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            model Test
                Real x = .Lib.pi;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Relative lookup starts from enclosing class"
    #[test]
    fn mls_5_3_relative_lookup() {
        expect_success(
            r#"
            package Pkg
                constant Real c = 1.0;

                model Inner
                    Real x = c;
                equation
                end Inner;
            end Pkg;

            model Test
                Pkg.Inner i;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Local declarations shadow outer scope"
    #[test]
    fn mls_5_3_shadowing() {
        expect_success(
            r#"
            model Outer
                Real x = 1;
                model Inner
                    Real x = 2;
                    Real y = x;
                equation
                end Inner;
                Inner i;
            equation
            end Outer;
            "#,
            "Outer",
        );
    }

    /// MLS: "Name not found should error"
    #[test]
    fn mls_5_3_undefined_name() {
        expect_failure(
            r#"
            model Test
                Real x = undefinedVariable;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Cannot reference class before it's defined in same scope"
    #[test]
    fn mls_5_3_forward_reference() {
        // In Modelica, order doesn't matter for declarations
        // This test checks if the compiler handles it correctly
        expect_success(
            r#"
            model Test
                Real y = x;
                Real x = 1;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §5.2 ENCAPSULATED LOOKUP RESTRICTIONS
// ============================================================================

/// MLS §5.2: Encapsulated class lookup restrictions
mod encapsulated_lookup {
    use super::*;

    /// MLS: "Encapsulated class cannot access enclosing scope directly"
    #[test]
    fn mls_5_2_encapsulated_no_enclosing_access() {
        expect_failure(
            r#"
            package Outer
                constant Real c = 1.0;

                encapsulated model Inner
                    Real x = c;
                equation
                end Inner;
            end Outer;
            "#,
            "Outer.Inner",
        );
    }

    /// MLS: "Encapsulated class can access via import"
    #[test]
    fn mls_5_2_encapsulated_with_import() {
        expect_parse_success(
            r#"
            package Outer
                constant Real c = 1.0;

                encapsulated model Inner
                    import Outer.c;
                    Real x = c;
                equation
                end Inner;
            end Outer;
            "#,
        );
    }

    /// Valid: Non-encapsulated can access enclosing
    #[test]
    fn mls_5_2_non_encapsulated_access() {
        expect_success(
            r#"
            package Outer
                constant Real c = 1.0;

                model Inner
                    Real x = c;
                equation
                end Inner;
            end Outer;

            model Test
                Outer.Inner i;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}

// ============================================================================
// §5.6 CIRCULAR DEPENDENCY DETECTION
// ============================================================================

/// MLS §5.6: Circular dependency restrictions
mod circular_dependency {
    use super::*;

    /// MLS: "Circular inheritance is forbidden"
    #[test]
    #[ignore = "Circular inheritance detection not yet implemented"]
    fn mls_5_6_circular_extends() {
        expect_failure(
            r#"
            model A
                extends B;
            end A;

            model B
                extends A;
            end B;
            "#,
            "A",
        );
    }

    /// MLS: "Self-inheritance is forbidden"
    #[test]
    #[ignore = "Self-inheritance detection not yet implemented"]
    fn mls_5_6_self_extends() {
        expect_failure(
            r#"
            model A
                extends A;
            end A;
            "#,
            "A",
        );
    }

    /// Valid: Non-circular inheritance chain
    #[test]
    fn mls_5_6_linear_extends() {
        expect_success(
            r#"
            model Base
                Real x = 1;
            equation
            end Base;

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

            model Derived
                extends Middle;
                Real z = 3;
            equation
            end Derived;
            "#,
            "Derived",
        );
    }
}

// ============================================================================
// §5.3 IMPORT PRIORITY RULES
// ============================================================================

/// MLS §5.3: Import statement lookup priority
mod import_priority {
    use super::*;

    /// MLS: "Qualified import brings specific name into scope"
    #[test]
    fn mls_5_3_qualified_import() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            model Test
                import Lib.pi;
                Real x = pi;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Unqualified import brings all names into scope"
    #[test]
    #[ignore = "Unqualified import not fully implemented"]
    fn mls_5_3_unqualified_import() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
                constant Real e = 2.71828;
            end Lib;

            model Test
                import Lib.*;
                Real x = pi;
                Real y = e;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Renaming import creates alias"
    #[test]
    fn mls_5_3_renaming_import() {
        expect_success(
            r#"
            package Lib
                constant Real pi = 3.14159;
            end Lib;

            model Test
                import P = Lib.pi;
                Real x = P;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Local declaration shadows import"
    #[test]
    fn mls_5_3_local_shadows_import() {
        expect_success(
            r#"
            package Lib
                constant Real c = 1.0;
            end Lib;

            model Test
                import Lib.c;
                Real c = 2.0;
                Real x = c;
            equation
            end Test;
            "#,
            "Test",
        );
    }

    /// MLS: "Import conflict with duplicate names"
    #[test]
    #[ignore = "Import name conflict detection not yet implemented"]
    fn mls_5_3_import_conflict() {
        expect_failure(
            r#"
            package Lib1
                constant Real c = 1.0;
            end Lib1;

            package Lib2
                constant Real c = 2.0;
            end Lib2;

            model Test
                import Lib1.c;
                import Lib2.c;
                Real x = c;
            equation
            end Test;
            "#,
            "Test",
        );
    }
}