llmcc-rust 0.2.21

llmcc: llm context compiler
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
/// Tests for let declaration type binding.
/// This module verifies that visit_let_declaration properly extracts type annotations
/// and establishes dependency relations between functions and the types used in let statements.
use llmcc_core::ir::HirId;
use llmcc_core::symbol::Symbol;
use llmcc_rust::{bind_symbols, build_llmcc_ir, collect_symbols, CompileCtxt, LangRust};

fn compile(
    source: &str,
) -> (
    &'static CompileCtxt<'static>,
    llmcc_core::context::CompileUnit<'static>,
    llmcc_rust::CollectionResult,
) {
    let sources = vec![source.as_bytes().to_vec()];
    let cc = Box::leak(Box::new(CompileCtxt::from_sources::<LangRust>(&sources)));
    let unit = cc.compile_unit(0);
    build_llmcc_ir::<LangRust>(cc).unwrap();
    let globals = cc.create_globals();
    let collection = collect_symbols(unit, globals);
    bind_symbols(unit, globals);
    (cc, unit, collection)
}

fn find_struct<'a>(
    collection: &'a llmcc_rust::CollectionResult,
    name: &str,
) -> &'a llmcc_rust::StructDescriptor {
    collection
        .structs
        .iter()
        .find(|desc| desc.name == name)
        .unwrap()
}

fn find_function<'a>(
    collection: &'a llmcc_rust::CollectionResult,
    name: &str,
) -> &'a llmcc_rust::FunctionDescriptor {
    collection
        .functions
        .iter()
        .find(|desc| desc.name == name)
        .unwrap()
}

fn find_enum<'a>(
    collection: &'a llmcc_rust::CollectionResult,
    name: &str,
) -> &'a llmcc_rust::EnumDescriptor {
    collection
        .enums
        .iter()
        .find(|desc| desc.name == name)
        .unwrap()
}

fn struct_symbol(
    unit: llmcc_core::context::CompileUnit<'static>,
    collection: &llmcc_rust::CollectionResult,
    name: &str,
) -> &'static Symbol {
    let desc = find_struct(collection, name);
    symbol(unit, desc.hir_id)
}

fn function_symbol(
    unit: llmcc_core::context::CompileUnit<'static>,
    collection: &llmcc_rust::CollectionResult,
    name: &str,
) -> &'static Symbol {
    let desc = find_function(collection, name);
    symbol(unit, desc.hir_id)
}

fn enum_symbol(
    unit: llmcc_core::context::CompileUnit<'static>,
    collection: &llmcc_rust::CollectionResult,
    name: &str,
) -> &'static Symbol {
    let desc = find_enum(collection, name);
    symbol(unit, desc.hir_id)
}

fn symbol(unit: llmcc_core::context::CompileUnit<'static>, hir_id: HirId) -> &'static Symbol {
    unit.get_scope(hir_id).symbol().unwrap()
}

fn assert_depends_on(symbol: &Symbol, target: &Symbol) {
    assert!(symbol.depends.borrow().iter().any(|id| *id == target.id));
}

fn assert_relation(dependent: &Symbol, dependency: &Symbol) {
    assert_depends_on(dependent, dependency);
}

#[test]
fn let_with_explicit_type_creates_dependency() {
    let source = r#"
        struct ProjectGraph;

        fn process_graph() {
            let pg: ProjectGraph = ProjectGraph;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let process_fn = function_symbol(unit, &collection, "process_graph");
    let pg_struct = struct_symbol(unit, &collection, "ProjectGraph");

    assert_relation(process_fn, pg_struct);
}

#[test]
fn multiple_let_declarations_with_different_types() {
    let source = r#"
        struct Config;
        struct Logger;

        fn initialize() {
            let cfg: Config = Config;
            let log: Logger = Logger;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let init_fn = function_symbol(unit, &collection, "initialize");
    let config_struct = struct_symbol(unit, &collection, "Config");
    let logger_struct = struct_symbol(unit, &collection, "Logger");

    assert_relation(init_fn, config_struct);
    assert_relation(init_fn, logger_struct);
}

#[test]
fn let_without_type_annotation_still_works() {
    let source = r#"
        fn simple() {
            let x = 5;
        }
    "#;

    let (_, unit, collection) = compile(source);
    let simple_fn = function_symbol(unit, &collection, "simple");

    // Should not panic - just proceed without type dependency
    let _deps = simple_fn.depends.borrow();
}

#[test]
fn let_with_mutable_and_type_annotation() {
    let source = r#"
        struct Buffer;

        fn allocate() {
            let mut buf: Buffer = Buffer;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let allocate_fn = function_symbol(unit, &collection, "allocate");
    let buffer_struct = struct_symbol(unit, &collection, "Buffer");

    assert_relation(allocate_fn, buffer_struct);
}

#[test]
fn nested_let_declarations_in_block() {
    let source = r#"
        struct Outer;
        struct Inner;

        fn nested() {
            {
                let outer: Outer = Outer;
                {
                    let inner: Inner = Inner;
                }
            }
        }
    "#;

    let (_, unit, collection) = compile(source);

    let nested_fn = function_symbol(unit, &collection, "nested");
    let outer_struct = struct_symbol(unit, &collection, "Outer");
    let inner_struct = struct_symbol(unit, &collection, "Inner");

    assert_relation(nested_fn, outer_struct);
    assert_relation(nested_fn, inner_struct);
}

#[test]
fn let_with_reference_type() {
    let source = r#"
        struct Data;

        fn borrow_data() {
            let data_ref: &Data = &Data;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let borrow_fn = function_symbol(unit, &collection, "borrow_data");
    let data_struct = struct_symbol(unit, &collection, "Data");

    assert_relation(borrow_fn, data_struct);
}

#[test]
fn let_in_function_parameter_scope() {
    let source = r#"
        struct State;

        fn with_param(s: State) {
            let local: State = s;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let with_param_fn = function_symbol(unit, &collection, "with_param");
    let state_struct = struct_symbol(unit, &collection, "State");

    assert_relation(with_param_fn, state_struct);
}

#[test]
fn multiple_functions_each_with_let_types() {
    let source = r#"
        struct TypeA;
        struct TypeB;

        fn func_a() {
            let a: TypeA = TypeA;
        }

        fn func_b() {
            let b: TypeB = TypeB;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let func_a = function_symbol(unit, &collection, "func_a");
    let func_b = function_symbol(unit, &collection, "func_b");
    let type_a = struct_symbol(unit, &collection, "TypeA");
    let type_b = struct_symbol(unit, &collection, "TypeB");

    assert_relation(func_a, type_a);
    assert_relation(func_b, type_b);
}

#[test]
fn let_with_enum_type() {
    let source = r#"
        enum Status {
            Active,
            Inactive,
        }

        fn check_status() {
            let s: Status = Status::Active;
        }
    "#;

    let (_, unit, collection) = compile(source);

    let check_fn = function_symbol(unit, &collection, "check_status");
    let status_enum = enum_symbol(unit, &collection, "Status");

    assert_relation(check_fn, status_enum);
}

#[test]
fn let_in_nested_scope_from_match() {
    let source = r#"
        struct Result;

        fn process() {
            match Some(0) {
                Some(_) => {
                    let res: Result = Result;
                }
                None => {}
            }
        }
    "#;

    let (_, unit, collection) = compile(source);

    let process_fn = function_symbol(unit, &collection, "process");
    let result_struct = struct_symbol(unit, &collection, "Result");

    assert_relation(process_fn, result_struct);
}

/// Tests for inferred types in let declarations (without explicit annotations)
/// When let x = Constructor() is used, dependencies should be tracked from the initializer

#[test]
fn let_without_explicit_type_struct_constructor() {
    let source = r#"
        struct Config;

        impl Config {
            fn new() -> Config {
                Config
            }
        }

        fn setup() {
            let cfg = Config::new();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let setup_fn = function_symbol(unit, &collection, "setup");
    let config_struct = struct_symbol(unit, &collection, "Config");

    // Method call creates dependency on the struct
    assert_relation(setup_fn, config_struct);
}

#[test]
fn let_without_explicit_type_method_call() {
    let source = r#"
        struct Builder;

        impl Builder {
            fn new() -> Builder {
                Builder
            }
        }

        fn create() {
            let builder = Builder::new();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let builder_struct = struct_symbol(unit, &collection, "Builder");
    let create_fn = function_symbol(unit, &collection, "create");

    // The let statement initializer `Builder::new()` creates dependency on both Builder and new()
    assert_relation(create_fn, builder_struct);
}

#[test]
fn multiple_let_without_types_from_different_sources() {
    let source = r#"
        struct Factory;

        impl Factory {
            fn new() -> Factory {
                Factory
            }
        }

        fn work() {
            let factory = Factory::new();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let work_fn = function_symbol(unit, &collection, "work");
    let factory_struct = struct_symbol(unit, &collection, "Factory");

    // Method call should track struct dependency
    assert_relation(work_fn, factory_struct);
}

#[test]
fn let_inferred_type_from_function_return() {
    let source = r#"
        struct Value;

        fn create_value() -> Value {
            Value
        }

        fn use_value() {
            let v = create_value();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let use_value_fn = function_symbol(unit, &collection, "use_value");
    let create_value_fn = function_symbol(unit, &collection, "create_value");

    // Should depend on the function called
    assert_relation(use_value_fn, create_value_fn);
}

#[test]
fn let_with_tuple_initializer() {
    let source = r#"
        struct A;
        struct B;

        impl A {
            fn new() -> A {
                A
            }
        }

        impl B {
            fn create() -> B {
                B
            }
        }

        fn combine() {
            let a = A::new();
            let b = B::create();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let combine_fn = function_symbol(unit, &collection, "combine");
    let struct_a = struct_symbol(unit, &collection, "A");
    let struct_b = struct_symbol(unit, &collection, "B");

    // Method calls should create dependencies
    assert_relation(combine_fn, struct_a);
    assert_relation(combine_fn, struct_b);
}

#[test]
fn let_with_array_of_struct() {
    let source = r#"
        struct Item;

        impl Item {
            fn default() -> Item {
                Item
            }
        }

        fn items() {
            let list = Item::default();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let items_fn = function_symbol(unit, &collection, "items");
    let item_struct = struct_symbol(unit, &collection, "Item");

    // Method call should create dependencies
    assert_relation(items_fn, item_struct);
}

#[test]
fn let_with_method_chain() {
    let source = r#"
        struct Builder;

        impl Builder {
            fn new() -> Builder {
                Builder
            }

            fn configure(self) -> Builder {
                self
            }
        }

        fn setup() {
            let configured = Builder::new().configure();
        }
    "#;

    let (_, unit, collection) = compile(source);

    let setup_fn = function_symbol(unit, &collection, "setup");
    let builder_struct = struct_symbol(unit, &collection, "Builder");

    // Should depend on Builder from the chain
    assert_relation(setup_fn, builder_struct);
}