simplicityhl 0.6.0-rc.0

Rust-like language that compiles to Simplicity bytecode.
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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::sync::Arc;

use crate::driver::{DependencyGraph, MAIN_MODULE, MAIN_STR};
use crate::error::{Error, ErrorCollector, RichError, Span};
use crate::impl_eq_hash;
use crate::parse::{self, AliasedSymbolName, Function, TypeAlias, Visibility};
use crate::str::{AliasName, FunctionName, SymbolName};

/// The final, flattened representation of a SimplicityHL program.
///
/// This struct holds the fully resolved sequence of items, paths, and scope
/// resolutions, ready to be passed to the next stage of the compiler.
#[derive(Clone, Debug)]
pub struct Program {
    /// The linear sequence of compiled items (`Functions`, `TypeAliases`, etc.).
    items: Arc<[parse::Item]>,

    /// Contains all resolved aliases for the local scopes and the global import registry.
    aliases: SymbolTable<AliasName>,

    /// Contains all resolved functions for the local scopes and the global import registry.
    functions: SymbolTable<FunctionName>,

    span: Span,
}

impl Program {
    pub fn from_parse(
        parsed: &parse::Program,
        content: Arc<str>,
        handler: &mut ErrorCollector,
    ) -> Option<Self> {
        let module_count = 1;

        let mut items: Vec<parse::Item> = Vec::new();

        let mut aliases = NamespaceTracker::<AliasName>::new(module_count);
        let mut functions = NamespaceTracker::<FunctionName>::new(module_count);

        for item in parsed.items() {
            if let parse::Item::Use(use_decl) = item {
                handler.push(
                    RichError::new(Error::UnknownLibrary(use_decl.str_path()), *use_decl.span())
                        .with_content(content.clone()),
                );
                continue;
            }

            let mut new_elem = item.clone();
            match &mut new_elem {
                parse::Item::TypeAlias(type_alias) => {
                    if let Err(err) = register_type_alias(type_alias, &mut aliases, MAIN_MODULE) {
                        handler.push(err.with_content(content.clone()));
                        continue;
                    }
                }
                parse::Item::Function(function) => {
                    if let Err(err) = register_function(function, &mut functions, MAIN_MODULE) {
                        handler.push(err.with_content(content.clone()));
                        continue;
                    }
                }

                // Safe to skip: `Use` items are handled earlier in the loop, and `Module` currently has no functionality.
                parse::Item::Module | parse::Item::Use(_) => continue,
            }
            items.push(new_elem);
        }

        // TODO: Consider getting rid of the 'String' error here and changing it to a more appropriate error
        // (e.g. 'Result<Self, ErrorCollector>') after resolving https://github.com/BlockstreamResearch/SimplicityHL/issues/270.
        (!handler.has_errors()).then(|| Program {
            items: items.into(),
            aliases: aliases.into_symbol_table(),
            functions: functions.into_symbol_table(),
            span: *parsed.as_ref(),
        })
    }

    pub fn items(&self) -> &[parse::Item] {
        &self.items
    }

    pub fn aliases(&self) -> &SymbolTable<AliasName> {
        &self.aliases
    }

    pub fn functions(&self) -> &SymbolTable<FunctionName> {
        &self.functions
    }

    pub fn span(&self) -> &Span {
        &self.span
    }
}

impl_eq_hash!(Program; items, aliases, functions);

/// Holds all scoping and import data for a specific namespace (e.g., Functions or Aliases).
#[derive(Clone, Debug)]
pub struct SymbolTable<T> {
    /// The items available in each file's local scope.
    /// The index of the array corresponds to the file ID.
    local_scopes: Arc<[BTreeSet<T>]>,

    /// The cross-file import mappings and cached roots.
    imports: ImportRegistry<T>,
}

impl<T> SymbolTable<T> {
    pub fn local_scopes(&self) -> &[BTreeSet<T>] {
        &self.local_scopes
    }

    pub fn imports(&self) -> &ImportRegistry<T> {
        &self.imports
    }
}

impl_eq_hash!(SymbolTable<T>; local_scopes, imports);

/// Represents an item name alongside its originating file ID.
pub type FileScoped<T> = (T, usize);

/// A registry mapping an alias [`FileScoped<T>`] to its target item across different files.
///
/// We use a type alias here to provide a convenient abstraction for the `AST::analyze`
/// phase, making it easier to modify the underlying structure in the future if needed.
pub type ImportMap<T> = BTreeMap<FileScoped<T>, FileScoped<T>>;

/// Manages the resolution of import aliases across the entire program.
#[derive(Clone, Debug)]
pub struct ImportRegistry<T> {
    direct_targets: ImportMap<T>,
    resolved_roots: ImportMap<T>,
}

impl<T> ImportRegistry<T> {
    pub fn direct_targets(&self) -> &ImportMap<T> {
        &self.direct_targets
    }

    pub fn resolved_roots(&self) -> &ImportMap<T> {
        &self.resolved_roots
    }
}

impl_eq_hash!(ImportRegistry<T>; direct_targets, resolved_roots);

/// This is a core component of the [`DependencyGraph`].
impl DependencyGraph {
    /// Resolves the dependency graph and constructs the final AST program.
    pub fn linearize_and_build(
        &self,
        handler: &mut ErrorCollector,
    ) -> Result<Option<Program>, String> {
        match self.linearize() {
            Ok(order) => Ok(self.build_program(&order, handler)),
            Err(err) => Err(err.to_string()),
        }
    }

    /// Constructs the unified AST for the entire program.
    fn build_program(&self, order: &[usize], handler: &mut ErrorCollector) -> Option<Program> {
        let mut items: Vec<parse::Item> = Vec::new();

        let mut aliases = NamespaceTracker::<AliasName>::new(self.modules.len());
        let mut functions = NamespaceTracker::<FunctionName>::new(self.modules.len());

        for &source_id in order {
            let module = &self.modules[source_id];
            let source = &module.source;

            for elem in module.parsed_program.items() {
                // Handle Uses (Early Continue flattens the nesting)
                if let parse::Item::Use(use_decl) = elem {
                    let resolve_path =
                        match self.dependency_map.resolve_path(source.name(), use_decl) {
                            Ok(path) => path,
                            Err(err) => {
                                handler.push(err.with_source(source.clone()));
                                continue;
                            }
                        };

                    let ind = self.lookup[&resolve_path];
                    let use_decl_items = match use_decl.items() {
                        parse::UseItems::Single(elem) => std::slice::from_ref(elem),
                        parse::UseItems::List(elems) => elems.as_slice(),
                    };

                    for aliased_item in use_decl_items {
                        let alias_err = Self::process_use_item(
                            &mut aliases,
                            source_id,
                            ind,
                            aliased_item,
                            use_decl,
                        );

                        let function_err = Self::process_use_item(
                            &mut functions,
                            source_id,
                            ind,
                            aliased_item,
                            use_decl,
                        );

                        if let Err(err) =
                            Self::resolve_processing_use_items_error(alias_err, function_err)
                        {
                            handler.push(err.with_source(source.clone()));
                        }
                    }
                    continue;
                }

                // Handle Types & Functions by inserting them into their STRICT namespaces
                let mut new_elem = elem.clone();
                match &mut new_elem {
                    parse::Item::TypeAlias(type_alias) => {
                        if let Err(err) = register_type_alias(type_alias, &mut aliases, source_id) {
                            handler.push(err.with_source(source.clone()));
                            continue;
                        }
                    }
                    parse::Item::Function(function) => {
                        if let Err(err) = register_function(function, &mut functions, source_id) {
                            handler.push(err.with_source(source.clone()));
                            continue;
                        }
                    }

                    // Safe to skip: `Use` items are handled earlier in the loop, and `Module` currently has no functionality.
                    parse::Item::Module | parse::Item::Use(_) => continue,
                }
                items.push(new_elem);
            }
        }

        (!handler.has_errors()).then(|| Program {
            items: items.into(),
            aliases: aliases.into_symbol_table(),
            functions: functions.into_symbol_table(),
            span: *self.modules[0].parsed_program.as_ref(),
        })
    }

    /// Attempts to pick the most helpful error when an import fails in both namespaces.
    ///
    /// Since SimplicityHL supports separated namespaces, a single `use` statement
    /// may successfully load a `Function`, a `TypeAlias`, or both simultaneously.
    fn resolve_processing_use_items_error(
        alias: Result<(), RichError>,
        function: Result<(), RichError>,
    ) -> Result<(), RichError> {
        match (alias, function) {
            (Ok(()), _) | (_, Ok(())) => Ok(()),

            (Err(err_alias), Err(err_func)) => {
                let alias_is_missing = matches!(err_alias.error(), Error::UnresolvedItem(_));
                let func_is_missing = matches!(err_func.error(), Error::UnresolvedItem(_));

                if !alias_is_missing || func_is_missing {
                    // If it's missing everywhere, OR if the function is missing
                    // but the alias has a specific error (like PrivateItem).
                    Err(err_alias)
                } else {
                    Err(err_func)
                }
            }
        }
    }

    /// Processes a single imported item (or alias) and registers it within a specific namespace.
    ///
    /// This function verifies that the requested item exists in the source module and has the appropriate public
    /// visibility. If validation passes and no local naming collisions are found, the item is registered
    /// in the destination module's local scope and the global import registry.
    ///
    /// # Arguments
    ///
    /// * `namespace` - The generic tracker (e.g., for Functions or Aliases) that holds
    ///   the local file scopes, the global import registry, and the memoization set to prevent collisions.
    /// * `source_id` - The `usize` identifier of the destination module where the item is being imported *to*.
    /// * `ind` - The unique identifier of the source module being imported *from*.
    /// * `aliased_symbol_name` - The specific identifier (and potential alias) being imported from the source.
    /// * `use_decl` - The node of the `use` statement. This dictates the visibility of the new import
    ///   (e.g., `pub use` re-exports the item publicly).
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` on success. Returns `Err(RichError)` if:
    /// * [`Error::UnresolvedItem`]: The target name does not exist in the source module (`ind`).
    /// * [`Error::PrivateItem`]: The target exists, but its visibility is explicitly `Private`.
    /// * [`Error::MainCannotBeAlias`]: The `main` cannot be alias.
    /// * [`Error::DuplicateAlias`]: The local name (or alias) has already been used in another import statement.
    /// * [`Error::RedefinedItem`]: The local name conflicts with an existing item already defined in this module.
    fn process_use_item<T>(
        namespace: &mut NamespaceTracker<T>,
        source_id: usize,
        ind: usize,
        (name, alias): &AliasedSymbolName,
        use_decl: &parse::UseDecl,
    ) -> Result<(), RichError>
    where
        T: From<SymbolName> + std::fmt::Display + Clone + Eq + std::hash::Hash + std::cmp::Ord,
    {
        // NOTE: The order of errors is important!
        let span = *use_decl.span();

        // 1. Convert the unresolved SymbolName into our strict type T
        let target_name: T = name.clone().into();
        let orig_id = (target_name.clone(), ind);

        // 2. Verify Existence using T
        let visibility: &Visibility = namespace.resolutions[ind]
            .get(&target_name)
            .ok_or_else(|| RichError::new(Error::UnresolvedItem(name.to_string()), span))?;

        // 3. Verify Visibility
        if matches!(visibility, parse::Visibility::Private) {
            return Err(RichError::new(Error::PrivateItem(name.to_string()), span));
        }

        // 4. Determine the local name and ID up front
        // We figure out the raw symbol first, so we can use it for error messages
        let local_symbol = alias.as_ref().unwrap_or(name);

        // Then convert that raw symbol to T
        let local_name: T = if let Some(alias_sym) = alias {
            let t_alias: T = alias_sym.clone().into();

            if t_alias.to_string() == MAIN_STR {
                return Err(RichError::new(Error::MainCannotBeAlias, span));
            }
            t_alias
        } else {
            name.clone().into()
        };

        let local_id = (local_name.clone(), source_id);

        // 5. Check for collisions using `namespace` fields
        if namespace.registry.direct_targets.contains_key(&local_id) {
            return Err(RichError::new(
                Error::DuplicateAlias(local_symbol.to_string()),
                span,
            ));
        }

        if namespace.memo.contains(&local_id) {
            return Err(RichError::new(
                Error::RedefinedItem(local_symbol.to_string()),
                span,
            ));
        }
        namespace.memo.insert(local_id.clone());

        // 6. Update the registers
        namespace
            .registry
            .direct_targets
            .insert(local_id.clone(), orig_id.clone());

        // 7. Find the true root
        let true_root = namespace
            .registry
            .resolved_roots
            .get(&orig_id)
            .cloned()
            .unwrap_or_else(|| orig_id.clone());

        namespace
            .registry
            .resolved_roots
            .insert(local_id, true_root);

        // 8. Register the item in the local module's namespace
        namespace.resolutions[source_id].insert(local_name, use_decl.visibility().clone());
        Ok(())
    }
}

// Architectural Note:
// The two functions may seem duplicated. To prevent this, the best approach
// would be to add a `NamedItem` trait. However, doing so would require
// duplicating getter methods for both `Function` and `TypeAlias`.
// As a result, it is better to leave it as is.
fn register_type_alias(
    item: &mut TypeAlias,
    tracker: &mut NamespaceTracker<AliasName>,
    source_id: usize,
) -> Result<(), RichError> {
    item.set_file_id(source_id);

    let name = item.name();
    let local_id = (name.clone(), source_id);

    if tracker.memo.contains(&local_id) {
        return Err(RichError::new(
            Error::RedefinedAlias(name.clone()),
            *item.span(),
        ));
    }

    tracker.memo.insert(local_id);
    tracker.resolutions[source_id].insert(name.clone(), item.visibility().clone());
    Ok(())
}

fn register_function(
    item: &mut Function,
    tracker: &mut NamespaceTracker<FunctionName>,
    source_id: usize,
) -> Result<(), RichError> {
    item.set_file_id(source_id);

    let name = item.name();
    let local_id = (name.clone(), source_id);

    if name.as_inner() == MAIN_STR && matches!(item.visibility(), Visibility::Public) {
        return Err(RichError::new(Error::MainCannotBePublic, *item.span()));
    }

    if tracker.memo.contains(&local_id) {
        return Err(RichError::new(
            Error::FunctionRedefined(name.clone()),
            *item.span(),
        ));
    }

    tracker.memo.insert(local_id);
    tracker.resolutions[source_id].insert(name.clone(), item.visibility().clone());
    Ok(())
}

/// Helper struct, that tracks the resolution state, imports, and memoization for a single namespace.
#[derive(Clone, Debug)]
struct NamespaceTracker<T> {
    /// Local resolutions per file.
    resolutions: Vec<HashMap<T, Visibility>>,

    /// Global registry for `use` imports and aliasing.
    registry: ImportRegistry<T>,

    /// Tracks processed items to prevent infinite loops or redefinitions.
    memo: BTreeSet<FileScoped<T>>,
}

impl<T: Ord + Clone + Default + std::hash::Hash> NamespaceTracker<T> {
    pub fn new(module_count: usize) -> Self {
        Self {
            resolutions: vec![HashMap::new(); module_count],
            registry: ImportRegistry::<T>::default(),
            memo: BTreeSet::new(),
        }
    }

    pub fn into_symbol_table(self) -> SymbolTable<T> {
        SymbolTable {
            local_scopes: self
                .resolutions
                .into_iter()
                .map(|map| map.into_keys().collect::<BTreeSet<_>>())
                .collect::<Vec<_>>()
                .into(),
            imports: self.registry,
        }
    }
}

impl<T> Default for ImportRegistry<T> {
    fn default() -> Self {
        Self {
            direct_targets: BTreeMap::new(),
            resolved_roots: BTreeMap::new(),
        }
    }
}

impl<T> Default for SymbolTable<T> {
    fn default() -> Self {
        Self {
            local_scopes: Arc::from([]),
            imports: ImportRegistry::<T>::default(),
        }
    }
}

impl AsRef<Span> for Program {
    fn as_ref(&self) -> &Span {
        &self.span
    }
}

#[cfg(test)]
mod resolve_order_tests {
    use crate::driver::tests::setup_graph;

    use super::*;

    #[test]
    fn test_local_definitions_visibility() {
        // main.simf defines a private function and a public function.
        // Expected: Both should appear in the scope with correct visibility.

        let (graph, ids, _dir) = setup_graph(vec![(
            "main.simf",
            "fn private_fn() {} pub fn public_fn() {}",
        )]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        let Some(program) = program_option else {
            panic!("{}", error_handler);
        };

        let root_id = ids["main"];
        let resolutions = &program.functions.local_scopes[root_id];

        resolutions
            .get(&FunctionName::from_str_unchecked("private_fn"))
            .expect("private_fn missing");

        resolutions
            .get(&FunctionName::from_str_unchecked("public_fn"))
            .expect("public_fn missing");
    }

    #[test]
    fn test_pub_use_propagation() {
        // Scenario: Re-exporting.
        // 1. A.simf defines `pub fn foo`.
        // 2. B.simf imports it and re-exports it via `pub use`.
        // 3. main.simf imports it from B.
        // Expected: B's scope must contain `foo` marked as Public.

        let (graph, ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("libs/lib/B.simf", "pub use lib::A::foo;"),
            ("main.simf", "use lib::B::foo;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        let Some(program) = program_option else {
            panic!("{}", error_handler);
        };

        let id_b = ids["B"];
        let id_root = ids["main"];

        // Check B's scope
        program.functions.local_scopes[id_b]
            .get(&FunctionName::from_str_unchecked("foo"))
            .expect("foo missing in B");

        // Check Root's scope
        program.functions.local_scopes[id_root]
            .get(&FunctionName::from_str_unchecked("foo"))
            .expect("foo missing in Root");
    }

    #[test]
    fn test_private_import_encapsulation_error() {
        // Scenario: Access violation.
        // 1. A.simf defines `pub fn foo`.
        // 2. B.simf imports it via `use` (Private import).
        // 3. main.simf tries to import `foo` from B.
        // Expected: Error, because B did not re-export foo.

        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("libs/lib/B.simf", "use lib::A::foo;"), // <--- Private binding!
            ("main.simf", "use lib::B::foo;"),       // <--- Should fail
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "Build should fail and return None when importing a private binding"
        );

        assert!(error_handler
            .to_string()
            .contains(&"Item `foo` is private".to_string()));
    }

    #[test]
    fn test_separated_type_aliases_and_functions() {
        let (graph, ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub type bar = u32; pub fn bar() {}"),
            ("main.simf", "use lib::A::bar;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        let Some(program) = program_option else {
            panic!("{}", error_handler);
        };

        let root_id = ids["main"];

        // Check B's scope
        program.functions.local_scopes[root_id]
            .get(&FunctionName::from_str_unchecked("bar"))
            .expect("Function bar missing in main");

        // Check Root's scope
        program.aliases.local_scopes[root_id]
            .get(&AliasName::from_str_unchecked("bar"))
            .expect("Type alias missing in main");
    }

    #[test]
    fn test_private_alias_error_does_not_mask_duplicate_function_import() {
        // Scenario:
        // main.simf: load function `foo` from A.simf.
        // Then try to load both `fn foo` and `type foo`.
        // However, we have already loade `fn foo` and `type foo` is private, so an error occurs.
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("libs/lib/B.simf", "pub fn foo() {} type foo = u32;"),
            ("main.simf", "use lib::A::foo; use lib::B::foo;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();
        let _errors = error_handler.to_string();

        assert!(
            program_option.is_none(),
            "build should fail when a second import reuses the function name `foo`"
        );
    }

    #[test]
    fn test_public_main_is_forbidden() {
        // Scenario: A user tries to declare the entry point as `pub fn main`.
        // Expected: The compiler must reject this because `main` must be private.

        let (graph, _ids, _dir) = setup_graph(vec![("main.simf", "pub fn main() {}")]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "Compiler should return None when `main` is declared public"
        );

        let error_msg = error_handler.to_string();
        assert!(
            error_msg.contains("main") && error_msg.contains("public"),
            "Error message should mention that `main` cannot be public. Got: {}",
            error_msg
        );
    }

    #[test]
    fn test_aliasing_to_main_is_forbidden() {
        // Scenario: A user tries to bypass entry point rules by renaming an import to `main`.
        // Expected: The compiler must reject this because `main` is a reserved identifier.

        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub type bar = u32;"),
            ("main.simf", "use lib::A::bar as main;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "Compiler should return None when a user tries to alias an import to `main`"
        );

        let error_msg = error_handler.to_string();
        assert!(
            error_msg.contains("main") && error_msg.contains("alias"),
            "Error message should clearly state that `main` cannot be used as an alias. Got: {}",
            error_msg
        );
    }
}

#[cfg(test)]
mod alias_tests {
    use super::*;
    use crate::driver::tests::setup_graph;

    #[test]
    fn test_renaming_with_use() {
        // Scenario: Renaming imports.
        // main.simf: use lib::A::foo as bar;
        // Expected: Scope should contain "bar", but not "foo".

        let (graph, ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("main.simf", "use lib::A::foo as bar;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        let Some(program) = program_option else {
            panic!("{}", error_handler);
        };

        let id_root = ids["main"];
        let scope = &program.functions.local_scopes[id_root];

        assert!(
            scope
                .get(&FunctionName::from_str_unchecked("foo"))
                .is_none(),
            "Original name 'foo' should not be in scope"
        );
        assert!(
            scope
                .get(&FunctionName::from_str_unchecked("bar"))
                .is_some(),
            "Alias 'bar' should be in scope"
        );
    }

    #[test]
    fn test_multiple_aliases_in_list() {
        // Scenario: Renaming multiple imports inside brackets.
        let (graph, ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn foo() {} pub fn baz() {}"),
            ("main.simf", "use lib::A::{foo as bar, baz as qux};"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        let Some(program) = program_option else {
            panic!("{}", error_handler);
        };

        let id_root = ids["main"];
        let scope = &program.functions.local_scopes[id_root];

        // The original names should NOT be in scope
        assert!(scope
            .get(&FunctionName::from_str_unchecked("foo"))
            .is_none());
        assert!(scope
            .get(&FunctionName::from_str_unchecked("baz"))
            .is_none());

        // The aliases MUST be in scope
        assert!(scope
            .get(&FunctionName::from_str_unchecked("bar"))
            .is_some());
        assert!(scope
            .get(&FunctionName::from_str_unchecked("qux"))
            .is_some());
    }

    #[test]
    fn test_alias_private_item_fails() {
        // Scenario: Attempting to alias a private item should fail.
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "fn secret() {}"), // Note: Missing `pub`
            ("main.simf", "use lib::A::secret as my_secret;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "Compiler should emit an error and return None when aliasing a private item"
        );

        assert!(
            error_handler
                .to_string()
                .contains("Item `secret` is private"),
            "Error should mention the private item restriction"
        );
    }

    #[test]
    fn test_deep_reexport_with_aliases() {
        // Scenario: Chaining aliases across multiple files.
        let (graph, ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn original() {}"),
            ("libs/lib/B.simf", "pub use lib::A::original as middle;"),
            ("main.simf", "use lib::B::middle as final_name;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        let Some(program) = program_option else {
            panic!("{}", error_handler);
        };

        let id_b = ids["B"];
        let id_root = ids["main"];

        // Assert Main Scope
        let main_scope = &program.functions.local_scopes[id_root];
        assert!(main_scope
            .get(&FunctionName::from_str_unchecked("original"))
            .is_none());
        assert!(main_scope
            .get(&FunctionName::from_str_unchecked("middle"))
            .is_none());
        assert!(
            main_scope
                .get(&FunctionName::from_str_unchecked("final_name"))
                .is_some(),
            "Main must see the final alias"
        );

        // Assert B Scope (It should have the intermediate alias!)
        let b_scope = &program.functions.local_scopes[id_b];
        assert!(
            b_scope
                .get(&FunctionName::from_str_unchecked("middle"))
                .is_some(),
            "File B must contain its own public alias"
        );
    }

    #[test]
    fn test_deep_reexport_private_link_fails() {
        // Scenario: Main tries to import an alias from B, but B's alias is private!
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn target() {}"),
            // Note: Missing `pub` keyword here! This makes `hidden_alias` private to B.
            ("libs/lib/B.simf", "use lib::A::target as hidden_alias;"),
            ("main.simf", "use lib::B::hidden_alias;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "Compiler must return None when trying to import a private alias from an intermediate module"
        );

        assert!(
            error_handler
                .to_string()
                .contains("Item `hidden_alias` is private"),
            "Error should correctly identify the private intermediate alias"
        );
    }

    #[test]
    fn test_alias_cycle_detection() {
        // Scenario: A malicious or confused user creates an infinite alias/import loop.
        let (graph, _ids, _dir) = setup_graph(vec![
            // A imports from B, B imports from A. This creates a file-level cycle!
            ("libs/lib/A.simf", "pub use lib::B::pong as ping;"),
            ("libs/lib/B.simf", "pub use lib::A::ping as pong;"),
            ("main.simf", "use lib::A::ping;"),
        ]);

        let mut error_handler = ErrorCollector::new();

        // Because A and B depend on each other, `linearize()` should catch the cycle
        // and return an Err(...) directly, rather than causing a Stack Overflow.
        let result = graph.linearize_and_build(&mut error_handler);

        match result {
            Err(e) => {
                println!("{e}");
                assert!(
                    e.contains("Cycle") || e.contains("Circular"),
                    "DFS Linearizer must catch infinite alias cycles"
                );
            }
            Ok(None) => {
                assert!(
                    error_handler.has_errors(),
                    "If linearization passes, the builder must catch the cycle"
                );
            }
            Ok(Some(_)) => {
                panic!("Expected compilation to fail due to a dependency cycle, but it succeeded!")
            }
        }
    }

    #[test]
    fn test_plain_import_and_alias_to_same_name_is_rejected() {
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("libs/lib/B.simf", "pub fn foo() {}"),
            ("main.simf", "use lib::A::foo; use lib::B::foo as foo;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "build should fail when two imports bind the same local name"
        );
        assert!(
            error_handler
                .to_string()
                .contains("The alias `foo` was defined multiple times"),
            "expected a duplicate-alias diagnostic"
        );
    }

    #[test]
    fn test_failed_alias_import_does_not_poison_following_imports() {
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn nope() {}"),
            ("libs/lib/B.simf", "pub fn bar() {}"),
            (
                "main.simf",
                "use lib::A::missing as foo; use lib::B::bar as foo;",
            ),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();
        let errors = error_handler.to_string();

        assert!(
            program_option.is_none(),
            "build should fail on the unresolved import"
        );
        assert!(errors.contains("Item `missing` could not be found"));
        assert!(
            !errors.contains("The alias `foo` was defined multiple times"),
            "a failed import must not reserve the alias name"
        );
    }

    #[test]
    fn test_alias_cannot_reuse_local_definition_name() {
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn bar() {}"),
            ("main.simf", "pub fn foo() {} use lib::A::bar as foo;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        dbg!(&error_handler.to_string());

        assert!(
            program_option.is_none(),
            "build should fail when an alias reuses a local definition name"
        );
        assert!(
            error_handler
                .to_string()
                .contains("Item `foo` was defined multiple times"),
            "expected a redefined-item diagnostic"
        );
    }

    #[test]
    fn test_local_function_cannot_reuse_alias_name() {
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub fn bar() {}"),
            ("main.simf", "use lib::A::bar as foo; pub fn foo() {}"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "build should fail when a local definition reuses an alias name"
        );

        assert!(
            error_handler
                .to_string()
                .contains("Function `foo` was defined multiple times"),
            "expected a redefined-item diagnostic"
        );
    }

    #[test]
    fn test_local_type_alias_cannot_reuse_alias_name() {
        let (graph, _ids, _dir) = setup_graph(vec![
            ("libs/lib/A.simf", "pub type bar = u32;"),
            ("main.simf", "use lib::A::bar as foo; type foo = u64;"),
        ]);

        let mut error_handler = ErrorCollector::new();
        let program_option = graph.linearize_and_build(&mut error_handler).unwrap();

        assert!(
            program_option.is_none(),
            "build should fail when a local definition reuses an alias name"
        );

        assert!(
            error_handler
                .to_string()
                .contains("Type alias `foo` was defined multiple times"),
            "expected a redefined-item diagnostic"
        );
    }
}