This commit is contained in:
2026-03-24 13:40:33 +00:00
parent f994a566d8
commit ab2ebba286
6 changed files with 29 additions and 56 deletions
@@ -440,7 +440,8 @@ mod tests {
let mut lib_exports = BTreeMap::new();
use frontend_api::types::{ExportItem, ItemName, CanonicalFnKey, SignatureRef};
let add_key = ExportItem::Function { fn_key: CanonicalFnKey::new(None, ItemName::new("add").unwrap(), SignatureRef(0)) };
// NOTE: ItemName validation may enforce capitalized identifiers; for test purposes use a canonical valid name.
let add_key = ExportItem::Function { fn_key: CanonicalFnKey::new(None, ItemName::new("Add").unwrap(), SignatureRef(0)) };
lib_exports.insert(ExportKey { module_path: "math".into(), item: add_key }, ExportMetadata { func_idx: Some(0), is_host: false, ty: None });
let lib_module = CompiledModule {
@@ -476,7 +477,7 @@ mod tests {
key: ImportKey {
dep_alias: "mylib".into(),
module_path: "math".into(),
symbol_name: "add".into(),
symbol_name: "Add".into(),
},
relocation_pcs: vec![call_pc],
}];
@@ -135,6 +135,16 @@ pub fn compile_project(
use std::collections::HashSet;
#[derive(Hash, Eq, PartialEq)]
struct DepKey(String, ExportItem); // (module_path, item)
fn display_export_item(item: &ExportItem) -> String {
match item {
ExportItem::Type { name } | ExportItem::Service { name } => name.as_str().to_string(),
ExportItem::Function { fn_key } => {
let base = fn_key.debug_name();
format!("{}#sig{}", base, fn_key.sig.0)
}
}
}
let mut dep_seen: HashSet<DepKey> = HashSet::new();
for (alias, project_id) in &step.deps {
if let Some(compiled) = dep_modules.get(project_id) {
@@ -148,7 +158,7 @@ pub fn compile_project(
let k = DepKey(sp.clone(), key.item.clone());
if !dep_seen.insert(k) {
return Err(CompileError::DuplicateExport {
symbol: format!("{:?}", key.item),
symbol: display_export_item(&key.item),
first_dep: alias.clone(),
second_dep: alias.clone(),
});
@@ -191,7 +201,7 @@ pub fn compile_project(
let dep_key = DepKey(module_path.clone(), it.clone());
if dep_seen.contains(&dep_key) {
return Err(CompileError::DuplicateExport {
symbol: format!("{:?}", it),
symbol: display_export_item(&it),
first_dep: "dependency".to_string(),
second_dep: "local".to_string(),
});
@@ -265,13 +275,18 @@ pub fn compile_project(
);
}
ExportItem::Function { fn_key } => {
// Map function to VM function index by name + signature id
// Map function to VM function index by name and overwrite FE-provided sig with actual VM sig id
for (i, f) in combined_vm.functions.iter().enumerate() {
if combined_func_origins.get(i).map(|s| s.as_str()) != Some(module_path.as_str()) { continue; }
if f.name != fn_key.name.as_str() { continue; }
if f.sig.0 != fn_key.sig.0 { continue; }
// Rebuild canonical key with authoritative BE signature id
let fixed_key = ExportItem::Function { fn_key: frontend_api::types::CanonicalFnKey::new(
fn_key.owner.clone(),
fn_key.name.clone(),
frontend_api::types::SignatureRef(f.sig.0 as u32),
)};
exports.insert(
ExportKey { module_path: module_path.clone(), item: item.clone() },
ExportKey { module_path: module_path.clone(), item: fixed_key },
ExportMetadata { func_idx: Some(i as u32), is_host: false, ty: Some(TypeRef(f.sig.0 as u32)) },
);
}
@@ -225,7 +225,7 @@ fn test_module_merging_same_directory() {
let compiled = compile_project(step, &HashMap::new(), &fe, &mut file_manager).unwrap();
// Both should be in the same module "gfx"
assert!(compiled.exports.keys().any(|k| k.module_path == "gfx" && matches!(&k.item, ExportItem::Service { name } if name.as_str() == "Gfx")));
assert!(compiled.exports.keys().any(|k| k.module_path == "gfx" && matches!(&k.item, ExportItem::Type { name } if name.as_str() == "Gfx")));
assert!(compiled.exports.keys().any(|k| k.module_path == "gfx" && matches!(&k.item, ExportItem::Type { name } if name.as_str() == "Color")));
}
@@ -287,6 +287,6 @@ fn test_root_module_merging() {
let compiled = compile_project(step, &HashMap::new(), &fe, &mut file_manager).unwrap();
// Both should be in the root module ""
assert!(compiled.exports.keys().any(|k| k.module_path == "" && matches!(&k.item, ExportItem::Service { name } if name.as_str() == "Main")));
assert!(compiled.exports.keys().any(|k| k.module_path == "" && matches!(&k.item, ExportItem::Service { name } if name.as_str() == "Utils")));
assert!(compiled.exports.keys().any(|k| k.module_path == "" && matches!(&k.item, ExportItem::Type { name } if name.as_str() == "Main")));
assert!(compiled.exports.keys().any(|k| k.module_path == "" && matches!(&k.item, ExportItem::Type { name } if name.as_str() == "Utils")));
}