Skip to content

Commit

Permalink
Make sure to drop suffix from symbols exposed to the host
Browse files Browse the repository at this point in the history
  • Loading branch information
agu-z committed Oct 24, 2024
1 parent 0455c8d commit 9039954
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
6 changes: 3 additions & 3 deletions crates/compiler/gen_llvm/src/llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4324,7 +4324,7 @@ fn expose_function_to_host<'a, 'ctx>(
return_layout: InLayout<'a>,
layout_ids: &mut LayoutIds<'a>,
) {
let ident_string = symbol.as_str(&env.interns);
let ident_string = symbol.as_unsuffixed_str(&env.interns);

let proc_layout = ProcLayout {
arguments,
Expand Down Expand Up @@ -5564,7 +5564,7 @@ pub fn build_procedures<'a>(
let getter_fn = function_value_by_func_spec(env, FuncBorrowSpec::Some(*func_spec), symbol);

let name = getter_fn.get_name().to_str().unwrap();
let getter_name = symbol.as_str(&env.interns);
let getter_name = symbol.as_unsuffixed_str(&env.interns);

// Add the getter function to the module.
let _ = expose_function_to_host_help_c_abi(
Expand Down Expand Up @@ -5830,7 +5830,7 @@ fn build_procedures_help<'a>(
GenTest | WasmGenTest | CliTest => { /* no host, or exposing types is not supported */ }
Binary | BinaryDev | BinaryGlue => {
for (proc_name, alias_name, hels) in host_exposed_lambda_sets.iter() {
let ident_string = proc_name.name().as_str(&env.interns);
let ident_string = proc_name.name().as_unsuffixed_str(&env.interns);
let fn_name: String = format!("{}_{}", ident_string, hels.id.0);

expose_alias_to_host(
Expand Down
10 changes: 10 additions & 0 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ impl Symbol {
})
}

pub fn as_unsuffixed_str(self, interns: &Interns) -> &str {
match self.suffix() {
IdentSuffix::None => self.as_str(interns),
IdentSuffix::Bang => {
let str = self.as_str(interns);
&str[..str.len() - 1]
}
}
}

pub const fn as_u64(self) -> u64 {
u64::from_ne_bytes(self.to_ne_bytes())
}
Expand Down
6 changes: 3 additions & 3 deletions crates/compiler/mono/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4649,20 +4649,20 @@ impl LayoutId {
// Returns something like "#UserApp_foo_1" when given a symbol that interns to "foo"
// and a LayoutId of 1.
pub fn to_symbol_string(self, symbol: Symbol, interns: &Interns) -> String {
let ident_string = symbol.as_str(interns).trim_end_matches('!');
let ident_string = symbol.as_unsuffixed_str(interns);
let module_string = interns.module_ids.get_name(symbol.module_id()).unwrap();
format!("{}_{}_{}", module_string, ident_string, self.0)
}

// Returns something like "roc__foo_1_exposed" when given a symbol that interns to "foo"
// and a LayoutId of 1.
pub fn to_exposed_symbol_string(self, symbol: Symbol, interns: &Interns) -> String {
let ident_string = symbol.as_str(interns).trim_end_matches('!');
let ident_string = symbol.as_unsuffixed_str(interns);
format!("roc__{}_{}_exposed", ident_string, self.0)
}

pub fn to_exposed_generic_symbol_string(self, symbol: Symbol, interns: &Interns) -> String {
let ident_string = symbol.as_str(interns).trim_end_matches('!');
let ident_string = symbol.as_unsuffixed_str(interns);
format!("roc__{}_{}_exposed_generic", ident_string, self.0)
}
}
Expand Down
21 changes: 12 additions & 9 deletions crates/linker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub fn generate_stub_lib(
.exposed_to_host
.top_level_values
.keys()
.map(|x| x.as_str(&loaded.interns).to_string())
.map(|x| x.as_unsuffixed_str(&loaded.interns).to_string())
.collect();

let exported_closure_types = loaded
Expand All @@ -108,7 +108,7 @@ pub fn generate_stub_lib(
format!(
"{}_{}",
x.module_string(&loaded.interns),
x.as_str(&loaded.interns)
x.as_unsuffixed_str(&loaded.interns)
)
})
.collect();
Expand Down Expand Up @@ -162,18 +162,21 @@ impl ExposedSymbols {
let mut custom_names = Vec::new();

for x in exposed_to_host.top_level_values.keys() {
let sym = x.as_str(interns);
let sym = x.as_unsuffixed_str(interns);

custom_names.extend([
format!("roc__{sym}_1_exposed"),
format!("roc__{sym}_1_exposed_generic"),
format!("roc__{sym}_1_exposed_size"),
]);

let exported_closure_types = exposed_to_host
.closure_types
.iter()
.map(|x| format!("{}_{}", x.module_string(interns), x.as_str(interns)));
let exported_closure_types = exposed_to_host.closure_types.iter().map(|x| {
format!(
"{}_{}",
x.module_string(interns),
x.as_unsuffixed_str(interns)
)
});

for (i, _) in exported_closure_types.enumerate() {
custom_names.extend([
Expand All @@ -185,7 +188,7 @@ impl ExposedSymbols {
}

for x in &exposed_to_host.getters {
let sym = x.as_str(interns);
let sym = x.as_unsuffixed_str(interns);
custom_names.extend([
sym.to_string(),
format!("{sym}_generic"),
Expand All @@ -194,7 +197,7 @@ impl ExposedSymbols {
}

for (top_level_value, lambda_set_id) in &exposed_to_host.lambda_sets {
let sym = top_level_value.as_str(interns);
let sym = top_level_value.as_unsuffixed_str(interns);
let id = lambda_set_id.0;
custom_names.extend([format!("roc__{sym}_{id}_caller")]);
}
Expand Down

0 comments on commit 9039954

Please sign in to comment.