miden-assembly 0.32.0

Miden VM assembly 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
// LINK DIAGNOSTICS
// ================================================================================================

use super::*;

#[test]
fn link_diagnostic_for_missing_declared_submodule() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod missing

        pub proc entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, None::<Box<Module>>)
        .expect_err("declared missing child module should be rejected");

    assert_diagnostic!(&err, "undefined module '::diag::root::missing'");

    Ok(())
}

#[test]
fn link_diagnostic_for_undeclared_child_module() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub proc entry
            nop
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [child])
        .expect_err("undeclared child module should be rejected");

    assert_diagnostic!(&err, "module '::diag::root::child' is not declared");
    assert_diagnostic!(&err, "`mod child` or `pub mod child`");

    Ok(())
}

#[test]
fn link_diagnostic_for_private_submodule_import() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        mod child

        pub proc entry
            nop
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;
    let consumer = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::consumer

        use diag::root::child

        pub proc entry
            exec.child::child_entry
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", consumer, [root, child])
        .expect_err("private submodule import should be rejected");

    assert_diagnostic!(&err, "private submodule '::diag::root::child'");
    assert_diagnostic!(&err, "only public submodules can be imported from another module");

    Ok(())
}

#[test]
fn private_submodule_is_visible_to_descendants_of_its_parent() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        mod internal
        pub mod api

        pub proc entry
            nop
        end
        "#
    ))?;
    let internal = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::internal

        pub const VALUE = 1
        pub proc internal_entry
            nop
        end
        "#
    ))?;
    let api = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::api

        use {VALUE} from diag::root::internal
        use diag::root::internal as internal_api

        pub proc entry
            push.VALUE
            exec.internal_api::internal_entry
        end
        "#
    ))?;

    Assembler::new(context.source_manager()).assemble_library("diag", root, [internal, api])?;

    Ok(())
}

#[test]
fn private_nested_submodule_is_not_visible_to_sibling_of_parent() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod parent
        pub mod sibling

        pub proc entry
            nop
        end
        "#
    ))?;
    let parent = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::parent

        mod hidden
        "#
    ))?;
    let hidden = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::parent::hidden

        pub const VALUE = 1
        "#
    ))?;
    let sibling = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::sibling

        use {VALUE} from diag::root::parent::hidden

        pub proc entry
            push.VALUE
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [parent, hidden, sibling])
        .expect_err("private nested submodule should not be visible to sibling of its parent");

    assert_diagnostic!(&err, "private submodule '::diag::root::parent::hidden'");
    assert_diagnostic!(&err, "only public submodules can be imported from another module");

    Ok(())
}

#[test]
fn link_diagnostic_for_module_reexport() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child

        pub proc entry
            nop
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;
    let consumer = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::consumer

        pub use {child} from diag::root

        pub proc entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", consumer, [root, child])
        .expect_err("module re-export should be rejected");

    assert_diagnostic!(&err, "item import target '::diag::root::child' resolved to a module");
    assert_diagnostic!(&err, "item-form imports may only import procedures, constants, or types");

    Ok(())
}

#[test]
fn link_diagnostic_for_import_target_through_import_alias() -> TestResult {
    let context = TestContext::new().with_warnings_as_errors(false);
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child

        pub proc entry
            nop
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub const VALUE = 1
        pub proc child_entry
            nop
        end
        "#
    ))?;
    let consumer = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::consumer

        use diag::root::child
        use {VALUE} from child

        pub proc entry
            push.VALUE
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", consumer, [root, child])
        .expect_err("import through another import should be rejected");

    assert_diagnostic!(
        &err,
        "import target 'child::VALUE' cannot be resolved through import 'child'"
    );
    assert_diagnostic!(&err, "imports are resolved independently");

    Ok(())
}

#[test]
fn pub_use_through_import_alias_is_rejected_even_when_global_path_exists() -> TestResult {
    let context = TestContext::new().with_warnings_as_errors(false);
    let imported = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::dep::child

        pub proc p
            push.1
        end
        "#
    ))?;
    let global = context.parse_module(source_file!(
        &context,
        r#"
        namespace child

        pub proc p
            push.2
        end
        "#
    ))?;
    let consumer = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::consumer

        use diag::dep::child
        pub use {p as alias} from child

        pub proc entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", consumer, [imported, global])
        .expect_err("pub use through another import should be rejected before global lookup");

    assert_diagnostic!(&err, "import target 'child::p' cannot be resolved through import 'child'");
    assert_diagnostic!(&err, "imports are resolved independently");

    Ok(())
}

#[test]
fn link_diagnostic_for_self_referential_module_import() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child

        pub proc entry
            nop
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        use diag::root::child

        pub proc child_entry
            exec.child::child_entry
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [child])
        .expect_err("module import of itself should be rejected");

    assert_diagnostic!(&err, "self-referential import of module 'diag::root::child'");
    assert_diagnostic!(&err, "a module cannot import itself");

    Ok(())
}

#[test]
fn link_diagnostic_for_importing_same_scope_submodule_with_alias() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child
        use diag::root::child as child_api

        pub proc entry
            exec.child_api::child_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [child])
        .expect_err("same-scope submodule imports should be rejected even when aliased");

    assert_diagnostic!(&err, "cannot import submodule '::diag::root::child'");
    assert_diagnostic!(&err, "submodule-qualified path");

    Ok(())
}

#[test]
fn link_diagnostic_for_importing_same_scope_submodule_with_self_alias() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child
        use self::child as child_api

        pub proc entry
            exec.child_api::child_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [child])
        .expect_err("self-relative same-scope submodule imports should be rejected");

    assert_diagnostic!(&err, "cannot import submodule '::diag::root::child'");
    assert_diagnostic!(&err, "submodule-qualified path");

    Ok(())
}

#[test]
fn code_paths_can_reference_current_and_descendant_items_absolutely() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        mod child

        proc local_helper
            nop
        end

        pub proc entry
            exec.::diag::root::local_helper
            exec.::diag::root::child::child_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;

    Assembler::new(context.source_manager()).assemble_library("diag", root, [child])?;

    Ok(())
}

#[test]
fn code_paths_can_reference_local_submodules_without_imports() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        mod child

        pub proc entry
            exec.child::child_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;

    Assembler::new(context.source_manager()).assemble_library("diag", root, [child])?;

    Ok(())
}

#[test]
fn link_diagnostic_for_relative_global_like_code_path() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child

        pub proc entry
            exec.diag::root::child::child_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [child])
        .expect_err("relative paths should not fall back to the global namespace");

    assert_diagnostic!(&err, "invalid relative item path 'diag::root::child::child_entry'");
    assert_diagnostic!(&err, "absolute, local, or qualified by an import or submodule");

    Ok(())
}

#[test]
fn code_paths_can_reference_imported_module_subpaths() -> TestResult {
    let context = TestContext::new();
    let dep = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::dep

        pub mod child

        pub proc entry
            nop
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::dep::child

        pub proc child_entry
            nop
        end
        "#
    ))?;
    let consumer = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::consumer

        use diag::dep as dep

        pub proc entry
            exec.dep::child::child_entry
        end
        "#
    ))?;

    Assembler::new(context.source_manager()).assemble_library("diag", consumer, [dep, child])?;

    Ok(())
}

#[test]
fn self_relative_import_walks_public_submodules() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child

        use {child_entry} from self::child

        pub proc entry
            exec.child_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        pub proc child_entry
            push.1
        end
        "#
    ))?;

    Assembler::new(context.source_manager()).assemble_library("diag", root, [child])?;

    Ok(())
}

#[test]
fn self_relative_import_rejects_private_descendant() -> TestResult {
    let context = TestContext::new();
    let root = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root

        pub mod child

        use self::child::hidden

        pub proc entry
            exec.hidden::hidden_entry
        end
        "#
    ))?;
    let child = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child

        mod hidden
        "#
    ))?;
    let hidden = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::root::child::hidden

        pub proc hidden_entry
            nop
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", root, [child, hidden])
        .expect_err("private descendant import should be rejected");

    assert_diagnostic!(&err, "private submodule '::diag::root::child::hidden'");

    Ok(())
}

#[test]
fn link_diagnostic_for_subpath_through_non_module_item() -> TestResult {
    let context = TestContext::new();
    let dep = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::dep

        pub const VALUE = 1
        pub proc entry
            nop
        end
        "#
    ))?;
    let consumer = context.parse_module(source_file!(
        &context,
        r#"
        namespace diag::consumer

        use {nested as NESTED} from diag::dep::VALUE

        pub proc entry
            exec.NESTED
        end
        "#
    ))?;

    let err = Assembler::new(context.source_manager())
        .assemble_library("diag", consumer, [dep])
        .expect_err("subpath through item should be rejected");

    assert_diagnostic!(&err, "invalid symbol path");
    assert_diagnostic!(&err, "all ancestors of a path must be modules");

    Ok(())
}

#[test]
fn imported_error_message_alias_is_resolved_without_panicking() {
    use std::{
        panic::{AssertUnwindSafe, catch_unwind},
        sync::Arc,
    };

    use miden_assembly_syntax::Parse;

    use crate::{Assembler, DefaultSourceManager};

    let source_manager: Arc<dyn crate::SourceManager> = Arc::new(DefaultSourceManager::default());

    // Library module `b` exports a string constant and an alias to it.
    let module_b_src = r#"
namespace b

pub const ERR1 = "oops"
pub const ERR2 = ERR1
"#;
    let module_b = <&str as Parse>::parse(module_b_src, false, source_manager.clone())
        .expect("module b parsing must succeed");

    // Executable module imports `ERR2` and uses it as an assertion error message.
    let module_a_src = r#"
use {ERR2} from b

begin
    assert.err=ERR2
end
"#;

    let assembled = catch_unwind(AssertUnwindSafe(|| {
        let mut assembler = Assembler::new(source_manager);
        assembler
            .compile_and_statically_link(module_b)
            .expect("linking module b must succeed");
        assembler.assemble_program("test", module_a_src)
    }));

    assert!(assembled.is_ok(), "assembler panicked during assembly");
    assembled
        .unwrap()
        .expect("expected imported error message alias to assemble successfully");
}