]> git.lizzy.rs Git - rust.git/commitdiff
Use new symbol names for items of various kinds.
authorMichael Woerister <michaelwoerister@posteo.net>
Mon, 15 Feb 2016 20:41:16 +0000 (15:41 -0500)
committerNiko Matsakis <niko@alum.mit.edu>
Fri, 25 Mar 2016 18:07:18 +0000 (14:07 -0400)
src/librustc_trans/back/link.rs
src/librustc_trans/trans/base.rs
src/librustc_trans/trans/callee.rs
src/librustc_trans/trans/consts.rs
src/test/auxiliary/issue-17718-aux.rs [new file with mode: 0644]
src/test/auxiliary/issue-17718.rs [deleted file]
src/test/run-pass/backtrace.rs
src/test/run-pass/issue-17718.rs

index 01ef33637d58bd8c038b823ce76b73fec10ce5a0..2a9bd6618a37393d9c91d008452f844efac2af28 100644 (file)
 use middle::cstore::{LinkagePreference, NativeLibraryKind};
 use middle::def_id::DefId;
 use middle::dependency_format::Linkage;
-use middle::ty::{Ty, TyCtxt};
-use rustc::front::map::DefPath;
-use trans::{CrateContext, CrateTranslation, gensym_name};
+use middle::ty::TyCtxt;
+use trans::CrateTranslation;
 use util::common::time;
-use util::sha2::{Digest, Sha256};
 use util::fs::fix_windows_verbatim_for_gcc;
 use rustc_back::tempdir::TempDir;
 
 use std::ffi::OsString;
 use std::fs;
 use std::io::{self, Read, Write};
-use std::iter::once;
 use std::mem;
 use std::path::{Path, PathBuf};
 use std::process::Command;
 use std::str;
 use flate;
-use serialize::hex::ToHex;
 use syntax::ast;
 use syntax::codemap::Span;
-use syntax::parse::token::{self, InternedString};
+use syntax::parse::token::InternedString;
 use syntax::attr::AttrMetaMethods;
 
 use rustc_front::hir;
@@ -195,50 +191,10 @@ pub fn build_link_meta(sess: &Session,
     return r;
 }
 
-fn truncated_hash_result(symbol_hasher: &mut Sha256) -> String {
-    let output = symbol_hasher.result_bytes();
-    // 64 bits should be enough to avoid collisions.
-    output[.. 8].to_hex().to_string()
-}
-
 pub fn def_to_string(_tcx: &TyCtxt, did: DefId) -> String {
     format!("{}:{}", did.krate, did.index.as_usize())
 }
 
-// This calculates STH for a symbol, as defined above
-fn symbol_hash<'tcx>(tcx: &TyCtxt<'tcx>,
-                     symbol_hasher: &mut Sha256,
-                     t: Ty<'tcx>,
-                     link_meta: &LinkMeta)
-                     -> String {
-    // NB: do *not* use abbrevs here as we want the symbol names
-    // to be independent of one another in the crate.
-
-    symbol_hasher.reset();
-    symbol_hasher.input_str(&link_meta.crate_name);
-    symbol_hasher.input_str("-");
-    symbol_hasher.input_str(link_meta.crate_hash.as_str());
-    symbol_hasher.input_str(&tcx.sess.crate_disambiguator.borrow()[..]);
-    symbol_hasher.input_str("-");
-    symbol_hasher.input(&tcx.sess.cstore.encode_type(tcx, t, def_to_string));
-    // Prefix with 'h' so that it never blends into adjacent digits
-    let mut hash = String::from("h");
-    hash.push_str(&truncated_hash_result(symbol_hasher));
-    hash
-}
-
-fn get_symbol_hash<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> String {
-    if let Some(h) = ccx.type_hashcodes().borrow().get(&t) {
-        return h.to_string()
-    }
-
-    let mut symbol_hasher = ccx.symbol_hasher().borrow_mut();
-    let hash = symbol_hash(ccx.tcx(), &mut *symbol_hasher, t, ccx.link_meta());
-    ccx.type_hashcodes().borrow_mut().insert(t, hash.clone());
-    hash
-}
-
-
 // Name sanitation. LLVM will happily accept identifiers with weird names, but
 // gas doesn't!
 // gas accepts the following characters in symbols: a-z, A-Z, 0-9, ., _, $
@@ -324,53 +280,6 @@ fn push(n: &mut String, s: &str) {
     n
 }
 
-pub fn exported_name(path: DefPath, hash: &str) -> String {
-    let path = path.into_iter()
-                   .map(|e| e.data.as_interned_str());
-    mangle(path, Some(hash))
-}
-
-pub fn mangle_exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, path: DefPath,
-                                      t: Ty<'tcx>, id: ast::NodeId) -> String {
-    let mut hash = get_symbol_hash(ccx, t);
-
-    // Paths can be completely identical for different nodes,
-    // e.g. `fn foo() { { fn a() {} } { fn a() {} } }`, so we
-    // generate unique characters from the node id. For now
-    // hopefully 3 characters is enough to avoid collisions.
-    const EXTRA_CHARS: &'static str =
-        "abcdefghijklmnopqrstuvwxyz\
-         ABCDEFGHIJKLMNOPQRSTUVWXYZ\
-         0123456789";
-    let id = id as usize;
-    let extra1 = id % EXTRA_CHARS.len();
-    let id = id / EXTRA_CHARS.len();
-    let extra2 = id % EXTRA_CHARS.len();
-    let id = id / EXTRA_CHARS.len();
-    let extra3 = id % EXTRA_CHARS.len();
-    hash.push(EXTRA_CHARS.as_bytes()[extra1] as char);
-    hash.push(EXTRA_CHARS.as_bytes()[extra2] as char);
-    hash.push(EXTRA_CHARS.as_bytes()[extra3] as char);
-
-    exported_name(path, &hash[..])
-}
-
-pub fn mangle_internal_name_by_type_and_seq<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
-                                                      t: Ty<'tcx>,
-                                                      name: &str) -> String {
-    let path = [token::intern(&t.to_string()).as_str(), gensym_name(name).as_str()];
-    let hash = get_symbol_hash(ccx, t);
-    mangle(path.iter().cloned(), Some(&hash[..]))
-}
-
-pub fn mangle_internal_name_by_path_and_seq(path: DefPath, flav: &str) -> String {
-    let names =
-        path.into_iter()
-            .map(|e| e.data.as_interned_str())
-            .chain(once(gensym_name(flav).as_str())); // append unique version of "flav"
-    mangle(names, None)
-}
-
 pub fn get_linker(sess: &Session) -> (String, Command) {
     if let Some(ref linker) = sess.opts.cg.linker {
         (linker.clone(), Command::new(linker))
index 1d8daf9d86b4ad2129dfe4c71be10a3a9c3f1da0..e903fc2811d02423b3074b5a619e949eb4dd48e8 100644 (file)
@@ -7,6 +7,7 @@
 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
+
 //! Translate the completed AST to the LLVM IR.
 //!
 //! Some functions here, such as trans_block and trans_expr, return a value --
@@ -29,8 +30,7 @@
 use super::CrateTranslation;
 use super::ModuleTranslation;
 
-use back::link::mangle_exported_name;
-use back::link;
+use back::{link, symbol_names};
 use lint;
 use llvm::{BasicBlockRef, Linkage, ValueRef, Vector, get_param};
 use llvm;
@@ -2421,10 +2421,11 @@ fn create_entry_fn(ccx: &CrateContext,
 }
 
 pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
-                               id: ast::NodeId,
-                               ty: Ty<'tcx>,
+                               instance: Instance<'tcx>,
                                attrs: &[ast::Attribute])
                                -> String {
+    let id = ccx.tcx().map.as_local_node_id(instance.def).unwrap();
+
     match ccx.external_srcs().borrow().get(&id) {
         Some(&did) => {
             let sym = ccx.sess().cstore.item_symbol(did);
@@ -2438,16 +2439,16 @@ pub fn exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
         // Use provided name
         Some(name) => name.to_string(),
         _ => {
-            let path = ccx.tcx().map.def_path_from_id(id);
             if attr::contains_name(attrs, "no_mangle") {
                 // Don't mangle
+                let path = ccx.tcx().map.def_path_from_id(id);
                 path.last().unwrap().data.to_string()
             } else {
                 match weak_lang_items::link_name(attrs) {
                     Some(name) => name.to_string(),
                     None => {
                         // Usual name mangling
-                        mangle_exported_name(ccx, path, ty, id)
+                        symbol_names::exported_name(ccx, &instance)
                     }
                 }
             }
index e15fe3698fe42f04f056146eda825e933a077186..6fe4598c1ba3cc397514e08f4cf6e8e225705737 100644 (file)
@@ -515,7 +515,7 @@ fn is_named_tuple_constructor(tcx: &TyCtxt, def_id: DefId) -> bool {
         Some(hir_map::NodeImplItem(&hir::ImplItem {
             ref attrs, id, span, node: hir::ImplItemKind::Method(..), ..
         })) => {
-            let sym = exported_name(ccx, id, ty, attrs);
+            let sym = exported_name(ccx, instance, attrs);
 
             if declare::get_defined_value(ccx, &sym).is_some() {
                 ccx.sess().span_fatal(span,
index 7e6b24969100bef944bee105a2e692e8eb398399..4cdb64a9bfb718d46d702a3aa216ab24c6c5991e 100644 (file)
@@ -1032,7 +1032,7 @@ pub fn get_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, def_id: DefId)
                 // we need to get the symbol from metadata instead of
                 // using the current crate's name/version
                 // information in the hash of the symbol
-                let sym = exported_name(ccx, id, ty, attrs);
+                let sym = exported_name(ccx, instance, attrs);
                 debug!("making {}", sym);
 
                 // Create the global before evaluating the initializer;
diff --git a/src/test/auxiliary/issue-17718-aux.rs b/src/test/auxiliary/issue-17718-aux.rs
new file mode 100644 (file)
index 0000000..373fc04
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(const_fn)]
+
+use std::sync::atomic;
+
+pub const C1: usize = 1;
+pub const C2: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
+pub const C3: fn() = foo;
+pub const C4: usize = C1 * C1 + C1 / C1;
+pub const C5: &'static usize = &C4;
+
+pub static S1: usize = 3;
+pub static S2: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
+
+fn foo() {}
diff --git a/src/test/auxiliary/issue-17718.rs b/src/test/auxiliary/issue-17718.rs
deleted file mode 100644 (file)
index 373fc04..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
-// file at the top-level directory of this distribution and at
-// http://rust-lang.org/COPYRIGHT.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#![feature(const_fn)]
-
-use std::sync::atomic;
-
-pub const C1: usize = 1;
-pub const C2: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
-pub const C3: fn() = foo;
-pub const C4: usize = C1 * C1 + C1 / C1;
-pub const C5: &'static usize = &C4;
-
-pub static S1: usize = 3;
-pub static S2: atomic::AtomicUsize = atomic::AtomicUsize::new(0);
-
-fn foo() {}
index 2a98706351a8b6797427b59feba16291e6ad7a73..88c5fb2c4089199ef411c8c0136f96ab97dfe869 100644 (file)
@@ -57,7 +57,7 @@ fn runtest(me: &str) {
     let out = p.wait_with_output().unwrap();
     assert!(!out.status.success());
     let s = str::from_utf8(&out.stderr).unwrap();
-    assert!(s.contains("stack backtrace") && s.contains(" - foo"),
+    assert!(s.contains("stack backtrace") && s.contains(" - backtrace::foo"),
             "bad output: {}", s);
 
     // Make sure the stack trace is *not* printed
@@ -67,7 +67,7 @@ fn runtest(me: &str) {
     let out = p.wait_with_output().unwrap();
     assert!(!out.status.success());
     let s = str::from_utf8(&out.stderr).unwrap();
-    assert!(!s.contains("stack backtrace") && !s.contains(" - foo"),
+    assert!(!s.contains("stack backtrace") && !s.contains(" - backtrace::foo"),
             "bad output2: {}", s);
 
     // Make sure a stack trace is printed
@@ -77,7 +77,7 @@ fn runtest(me: &str) {
     let s = str::from_utf8(&out.stderr).unwrap();
     // loosened the following from double::h to double:: due to
     // spurious failures on mac, 32bit, optimized
-    assert!(s.contains("stack backtrace") && s.contains(" - double"),
+    assert!(s.contains("stack backtrace") && s.contains(" - backtrace::double"),
             "bad output3: {}", s);
 
     // Make sure a stack trace isn't printed too many times
index 2bb69d105ff5d8c50b05836f29e9e330f3a26e90..744e63f159b6566dc17ceb4187399bf043e2f495 100644 (file)
@@ -8,13 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// aux-build:issue-17718.rs
+// aux-build:issue-17718-aux.rs
 
 
 #![feature(core)]
 #![feature(const_fn)]
 
-extern crate issue_17718 as other;
+extern crate issue_17718_aux as other;
 
 use std::sync::atomic::{AtomicUsize, Ordering};