@@ -42,7 +42,7 @@ use std::ffi::CString;
42
42
use std:: fmt:: Write ;
43
43
use std:: ptr;
44
44
use std:: path:: { Path , PathBuf } ;
45
- use syntax:: ast;
45
+ use syntax:: { ast, attr } ;
46
46
use syntax:: symbol:: { Interner , InternedString , Symbol } ;
47
47
use syntax_pos:: { self , Span , FileName } ;
48
48
@@ -1643,8 +1643,10 @@ pub fn create_global_var_metadata(cx: &CrateContext,
1643
1643
}
1644
1644
1645
1645
let tcx = cx. tcx ( ) ;
1646
-
1647
1646
let node_def_id = tcx. hir . local_def_id ( node_id) ;
1647
+ let no_mangle = attr:: contains_name ( & tcx. get_attrs ( node_def_id) , "no_mangle" ) ;
1648
+ // We may want to remove the namespace scope if we're in an extern block, see:
1649
+ // https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952
1648
1650
let var_scope = get_namespace_for_item ( cx, node_def_id) ;
1649
1651
let span = cx. tcx ( ) . def_span ( node_def_id) ;
1650
1652
@@ -1659,18 +1661,24 @@ pub fn create_global_var_metadata(cx: &CrateContext,
1659
1661
let variable_type = Instance :: mono ( cx. tcx ( ) , node_def_id) . ty ( cx. tcx ( ) ) ;
1660
1662
let type_metadata = type_metadata ( cx, variable_type, span) ;
1661
1663
let var_name = tcx. item_name ( node_def_id) . to_string ( ) ;
1662
- let linkage_name = mangled_name_of_item ( cx, node_def_id, "" ) ;
1663
-
1664
1664
let var_name = CString :: new ( var_name) . unwrap ( ) ;
1665
- let linkage_name = CString :: new ( linkage_name) . unwrap ( ) ;
1665
+ let linkage_name = if no_mangle {
1666
+ None
1667
+ } else {
1668
+ let linkage_name = mangled_name_of_item ( cx, node_def_id, "" ) ;
1669
+ Some ( CString :: new ( linkage_name) . unwrap ( ) )
1670
+ } ;
1666
1671
1667
1672
let global_align = cx. align_of ( variable_type) ;
1668
1673
1669
1674
unsafe {
1670
1675
llvm:: LLVMRustDIBuilderCreateStaticVariable ( DIB ( cx) ,
1671
1676
var_scope,
1672
1677
var_name. as_ptr ( ) ,
1673
- linkage_name. as_ptr ( ) ,
1678
+ // If null, linkage_name field is omitted,
1679
+ // which is what we want for no_mangle statics
1680
+ linkage_name. as_ref ( )
1681
+ . map_or ( ptr:: null ( ) , |name| name. as_ptr ( ) ) ,
1674
1682
file_metadata,
1675
1683
line_number,
1676
1684
type_metadata,
0 commit comments