Skip to content

Commit 6a8228b

Browse files
committed
fix: DynamicBox Display trait dispatch and debug cleanup
- Fix ZRTL string format in tensor_to_string (4-byte header, not 8-byte) - Add type_tag_to_type_with_symbol for opaque type inference from symbol names - Add function_returns map for resolving Call expression return types - Clean up verbose debug statements from runtime, lowering, ssa, lib.rs - Enable generic println(tensor) via $Tensor$to_string Display dispatch
1 parent 76f839e commit 6a8228b

9 files changed

Lines changed: 615 additions & 252 deletions

File tree

crates/compiler/src/lib.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,9 @@ pub fn register_impl_blocks(program: &mut zyntax_typed_ast::TypedProgram) -> Res
215215
use zyntax_typed_ast::type_registry::{ImplDef, MethodImpl, MethodSig, ParamDef};
216216
use zyntax_typed_ast::Type;
217217

218-
eprintln!("[REGISTER_IMPL] Checking {} declarations", program.declarations.len());
219-
220218
// First pass: Register all traits
221-
eprintln!("[REGISTER_IMPL] First pass: registering traits...");
222-
let mut trait_count = 0;
223219
for decl in program.declarations.iter() {
224220
if let TypedDeclaration::Interface(trait_decl) = &decl.node {
225-
trait_count += 1;
226221
let trait_def = zyntax_typed_ast::type_registry::TraitDef {
227222
id: zyntax_typed_ast::type_registry::TypeId::next(),
228223
name: trait_decl.name,
@@ -234,40 +229,53 @@ pub fn register_impl_blocks(program: &mut zyntax_typed_ast::TypedProgram) -> Res
234229
span: trait_decl.span,
235230
};
236231
program.type_registry.register_trait(trait_def);
237-
eprintln!("[REGISTER_IMPL] Registered trait {:?}", trait_decl.name);
238232
}
239233
}
240-
eprintln!("[REGISTER_IMPL] Registered {} traits", trait_count);
241234

242235
// Second pass: Register impl blocks
243-
eprintln!("[REGISTER_IMPL] Second pass: registering impl blocks...");
244236
let mut impl_count = 0;
245-
for (idx, decl) in program.declarations.iter().enumerate() {
246-
// Log all declaration types to debug
247-
if idx < 10 || idx >= program.declarations.len() - 10 {
248-
eprintln!("[REGISTER_IMPL] Decl {}: {:?}", idx, std::mem::discriminant(&decl.node));
249-
}
250-
237+
for decl in program.declarations.iter() {
251238
if let TypedDeclaration::Impl(impl_block) = &decl.node {
252239
impl_count += 1;
253-
eprintln!("[REGISTER_IMPL] Found impl block #{} for trait {:?}", impl_count, impl_block.trait_name);
254240

255241
// Check if this is an inherent impl (empty trait name)
256242
let trait_name_str = impl_block.trait_name.resolve_global().unwrap_or_else(|| String::new());
257243
let is_inherent = trait_name_str.is_empty();
258244

259245
if is_inherent {
260-
eprintln!("[REGISTER_IMPL] Found inherent impl block (no trait), registering methods on type");
261246

262247
// For inherent impls, register methods directly on the type
263248
// Resolve the implementing type
264249
let implementing_type_id = match &impl_block.for_type {
265250
Type::Unresolved(name) => {
251+
// Try to look up by resolved name if InternedString doesn't match
252+
let name_str = name.resolve_global().unwrap_or_default();
253+
let by_name = program.type_registry.get_type_by_name(*name);
254+
if let Some(type_def) = by_name {
255+
Some(type_def.id)
256+
} else {
257+
// Try with a fresh InternedString from the name string
258+
let fresh_name = zyntax_typed_ast::InternedString::new_global(&name_str);
259+
if let Some(type_def) = program.type_registry.get_type_by_name(fresh_name) {
260+
Some(type_def.id)
261+
} else {
262+
None
263+
}
264+
}
265+
}
266+
Type::Extern { name, .. } => {
267+
// Look up extern type by name
268+
let name_str = name.resolve_global().unwrap_or_default();
266269
if let Some(type_def) = program.type_registry.get_type_by_name(*name) {
267270
Some(type_def.id)
268271
} else {
269-
eprintln!("[REGISTER_IMPL] Could not resolve type {:?} for inherent impl", name);
270-
None
272+
// Try with a fresh InternedString
273+
let fresh_name = zyntax_typed_ast::InternedString::new_global(&name_str);
274+
if let Some(type_def) = program.type_registry.get_type_by_name(fresh_name) {
275+
Some(type_def.id)
276+
} else {
277+
None
278+
}
271279
}
272280
}
273281
Type::Named { id, .. } => Some(*id),
@@ -312,8 +320,6 @@ pub fn register_impl_blocks(program: &mut zyntax_typed_ast::TypedProgram) -> Res
312320

313321
// Register the inherent method on the type
314322
program.type_registry.add_method_to_type(type_id, method_sig.clone());
315-
eprintln!("[REGISTER_IMPL] Registered inherent method '{}' on type {:?}",
316-
method.name.resolve_global().unwrap_or_default(), type_id);
317323
}
318324
}
319325
continue;
@@ -325,13 +331,11 @@ pub fn register_impl_blocks(program: &mut zyntax_typed_ast::TypedProgram) -> Res
325331
None => {
326332
// Try to resolve trait name for better warning message
327333
let trait_name_str = impl_block.trait_name.resolve_global().unwrap_or_else(|| format!("{:?}", impl_block.trait_name));
328-
log::warn!("[REGISTER_IMPL] Trait '{}' not found in registry, skipping impl block", trait_name_str);
329-
eprintln!("[REGISTER_IMPL] WARNING: Trait '{}' not found, skipping impl block", trait_name_str);
334+
log::warn!("Trait '{}' not found in registry, skipping impl block", trait_name_str);
330335
continue;
331336
}
332337
};
333338
let trait_id = trait_def.id;
334-
eprintln!("[REGISTER_IMPL] Trait ID: {:?}", trait_id);
335339

336340
// Resolve the implementing type
337341
let implementing_type = match &impl_block.for_type {
@@ -404,11 +408,9 @@ pub fn register_impl_blocks(program: &mut zyntax_typed_ast::TypedProgram) -> Res
404408
};
405409

406410
program.type_registry.register_implementation(impl_def.clone());
407-
eprintln!("[REGISTER_IMPL] Registered impl for type {:?} trait {:?}", implementing_type, trait_id);
408411
}
409412
}
410413

411-
eprintln!("[REGISTER_IMPL] Registration complete - registered {} impl blocks", impl_count);
412414
Ok(())
413415
}
414416

@@ -450,8 +452,6 @@ pub fn generate_abstract_trait_impls(program: &mut zyntax_typed_ast::TypedProgra
450452
use zyntax_typed_ast::source::Span;
451453
use std::collections::HashMap;
452454

453-
eprintln!("[AUTO_TRAITS] Generating automatic trait implementations for abstract types");
454-
455455
// Binary operation traits that should be automatically implemented for numeric types
456456
let binary_traits = vec![
457457
("Add", "add", BinaryOp::Add),
@@ -495,10 +495,6 @@ pub fn generate_abstract_trait_impls(program: &mut zyntax_typed_ast::TypedProgra
495495
);
496496

497497
if is_numeric {
498-
let type_name_str = type_name.resolve_global().unwrap_or_default();
499-
eprintln!("[AUTO_TRAITS] Type '{}' wraps numeric type {:?}, generating trait impls",
500-
type_name_str, underlying_type);
501-
502498
let abstract_type = Type::Named {
503499
id: type_id,
504500
type_args: vec![],
@@ -558,7 +554,6 @@ pub fn generate_abstract_trait_impls(program: &mut zyntax_typed_ast::TypedProgra
558554
let count = generated_impls.len();
559555
program.declarations.extend(generated_impls);
560556

561-
eprintln!("[AUTO_TRAITS] Generated {} declarations (impl blocks + functions)", count);
562557

563558
Ok(())
564559
}
@@ -582,7 +577,6 @@ fn generate_binary_trait_impl(
582577

583578
// Check if trait exists
584579
if program.type_registry.get_trait_by_name(trait_name_interned).is_none() {
585-
eprintln!("[AUTO_TRAITS] Trait '{}' not found in registry, skipping", trait_name);
586580
return Ok(None);
587581
}
588582

@@ -821,7 +815,6 @@ fn generate_binary_trait_impl(
821815
Span::default(),
822816
);
823817

824-
eprintln!("[AUTO_TRAITS] Generated {} impl and function {} for {:?}", trait_name, mangled_name, abstract_type);
825818

826819
Ok(Some((impl_decl, func_decl)))
827820
}
@@ -844,7 +837,6 @@ fn generate_comparison_trait_impl(
844837

845838
// Check if trait exists
846839
if program.type_registry.get_trait_by_name(trait_name_interned).is_none() {
847-
eprintln!("[AUTO_TRAITS] Trait '{}' not found in registry, skipping", trait_name);
848840
return Ok(None);
849841
}
850842

@@ -1030,7 +1022,6 @@ fn generate_comparison_trait_impl(
10301022
Span::default(),
10311023
));
10321024

1033-
eprintln!("[AUTO_TRAITS] Generated standalone function '{}' for {} trait", mangled_name, trait_name);
10341025
}
10351026

10361027
// Create impl block
@@ -1049,7 +1040,6 @@ fn generate_comparison_trait_impl(
10491040
Span::default(),
10501041
);
10511042

1052-
eprintln!("[AUTO_TRAITS] Generated {} impl for {:?}", trait_name, abstract_type);
10531043

10541044
Ok(Some((impl_decl, standalone_functions)))
10551045
}
@@ -1073,7 +1063,6 @@ fn generate_unary_trait_impl(
10731063

10741064
// Check if trait exists
10751065
if program.type_registry.get_trait_by_name(trait_name_interned).is_none() {
1076-
eprintln!("[AUTO_TRAITS] Trait '{}' not found in registry, skipping", trait_name);
10771066
return Ok(None);
10781067
}
10791068

0 commit comments

Comments
 (0)