Skip to content

Commit ac7d76d

Browse files
committed
feat: implement AST mutation to apply inferred types from type resolver
This implements the proper architectural solution for Type::Any resolution by applying resolved types from the InferenceContext back to the TypedAST after type checking completes. This ensures all language implementations benefit from type inference, not just ZynPEG. Changes: - Add apply_inferred_types() method to TypeChecker that walks TypedProgram and replaces Type::Any with resolved types from inference substitutions - Update lowering pipeline to call apply_inferred_types() after type checking - Change lower_program() signatures throughout to accept &mut TypedProgram - Update compile_to_hir() and compile_to_jit() public APIs to use mutable refs - Remove SKIP_TYPE_CHECK bypass from zyntax_embed runtime to enable type resolution - Fix all test files to pass mutable program references Architecture: Parser → TypedAST (Type::Any) → Type Checker → Inference solves constraints → apply_inferred_types() mutates AST → Lowering (fully resolved types)
1 parent 90fff8d commit ac7d76d

12 files changed

Lines changed: 206 additions & 61 deletions

crates/compiler/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl Default for CompilationConfig {
173173
/// 4. Run optimization passes
174174
/// 5. Return optimized HIR ready for backend code generation
175175
pub fn compile_to_hir(
176-
program: &TypedProgram,
176+
program: &mut TypedProgram,
177177
type_registry: Arc<zyntax_typed_ast::TypeRegistry>,
178178
config: CompilationConfig,
179179
) -> CompilerResult<HirModule> {
@@ -300,7 +300,7 @@ pub fn compile_to_hir(
300300
/// 3. Returns a Cranelift backend ready to execute functions
301301
#[cfg(feature = "cranelift")]
302302
pub fn compile_to_jit(
303-
program: &TypedProgram,
303+
program: &mut TypedProgram,
304304
type_registry: Arc<zyntax_typed_ast::TypeRegistry>,
305305
config: CompilationConfig,
306306
) -> CompilerResult<cranelift_backend::CraneliftBackend> {

crates/compiler/src/lowering.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ pub enum DiagnosticLevel {
328328
/// Main AST lowering interface
329329
pub trait AstLowering {
330330
/// Lower a typed program to HIR
331-
fn lower_program(&mut self, program: &TypedProgram) -> CompilerResult<HirModule>;
331+
fn lower_program(&mut self, program: &mut TypedProgram) -> CompilerResult<HirModule>;
332332
}
333333

334334
/// Lowering pipeline implementation
@@ -382,7 +382,7 @@ impl LoweringContext {
382382
}
383383

384384
impl AstLowering for LoweringContext {
385-
fn lower_program(&mut self, program: &TypedProgram) -> CompilerResult<HirModule> {
385+
fn lower_program(&mut self, program: &mut TypedProgram) -> CompilerResult<HirModule> {
386386
// Phase 0: Run type checking and inference (Issue 0 Phase 1)
387387
// This validates types and performs type inference, reporting any errors
388388
// Skip type checking if SKIP_TYPE_CHECK env var is set (for debugging)
@@ -416,7 +416,7 @@ impl AstLowering for LoweringContext {
416416
impl LoweringContext {
417417
/// Run type checking and inference on the program
418418
/// This is Issue 0 Phase 1: integrate type inference into lowering
419-
fn run_type_checking(&mut self, program: &TypedProgram) -> CompilerResult<()> {
419+
fn run_type_checking(&mut self, program: &mut TypedProgram) -> CompilerResult<()> {
420420
use zyntax_typed_ast::type_checker::{TypeChecker, TypeCheckOptions};
421421

422422
// Create type checker - needs Box<TypeRegistry>
@@ -431,6 +431,10 @@ impl LoweringContext {
431431
// Run type checking (validates and performs internal inference)
432432
type_checker.check_program(program);
433433

434+
// Apply resolved types from inference to the AST
435+
// This propagates Type::Any resolutions back to the AST nodes
436+
type_checker.apply_inferred_types(program);
437+
434438
// Check for type errors and display diagnostics
435439
let diagnostics_collector = type_checker.diagnostics();
436440
let error_count = diagnostics_collector.error_count();

crates/compiler/tests/compilation_pipeline_tests.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn test_compilation_pipeline_simple_function() {
7878
span: test_span(),
7979
};
8080

81-
let program = create_test_program(&mut arena, "return_42", body);
81+
let mut program = create_test_program(&mut arena, "return_42", body);
8282

8383
// Run compilation pipeline
8484
let config = CompilationConfig {
@@ -88,7 +88,7 @@ fn test_compilation_pipeline_simple_function() {
8888
..Default::default()
8989
};
9090

91-
let result = compile_to_hir(&program, type_registry, config);
91+
let result = compile_to_hir(&mut program, type_registry, config);
9292
assert!(result.is_ok(), "Compilation should succeed: {:?}", result.err());
9393

9494
let hir_module = result.unwrap();
@@ -153,7 +153,7 @@ fn test_compilation_pipeline_binary_operation() {
153153
span: test_span(),
154154
};
155155

156-
let program = create_test_program(&mut arena, "add_numbers", body);
156+
let mut program = create_test_program(&mut arena, "add_numbers", body);
157157

158158
// Run compilation pipeline
159159
let config = CompilationConfig {
@@ -163,7 +163,7 @@ fn test_compilation_pipeline_binary_operation() {
163163
..Default::default()
164164
};
165165

166-
let result = compile_to_hir(&program, type_registry, config);
166+
let result = compile_to_hir(&mut program, type_registry, config);
167167
assert!(result.is_ok(), "Compilation should succeed: {:?}", result.err());
168168

169169
let hir_module = result.unwrap();
@@ -328,7 +328,7 @@ fn test_compilation_pipeline_with_analysis() {
328328
span: test_span(),
329329
};
330330

331-
let program = create_test_program(&mut arena, "test_analysis", body);
331+
let mut program = create_test_program(&mut arena, "test_analysis", body);
332332

333333
// Run compilation with all passes enabled
334334
let config = CompilationConfig {
@@ -338,7 +338,7 @@ fn test_compilation_pipeline_with_analysis() {
338338
..Default::default()
339339
};
340340

341-
let result = compile_to_hir(&program, type_registry, config);
341+
let result = compile_to_hir(&mut program, type_registry, config);
342342
assert!(result.is_ok(), "Compilation with analysis should succeed");
343343

344344
let hir_module = result.unwrap();
@@ -370,7 +370,7 @@ fn test_compilation_pipeline_with_memory_management() {
370370
span: test_span(),
371371
};
372372

373-
let program = create_test_program(&mut arena, "test_memory", body);
373+
let mut program = create_test_program(&mut arena, "test_memory", body);
374374

375375
// Run compilation with ARC memory strategy
376376
let config = CompilationConfig {
@@ -381,7 +381,7 @@ fn test_compilation_pipeline_with_memory_management() {
381381
..Default::default()
382382
};
383383

384-
let result = compile_to_hir(&program, type_registry, config);
384+
let result = compile_to_hir(&mut program, type_registry, config);
385385
assert!(result.is_ok(), "Compilation with ARC should succeed");
386386

387387
let hir_module = result.unwrap();
@@ -411,7 +411,7 @@ fn test_compilation_pipeline_without_memory_management() {
411411
span: test_span(),
412412
};
413413

414-
let program = create_test_program(&mut arena, "test_no_memory", body);
414+
let mut program = create_test_program(&mut arena, "test_no_memory", body);
415415

416416
// Run compilation with memory management disabled
417417
let config = CompilationConfig {
@@ -422,7 +422,7 @@ fn test_compilation_pipeline_without_memory_management() {
422422
..Default::default()
423423
};
424424

425-
let result = compile_to_hir(&program, type_registry, config);
425+
let result = compile_to_hir(&mut program, type_registry, config);
426426
assert!(result.is_ok(), "Compilation without memory management should succeed");
427427

428428
let hir_module = result.unwrap();
@@ -452,7 +452,7 @@ fn test_compilation_pipeline_with_gc_strategy() {
452452
span: test_span(),
453453
};
454454

455-
let program = create_test_program(&mut arena, "test_gc", body);
455+
let mut program = create_test_program(&mut arena, "test_gc", body);
456456

457457
// Run compilation with GC memory strategy
458458
let config = CompilationConfig {
@@ -463,7 +463,7 @@ fn test_compilation_pipeline_with_gc_strategy() {
463463
..Default::default()
464464
};
465465

466-
let result = compile_to_hir(&program, type_registry, config);
466+
let result = compile_to_hir(&mut program, type_registry, config);
467467
assert!(result.is_ok(), "Compilation with GC should succeed");
468468

469469
let hir_module = result.unwrap();
@@ -509,7 +509,7 @@ fn test_compilation_pipeline_all_features() {
509509
span: test_span(),
510510
};
511511

512-
let program = create_test_program(&mut arena, "test_all_features", body);
512+
let mut program = create_test_program(&mut arena, "test_all_features", body);
513513

514514
// Run compilation with ALL features enabled
515515
let config = CompilationConfig {
@@ -523,7 +523,7 @@ fn test_compilation_pipeline_all_features() {
523523
import_resolver: None,
524524
};
525525

526-
let result = compile_to_hir(&program, type_registry, config);
526+
let result = compile_to_hir(&mut program, type_registry, config);
527527
assert!(result.is_ok(), "Compilation with all features should succeed");
528528

529529
let hir_module = result.unwrap();
@@ -576,7 +576,7 @@ fn test_compilation_pipeline_with_memory_optimizations() {
576576
span: test_span(),
577577
};
578578

579-
let program = create_test_program(&mut arena, "test_opt", body);
579+
let mut program = create_test_program(&mut arena, "test_opt", body);
580580

581581
// Run compilation with full optimization including memory optimizations
582582
let config = CompilationConfig {
@@ -590,7 +590,7 @@ fn test_compilation_pipeline_with_memory_optimizations() {
590590
import_resolver: None,
591591
};
592592

593-
let result = compile_to_hir(&program, type_registry, config);
593+
let result = compile_to_hir(&mut program, type_registry, config);
594594
assert!(result.is_ok(), "Compilation with memory optimizations should succeed");
595595

596596
let hir_module = result.unwrap();
@@ -658,7 +658,7 @@ fn test_compilation_pipeline_with_async_function() -> CompilerResult<()> {
658658
..Default::default()
659659
};
660660

661-
let hir_module = compile_to_hir(&program, type_registry, config)?;
661+
let hir_module = compile_to_hir(&mut program, type_registry, config)?;
662662

663663
// Verify module was created
664664
// Async functions generate additional state machine functions, so we expect >= 1
@@ -728,7 +728,7 @@ fn test_compilation_pipeline_without_async_runtime() -> CompilerResult<()> {
728728
..Default::default()
729729
};
730730

731-
let hir_module = compile_to_hir(&program, type_registry, config)?;
731+
let hir_module = compile_to_hir(&mut program, type_registry, config)?;
732732

733733
// Verify module was created
734734
assert!(hir_module.functions.len() >= 1, "Should have at least one function");
@@ -835,7 +835,7 @@ fn test_compilation_pipeline_mixed_sync_async() -> CompilerResult<()> {
835835
..Default::default()
836836
};
837837

838-
let hir_module = compile_to_hir(&program, type_registry, config)?;
838+
let hir_module = compile_to_hir(&mut program, type_registry, config)?;
839839

840840
// Verify both functions were created
841841
// Async functions may generate additional state machine functions
@@ -879,7 +879,7 @@ fn test_compilation_pipeline_while_loop() {
879879
span: test_span(),
880880
};
881881

882-
let program = create_test_program(&mut arena, "test_while", body);
882+
let mut program = create_test_program(&mut arena, "test_while", body);
883883

884884
let config = CompilationConfig {
885885
opt_level: 0,
@@ -888,7 +888,7 @@ fn test_compilation_pipeline_while_loop() {
888888
..Default::default()
889889
};
890890

891-
let result = compile_to_hir(&program, type_registry, config);
891+
let result = compile_to_hir(&mut program, type_registry, config);
892892
assert!(result.is_ok(), "While loop compilation should succeed: {:?}", result.err());
893893

894894
let hir_module = result.unwrap();
@@ -937,7 +937,7 @@ fn test_compilation_pipeline_infinite_loop_with_break() {
937937
span: test_span(),
938938
};
939939

940-
let program = create_test_program(&mut arena, "test_loop_break", body);
940+
let mut program = create_test_program(&mut arena, "test_loop_break", body);
941941

942942
let config = CompilationConfig {
943943
opt_level: 0,
@@ -946,7 +946,7 @@ fn test_compilation_pipeline_infinite_loop_with_break() {
946946
..Default::default()
947947
};
948948

949-
let result = compile_to_hir(&program, type_registry, config);
949+
let result = compile_to_hir(&mut program, type_registry, config);
950950
assert!(result.is_ok(), "Infinite loop with break should compile: {:?}", result.err());
951951

952952
let hir_module = result.unwrap();
@@ -978,7 +978,7 @@ fn test_compilation_pipeline_nested_blocks() {
978978
span: test_span(),
979979
};
980980

981-
let program = create_test_program(&mut arena, "test_nested_blocks", outer_block);
981+
let mut program = create_test_program(&mut arena, "test_nested_blocks", outer_block);
982982

983983
let config = CompilationConfig {
984984
opt_level: 0,
@@ -987,7 +987,7 @@ fn test_compilation_pipeline_nested_blocks() {
987987
..Default::default()
988988
};
989989

990-
let result = compile_to_hir(&program, type_registry, config);
990+
let result = compile_to_hir(&mut program, type_registry, config);
991991
assert!(result.is_ok(), "Nested blocks should compile: {:?}", result.err());
992992

993993
let hir_module = result.unwrap();

crates/compiler/tests/expression_lowering_tests.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn test_literal_lowering() {
107107
config,
108108
);
109109

110-
let result = ctx.lower_program(&program);
110+
let result = ctx.lower_program(&mut program);
111111
assert!(result.is_ok(), "Failed to lower literal expression: {:?}", result.err());
112112

113113
let module = result.unwrap();
@@ -166,7 +166,7 @@ fn test_binary_operation_lowering() {
166166
config,
167167
);
168168

169-
let result = ctx.lower_program(&program);
169+
let result = ctx.lower_program(&mut program);
170170
assert!(result.is_ok(), "Failed to lower binary operation: {:?}", result.err());
171171

172172
let module = result.unwrap();
@@ -232,7 +232,7 @@ fn test_unary_operation_lowering() {
232232
config,
233233
);
234234

235-
let result = ctx.lower_program(&program);
235+
let result = ctx.lower_program(&mut program);
236236
assert!(result.is_ok(), "Failed to lower unary operation: {:?}", result.err());
237237

238238
let module = result.unwrap();
@@ -311,7 +311,7 @@ fn test_if_expression_lowering() {
311311
config,
312312
);
313313

314-
let result = ctx.lower_program(&program);
314+
let result = ctx.lower_program(&mut program);
315315
assert!(result.is_ok(), "Failed to lower if expression: {:?}", result.err());
316316

317317
let module = result.unwrap();
@@ -386,7 +386,7 @@ fn test_variable_reference_lowering() {
386386
config,
387387
);
388388

389-
let result = ctx.lower_program(&program);
389+
let result = ctx.lower_program(&mut program);
390390
assert!(result.is_ok(), "Failed to lower variable reference: {:?}", result.err());
391391
}
392392

@@ -448,7 +448,7 @@ fn test_tuple_construction_lowering() {
448448
config,
449449
);
450450

451-
let result = ctx.lower_program(&program);
451+
let result = ctx.lower_program(&mut program);
452452
assert!(result.is_ok(), "Failed to lower tuple construction: {:?}", result.err());
453453

454454
let module = result.unwrap();
@@ -529,7 +529,7 @@ fn test_array_construction_lowering() {
529529
config,
530530
);
531531

532-
let result = ctx.lower_program(&program);
532+
let result = ctx.lower_program(&mut program);
533533
assert!(result.is_ok(), "Failed to lower array construction: {:?}", result.err());
534534
}
535535

@@ -601,7 +601,7 @@ fn test_complex_expression_lowering() {
601601
config,
602602
);
603603

604-
let result = ctx.lower_program(&program);
604+
let result = ctx.lower_program(&mut program);
605605
assert!(result.is_ok(), "Failed to lower complex expression: {:?}", result.err());
606606

607607
let module = result.unwrap();
@@ -827,7 +827,7 @@ fn test_assignment_lowering() {
827827
config,
828828
);
829829

830-
let result = ctx.lower_program(&program);
830+
let result = ctx.lower_program(&mut program);
831831
assert!(result.is_ok(), "Failed to lower assignment expression: {:?}", result.err());
832832

833833
let module = result.unwrap();
@@ -960,7 +960,7 @@ fn test_pattern_matching_lowering() {
960960
config,
961961
);
962962

963-
let result = ctx.lower_program(&program);
963+
let result = ctx.lower_program(&mut program);
964964
assert!(result.is_ok(), "Failed to lower match expression: {:?}", result.err());
965965

966966
let module = result.unwrap();

0 commit comments

Comments
 (0)