simplicityhl 0.7.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
use std::collections::HashMap;

use crate::driver::{DependencyGraph, CRATE_STR, MAIN_MODULE, MAIN_STR};
use crate::error::{Diagnostic, DiagnosticManager, Error, Span};
use crate::parse::{self, Visibility};
use crate::str::{Identifier, ModuleName};

/// All enum declarations among `items`, recursing into `mod` blocks.
fn enum_declarations(items: &[parse::Item]) -> Vec<&parse::EnumDeclaration> {
    let mut found = Vec::new();
    for item in items {
        match item {
            parse::Item::EnumDeclaration(decl) => found.push(decl),
            parse::Item::Module(module) => found.extend(enum_declarations(module.items())),
            _ => {}
        }
    }
    found
}

// TODO: allow enums in deps when mentioned problems are resolved
/// Enums by design are nominative, therefore to reason about same named enums in different modules
/// we have to have a stable ABI with the suport of "qualified name".
/// Currently, there is no support of "qualified name" concpet, therefore at the time of creating
/// enums, it is forbidden to decler them in dependencies.    
///
/// If we used current ABI we would face following problems:
/// 1. Adding or removing an unrelated dependency renumbers the files, so the same enum's ABI
///    name changes between builds even though no source changed.
///    The whole point of "identity is the qualified name" is that serialized forms can identify an enum across builds.
/// 2. Unwritable witness files. A user filling in a witness would have
///    to write `unit_2::Action::Cold` (a name that appears nowhere in their source and that they can't predict).
/// 3. Meaningless nominal distinctness. `a::Action` vs `b::Action` being distinct types only makes
///    sense if a and b are the user's module names, not compiler-generated counters.
fn forbid_enum_dec_in_deps(
    source_id: usize,
    local_items: &[parse::Item],
    diagnostics: &mut DiagnosticManager,
) {
    if source_id == MAIN_MODULE {
        return;
    }

    for decl in enum_declarations(local_items) {
        diagnostics.push(Diagnostic::new(
            Error::Grammar {
                msg: format!(
                    "enum `{}` is declared in a dependency file; \
                     enums may only be declared in the program's own files",
                    decl.name()
                ),
            },
            *decl.as_ref(),
        ));
    }
}

/// This is a core component of the [`DependencyGraph`].
impl DependencyGraph {
    /// Resolves the dependency graph and constructs the final AST program.
    pub(crate) fn linearize_and_assemble(
        &self,
        diagnostics: &mut DiagnosticManager,
    ) -> Option<parse::Program> {
        match self.linearize() {
            Ok(order) => self.assemble_program(&order, diagnostics),
            Err(err) => {
                diagnostics.push(err);
                None
            }
        }
    }

    /// Constructs the unified array of items for the entire multi-program.
    fn assemble_program(
        &self,
        order: &[usize],
        diagnostics: &mut DiagnosticManager,
    ) -> Option<parse::Program> {
        let mut items = Vec::with_capacity(order.len());

        let target_ids: HashMap<Span, usize> = self
            .use_cache
            .iter()
            .map(|(span, resolved)| {
                let id = self
                    .sources
                    .id(&resolved.path)
                    .expect("resolved path must be registered in source map");
                (*span, id)
            })
            .collect();

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

            let local_items: Vec<parse::Item> = module
                .program
                .items()
                .iter()
                .filter_map(|item| self.rewrite_item(item, &target_ids))
                .collect();

            if source_id == MAIN_MODULE {
                let has_main = local_items.iter().any(|item| {
                    matches!(item, parse::Item::Function(f) if f.name().as_inner() == MAIN_STR)
                });

                if !has_main {
                    diagnostics.push(Diagnostic::global(Error::CannotParse {
                        msg: Error::MainOutOfEntryFile.to_string(),
                    }));
                }
            }

            forbid_enum_dec_in_deps(source_id, &local_items, diagnostics);

            // TODO(enums): the flattened output wraps every file — the
            // entry file included — in a generated module, but enum
            // declarations are only valid at the top level of a file, so
            // flattening an enum program produces source that no longer
            // re-parses (`TemplateProgram::flatten`). Splice the entry
            // file's items at the root instead of wrapping them.
            let name = ModuleName::from_str_unchecked(Self::get_module_name(source_id).as_inner());
            items.push(parse::Item::Module(parse::Module::new(
                source_id,
                Visibility::Private,
                name,
                &local_items,
            )));
        }

        (!diagnostics.has_errors())
            .then(|| parse::Program::new(&items, *self.modules[&MAIN_MODULE].program.as_ref()))
    }

    /// Rewrites a single item for the flattened single-file representation.
    fn rewrite_item(
        &self,
        item: &parse::Item,
        target_ids: &HashMap<Span, usize>,
    ) -> Option<parse::Item> {
        match item {
            parse::Item::Use(use_decl) => Some(self.rewrite_use(use_decl, target_ids)),
            parse::Item::Module(module) => {
                let items: Vec<parse::Item> = module
                    .items()
                    .iter()
                    .filter_map(|inner_item| self.rewrite_item(inner_item, target_ids))
                    .collect();

                Some(parse::Item::Module(parse::Module::new(
                    module.span().file_id,
                    module.visibility().clone(),
                    module.name().clone(),
                    &items,
                )))
            }
            parse::Item::TypeAlias(_)
            | parse::Item::Function(_)
            | parse::Item::EnumDeclaration(_) => Some(item.clone()),
            parse::Item::Ignored => None,
        }
    }

    /// Rewrites a `use` declaration into its canonical `crate`-rooted form.
    ///
    /// The resolved path becomes `crate::unit_<N>::<mod_path...>`, where `N` is
    /// the source id of the file that owns the imported item.
    ///
    /// ## Example
    ///
    /// - `use base_math::simple_op::hash` → `use crate::unit_2::hash`
    fn rewrite_use(
        &self,
        use_decl: &parse::UseDecl,
        target_ids: &HashMap<Span, usize>,
    ) -> parse::Item {
        let span = *use_decl.span();
        let resolved = &self.use_cache[&span];
        let target_id = target_ids[&span];

        let mut new_path = Vec::with_capacity(resolved.mod_path.len() + 2);
        new_path.push(Identifier::from_str_unchecked(CRATE_STR));
        new_path.push(Self::get_module_name(target_id));
        new_path.extend(resolved.mod_path.iter().cloned());

        let mut use_decl = use_decl.clone();
        use_decl.set_path(&new_path);
        parse::Item::Use(use_decl)
    }

    fn get_module_name(source_id: usize) -> Identifier {
        Identifier::from_str_unchecked(format!("unit_{}", source_id).as_str())
    }
}

#[cfg(test)]
mod flattening_tests {
    use crate::driver::tests::setup_graph;
    use crate::driver::CRATE_STR;
    use crate::parse::{self, Visibility};

    use std::collections::HashMap;

    // Helper to get the built program
    fn build_flattened_program(
        files: Vec<(&str, &str)>,
    ) -> (parse::Program, HashMap<String, usize>) {
        let (graph, ids, _dir, mut diagnostics) = setup_graph(files);

        let Some(program) = graph.linearize_and_assemble(&mut diagnostics) else {
            panic!("{}", &diagnostics);
        };

        (program, ids)
    }

    #[test]
    fn test_dependency_is_wrapped_in_file_module() {
        // Scenario: A dependency file MUST be wrapped in a `mod file_N` block,
        // and its visibility must be Private to prevent leaking.
        let (program, ids) = build_flattened_program(vec![
            ("libs/lib/A.simf", "pub fn dep_func() {}"),
            ("main.simf", "use lib::A::dep_func; fn main() {}"),
        ]);

        let file_a_id = ids["A"];
        let expected_mod_name = format!("unit_{}", file_a_id);

        let wrapped_module = program
            .items()
            .iter()
            .find_map(|item| {
                if let parse::Item::Module(m) = item {
                    if m.name().as_inner() == expected_mod_name.as_str() {
                        return Some(m);
                    }
                }
                None
            })
            .expect("Dependency should be wrapped in a file_N module");

        assert!(
            matches!(wrapped_module.visibility(), Visibility::Private),
            "The file wrapper module must be strictly private"
        );

        let has_dep_func = wrapped_module.items().iter().any(
            |item| matches!(item, parse::Item::Function(f) if f.name().as_inner() == "dep_func"),
        );
        assert!(
            has_dep_func,
            "The file_N module must contain the dependency's items"
        );
    }

    #[test]
    fn test_use_paths_are_rewritten_to_canonical_files() {
        // Scenario: When main.simf says `use lib::A::foo`, the AST flattener
        // must rewrite this path to `use crate::file_N::foo`.
        let (program, ids) = build_flattened_program(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("main.simf", "use lib::A::foo; fn main() {}"),
        ]);

        let file_a_id = ids["A"];
        let expected_file_segment = format!("unit_{}", file_a_id);

        // Flatten the modules and search their inner contents
        let use_decl = program
            .items()
            .iter()
            .filter_map(|item| {
                if let parse::Item::Module(module) = item {
                    Some(module.items()) // Get the slice of inner items
                } else {
                    None
                }
            })
            .flatten() // Unpack all the inner slices into a single stream
            .find_map(|inner_item| {
                if let parse::Item::Use(u) = inner_item {
                    Some(u)
                } else {
                    None
                }
            })
            .expect("Main module should contain a use declaration");

        // Get the segments of the rewritten path
        let path = use_decl.path();

        assert!(
            path.len() >= 2,
            "Rewritten path must have at least 2 segments"
        );
        assert_eq!(
            path[0].as_inner(),
            CRATE_STR,
            "Path must start with `crate`"
        );
        assert_eq!(
            path[1].as_inner(),
            expected_file_segment.as_str(),
            "Path must route through the canonical `unit_N`"
        );
    }

    #[test]
    fn dependency_main_does_not_satisfy_missing_root_main() {
        let (graph, _ids, _dir, mut diagnostics) = setup_graph(vec![
            ("main.simf", "use lib::A::helper;"),
            (
                "libs/lib/A.simf",
                "fn main() { assert!(false); } pub fn helper() {}",
            ),
        ]);

        let driver_program = graph.linearize_and_assemble(&mut diagnostics);

        assert!(
            driver_program.is_none(),
            "Expected the build to fail and return None, but got: {:?}",
            driver_program
        );

        assert!(
            diagnostics.has_errors(),
            "a dependency `fn main` must not satisfy a missing entrypoint `fn main`"
        );
    }
}

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

    // Helper to run the driver and return the error collector so we can inspect it.
    fn run_driver(files: Vec<(&str, &str)>) -> DiagnosticManager {
        let (graph, _ids, _dir, mut diagnostics) = setup_graph(files);
        let _ = graph.linearize_and_assemble(&mut diagnostics).unwrap();
        diagnostics
    }

    #[test]
    fn test_crate_path_resolves_to_physical_file() {
        // Scenario: `crate::utils::math` should map to the physical `utils/math.simf` file.
        let diagnostics = run_driver(vec![
            ("utils/math.simf", "pub fn add() {}"),
            ("main.simf", "use crate::utils::math::add; fn main() {}"),
        ]);

        assert!(
            !diagnostics.has_errors(),
            "Driver should successfully find the physical file 'utils/math.simf'. Errors: {}",
            diagnostics
        );
    }

    #[test]
    fn test_crate_path_fallback_to_inline_module() {
        // Scenario: `brother.simf` does NOT exist. `crate::brother` must fallback
        // to `main.simf` and treat `brother` as an inline mod_path.
        let diagnostics = run_driver(vec![(
            "main.simf",
            "
                mod brother { pub fn toy() {} }
                use crate::brother::toy; 
                fn main() {}
            ",
        )]);

        assert!(
            !diagnostics.has_errors(),
            "Driver must fallback to main.simf for inline modules without throwing FileNotFound. Errors: {}",
            diagnostics
        );
    }

    #[test]
    fn test_crate_path_deeply_nested_inline_fallback() {
        // Scenario: A physical file exists (`utils.simf`), but the REST of the path is inline modules!
        let diagnostics = run_driver(vec![
            (
                "utils.simf",
                "pub mod deeply { pub mod nested { pub fn func() {} } }",
            ),
            (
                "main.simf",
                "use crate::utils::deeply::nested::func; fn main() {}",
            ),
        ]);

        assert!(
            !diagnostics.has_errors(),
            "Driver must split the path at the file boundary correctly. Errors: {}",
            diagnostics
        );
    }

    #[test]
    fn test_external_dependency_resolution() {
        // Scenario: Resolving `use lib::A::foo` across the remapping boundary.
        let diagnostics = run_driver(vec![
            ("libs/lib/A.simf", "pub fn foo() {}"),
            ("main.simf", "use lib::A::foo; fn main() {}"),
        ]);

        assert!(
            !diagnostics.has_errors(),
            "External dependency resolution via drp_name failed. Errors: {}",
            diagnostics
        );
    }
}