@@ -779,17 +779,25 @@ impl CodeGenerator for Var {
779
779
}
780
780
}
781
781
} else {
782
- // If necessary, apply a `#[link_name]` attribute
783
- if let Some ( link_name) = self . link_name ( ) {
784
- attrs. push ( attributes:: link_name :: < false > ( link_name) ) ;
785
- } else {
786
- let link_name =
787
- self . mangled_name ( ) . unwrap_or_else ( || self . name ( ) ) ;
788
- if !utils:: names_will_be_identical_after_mangling (
789
- & canonical_name,
790
- link_name,
791
- None ,
792
- ) {
782
+ // None means the canonical name will end up working out the same, so no need to add the attribute.
783
+ let link_name_attribute: Option < & str > =
784
+ self . link_name ( ) . or_else ( || {
785
+ let link_name =
786
+ self . mangled_name ( ) . unwrap_or_else ( || self . name ( ) ) ;
787
+ if utils:: names_will_be_identical_after_mangling (
788
+ & canonical_name,
789
+ link_name,
790
+ None ,
791
+ ) {
792
+ None
793
+ } else {
794
+ Some ( link_name)
795
+ }
796
+ } ) ;
797
+
798
+ // Apply the `#[link_name]` attribute if necessary, but not if we're not generating dynamic library bindings as they're linked at runtime.
799
+ if let Some ( link_name) = link_name_attribute {
800
+ if ctx. options ( ) . dynamic_library_name . is_none ( ) {
793
801
attrs. push ( attributes:: link_name :: < false > ( link_name) ) ;
794
802
}
795
803
}
@@ -814,8 +822,10 @@ impl CodeGenerator for Var {
814
822
) ;
815
823
816
824
if ctx. options ( ) . dynamic_library_name . is_some ( ) {
825
+ let symbol = link_name_attribute. unwrap_or ( & canonical_name) ;
817
826
result. dynamic_items ( ) . push_var (
818
827
canonical_ident,
828
+ symbol,
819
829
self . ty ( )
820
830
. to_rust_ty_or_opaque ( ctx, & ( ) )
821
831
. into_token_stream ( ) ,
@@ -4641,21 +4651,19 @@ impl CodeGenerator for Function {
4641
4651
write ! ( & mut canonical_name, "{times_seen}" ) . unwrap ( ) ;
4642
4652
}
4643
4653
4644
- let mut has_link_name_attr = false ;
4645
- if let Some ( link_name) = self . link_name ( ) {
4646
- attributes. push ( attributes:: link_name :: < false > ( link_name) ) ;
4647
- has_link_name_attr = true ;
4648
- } else {
4649
- let link_name = mangled_name. unwrap_or ( name) ;
4650
- if !is_dynamic_function &&
4651
- !utils:: names_will_be_identical_after_mangling (
4652
- & canonical_name,
4653
- link_name,
4654
- Some ( abi) ,
4655
- )
4656
- {
4654
+ let link_name_attr = self . link_name ( ) . or_else ( || {
4655
+ let mangled_name = mangled_name. unwrap_or ( name) ;
4656
+ ( !utils:: names_will_be_identical_after_mangling (
4657
+ & canonical_name,
4658
+ mangled_name,
4659
+ Some ( abi) ,
4660
+ ) )
4661
+ . then ( || mangled_name)
4662
+ } ) ;
4663
+
4664
+ if let Some ( link_name) = link_name_attr {
4665
+ if ctx. options ( ) . dynamic_library_name . is_none ( ) {
4657
4666
attributes. push ( attributes:: link_name :: < false > ( link_name) ) ;
4658
- has_link_name_attr = true ;
4659
4667
}
4660
4668
}
4661
4669
@@ -4667,8 +4675,9 @@ impl CodeGenerator for Function {
4667
4675
quote ! { #[ link( wasm_import_module = #name) ] }
4668
4676
} ) ;
4669
4677
4670
- let should_wrap =
4671
- is_internal && ctx. options ( ) . wrap_static_fns && !has_link_name_attr;
4678
+ let should_wrap = is_internal &&
4679
+ ctx. options ( ) . wrap_static_fns &&
4680
+ link_name_attr. is_none ( ) ;
4672
4681
4673
4682
if should_wrap {
4674
4683
let name = canonical_name. clone ( ) + ctx. wrap_static_fns_suffix ( ) ;
@@ -4734,11 +4743,14 @@ impl CodeGenerator for Function {
4734
4743
4735
4744
// If we're doing dynamic binding generation, add to the dynamic items.
4736
4745
if is_dynamic_function {
4746
+ let ident_str = ident. to_string ( ) ;
4747
+ let symbol = link_name_attr. unwrap_or ( & ident_str) ;
4737
4748
let args_identifiers =
4738
4749
utils:: fnsig_argument_identifiers ( ctx, signature) ;
4739
4750
let ret_ty = utils:: fnsig_return_ty ( ctx, signature) ;
4740
4751
result. dynamic_items ( ) . push_func (
4741
4752
ident,
4753
+ symbol,
4742
4754
abi,
4743
4755
signature. is_variadic ( ) ,
4744
4756
ctx. options ( ) . dynamic_link_require_all ,
0 commit comments