From c4c39e9395f10005aa6919d426e18fec43328392 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sat, 6 Oct 2018 11:42:14 +0200 Subject: [PATCH] codegen_llvm_back: use Cow<'static, str> where applicable --- src/librustc_codegen_llvm/back/archive.rs | 3 +-- src/librustc_codegen_llvm/back/bytecode.rs | 16 ++++++++-------- src/librustc_codegen_llvm/back/lto.rs | 18 +++++++++--------- src/librustc_codegen_llvm/back/write.rs | 6 +++--- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/librustc_codegen_llvm/back/archive.rs b/src/librustc_codegen_llvm/back/archive.rs index 077c8c31f4a..a69d5b7fd9b 100644 --- a/src/librustc_codegen_llvm/back/archive.rs +++ b/src/librustc_codegen_llvm/back/archive.rs @@ -282,10 +282,9 @@ impl<'a> ArchiveBuilder<'a> { let ret = if r.into_result().is_err() { let err = llvm::LLVMRustGetLastError(); let msg = if err.is_null() { - "failed to write archive".to_string() + "failed to write archive".into() } else { String::from_utf8_lossy(CStr::from_ptr(err).to_bytes()) - .into_owned() }; Err(io::Error::new(io::ErrorKind::Other, msg)) } else { diff --git a/src/librustc_codegen_llvm/back/bytecode.rs b/src/librustc_codegen_llvm/back/bytecode.rs index 9a3dd9d2f88..c6cb94a7e33 100644 --- a/src/librustc_codegen_llvm/back/bytecode.rs +++ b/src/librustc_codegen_llvm/back/bytecode.rs @@ -106,39 +106,39 @@ pub struct DecodedBytecode<'a> { } impl<'a> DecodedBytecode<'a> { - pub fn new(data: &'a [u8]) -> Result, String> { + pub fn new(data: &'a [u8]) -> Result, &'static str> { if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) { - return Err("magic bytecode prefix not found".to_string()) + return Err("magic bytecode prefix not found") } let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..]; if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) { - return Err("wrong version prefix found in bytecode".to_string()) + return Err("wrong version prefix found in bytecode") } let data = &data[4..]; if data.len() < 4 { - return Err("bytecode corrupted".to_string()) + return Err("bytecode corrupted") } let identifier_len = unsafe { u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize }; let data = &data[4..]; if data.len() < identifier_len { - return Err("bytecode corrupted".to_string()) + return Err("bytecode corrupted") } let identifier = match str::from_utf8(&data[..identifier_len]) { Ok(s) => s, - Err(_) => return Err("bytecode corrupted".to_string()) + Err(_) => return Err("bytecode corrupted") }; let data = &data[identifier_len..]; if data.len() < 8 { - return Err("bytecode corrupted".to_string()) + return Err("bytecode corrupted") } let bytecode_len = unsafe { u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize }; let data = &data[8..]; if data.len() < bytecode_len { - return Err("bytecode corrupted".to_string()) + return Err("bytecode corrupted") } let encoded_bytecode = &data[..bytecode_len]; diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs index cb944e08c83..5521ddf0e6a 100644 --- a/src/librustc_codegen_llvm/back/lto.rs +++ b/src/librustc_codegen_llvm/back/lto.rs @@ -296,7 +296,7 @@ fn fat_lto(cgcx: &CodegenContext, let data = bc_decoded.data(); linker.add(&data).map_err(|()| { let msg = format!("failed to load bc of {:?}", name); - write::llvm_err(&diag_handler, msg) + write::llvm_err(&diag_handler, &msg) }) })?; timeline.record(&format!("link {:?}", name)); @@ -490,7 +490,7 @@ fn thin_lto(cgcx: &CodegenContext, symbol_white_list.as_ptr(), symbol_white_list.len() as u32, ).ok_or_else(|| { - write::llvm_err(&diag_handler, "failed to prepare thin LTO context".to_string()) + write::llvm_err(&diag_handler, "failed to prepare thin LTO context") })?; info!("thin LTO data created"); @@ -746,7 +746,7 @@ impl ThinModule { { let diag_handler = cgcx.create_diag_handler(); let tm = (cgcx.tm_factory)().map_err(|e| { - write::llvm_err(&diag_handler, e) + write::llvm_err(&diag_handler, &e) })?; // Right now the implementation we've got only works over serialized @@ -761,7 +761,7 @@ impl ThinModule { self.data().len(), self.shared.module_names[self.idx].as_ptr(), ).ok_or_else(|| { - let msg = "failed to parse bitcode for thin LTO module".to_string(); + let msg = "failed to parse bitcode for thin LTO module"; write::llvm_err(&diag_handler, msg) })? as *const _; let module = ModuleCodegen { @@ -785,7 +785,7 @@ impl ThinModule { let mut cu2 = ptr::null_mut(); llvm::LLVMRustThinLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2); if !cu2.is_null() { - let msg = "multiple source DICompileUnits found".to_string(); + let msg = "multiple source DICompileUnits found"; return Err(write::llvm_err(&diag_handler, msg)) } @@ -806,25 +806,25 @@ impl ThinModule { // You can find some more comments about these functions in the LLVM // bindings we've got (currently `PassWrapper.cpp`) if !llvm::LLVMRustPrepareThinLTORename(self.shared.data.0, llmod) { - let msg = "failed to prepare thin LTO module".to_string(); + let msg = "failed to prepare thin LTO module"; return Err(write::llvm_err(&diag_handler, msg)) } cgcx.save_temp_bitcode(&module, "thin-lto-after-rename"); timeline.record("rename"); if !llvm::LLVMRustPrepareThinLTOResolveWeak(self.shared.data.0, llmod) { - let msg = "failed to prepare thin LTO module".to_string(); + let msg = "failed to prepare thin LTO module"; return Err(write::llvm_err(&diag_handler, msg)) } cgcx.save_temp_bitcode(&module, "thin-lto-after-resolve"); timeline.record("resolve"); if !llvm::LLVMRustPrepareThinLTOInternalize(self.shared.data.0, llmod) { - let msg = "failed to prepare thin LTO module".to_string(); + let msg = "failed to prepare thin LTO module"; return Err(write::llvm_err(&diag_handler, msg)) } cgcx.save_temp_bitcode(&module, "thin-lto-after-internalize"); timeline.record("internalize"); if !llvm::LLVMRustPrepareThinLTOImport(self.shared.data.0, llmod) { - let msg = "failed to prepare thin LTO module".to_string(); + let msg = "failed to prepare thin LTO module"; return Err(write::llvm_err(&diag_handler, msg)) } cgcx.save_temp_bitcode(&module, "thin-lto-after-import"); diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index b55121ca690..f5c7da138c4 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -90,7 +90,7 @@ pub const TLS_MODEL_ARGS : [(&'static str, llvm::ThreadLocalMode); 4] = [ const PRE_THIN_LTO_BC_EXT: &str = "pre-thin-lto.bc"; -pub fn llvm_err(handler: &errors::Handler, msg: String) -> FatalError { +pub fn llvm_err(handler: &errors::Handler, msg: &str) -> FatalError { match llvm::last_error() { Some(err) => handler.fatal(&format!("{}: {}", msg, err)), None => handler.fatal(&msg), @@ -109,7 +109,7 @@ pub fn write_output_file( let result = llvm::LLVMRustWriteOutputFile(target, pm, m, output_c.as_ptr(), file_type); if result.into_result().is_err() { let msg = format!("could not write output to {}", output.display()); - Err(llvm_err(handler, msg)) + Err(llvm_err(handler, &msg)) } else { Ok(()) } @@ -139,7 +139,7 @@ pub fn create_target_machine( find_features: bool, ) -> &'static mut llvm::TargetMachine { target_machine_factory(sess, find_features)().unwrap_or_else(|err| { - llvm_err(sess.diagnostic(), err).raise() + llvm_err(sess.diagnostic(), &err).raise() }) }