Skip to content

Commit c4c39e9

Browse files
committed
codegen_llvm_back: use Cow<'static, str> where applicable
1 parent 1d1dc48 commit c4c39e9

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

src/librustc_codegen_llvm/back/archive.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,9 @@ impl<'a> ArchiveBuilder<'a> {
282282
let ret = if r.into_result().is_err() {
283283
let err = llvm::LLVMRustGetLastError();
284284
let msg = if err.is_null() {
285-
"failed to write archive".to_string()
285+
"failed to write archive".into()
286286
} else {
287287
String::from_utf8_lossy(CStr::from_ptr(err).to_bytes())
288-
.into_owned()
289288
};
290289
Err(io::Error::new(io::ErrorKind::Other, msg))
291290
} else {

src/librustc_codegen_llvm/back/bytecode.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,39 +106,39 @@ pub struct DecodedBytecode<'a> {
106106
}
107107

108108
impl<'a> DecodedBytecode<'a> {
109-
pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, String> {
109+
pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, &'static str> {
110110
if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
111-
return Err("magic bytecode prefix not found".to_string())
111+
return Err("magic bytecode prefix not found")
112112
}
113113
let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
114114
if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
115-
return Err("wrong version prefix found in bytecode".to_string())
115+
return Err("wrong version prefix found in bytecode")
116116
}
117117
let data = &data[4..];
118118
if data.len() < 4 {
119-
return Err("bytecode corrupted".to_string())
119+
return Err("bytecode corrupted")
120120
}
121121
let identifier_len = unsafe {
122122
u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize
123123
};
124124
let data = &data[4..];
125125
if data.len() < identifier_len {
126-
return Err("bytecode corrupted".to_string())
126+
return Err("bytecode corrupted")
127127
}
128128
let identifier = match str::from_utf8(&data[..identifier_len]) {
129129
Ok(s) => s,
130-
Err(_) => return Err("bytecode corrupted".to_string())
130+
Err(_) => return Err("bytecode corrupted")
131131
};
132132
let data = &data[identifier_len..];
133133
if data.len() < 8 {
134-
return Err("bytecode corrupted".to_string())
134+
return Err("bytecode corrupted")
135135
}
136136
let bytecode_len = unsafe {
137137
u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize
138138
};
139139
let data = &data[8..];
140140
if data.len() < bytecode_len {
141-
return Err("bytecode corrupted".to_string())
141+
return Err("bytecode corrupted")
142142
}
143143
let encoded_bytecode = &data[..bytecode_len];
144144

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ fn fat_lto(cgcx: &CodegenContext,
296296
let data = bc_decoded.data();
297297
linker.add(&data).map_err(|()| {
298298
let msg = format!("failed to load bc of {:?}", name);
299-
write::llvm_err(&diag_handler, msg)
299+
write::llvm_err(&diag_handler, &msg)
300300
})
301301
})?;
302302
timeline.record(&format!("link {:?}", name));
@@ -490,7 +490,7 @@ fn thin_lto(cgcx: &CodegenContext,
490490
symbol_white_list.as_ptr(),
491491
symbol_white_list.len() as u32,
492492
).ok_or_else(|| {
493-
write::llvm_err(&diag_handler, "failed to prepare thin LTO context".to_string())
493+
write::llvm_err(&diag_handler, "failed to prepare thin LTO context")
494494
})?;
495495

496496
info!("thin LTO data created");
@@ -746,7 +746,7 @@ impl ThinModule {
746746
{
747747
let diag_handler = cgcx.create_diag_handler();
748748
let tm = (cgcx.tm_factory)().map_err(|e| {
749-
write::llvm_err(&diag_handler, e)
749+
write::llvm_err(&diag_handler, &e)
750750
})?;
751751

752752
// Right now the implementation we've got only works over serialized
@@ -761,7 +761,7 @@ impl ThinModule {
761761
self.data().len(),
762762
self.shared.module_names[self.idx].as_ptr(),
763763
).ok_or_else(|| {
764-
let msg = "failed to parse bitcode for thin LTO module".to_string();
764+
let msg = "failed to parse bitcode for thin LTO module";
765765
write::llvm_err(&diag_handler, msg)
766766
})? as *const _;
767767
let module = ModuleCodegen {
@@ -785,7 +785,7 @@ impl ThinModule {
785785
let mut cu2 = ptr::null_mut();
786786
llvm::LLVMRustThinLTOGetDICompileUnit(llmod, &mut cu1, &mut cu2);
787787
if !cu2.is_null() {
788-
let msg = "multiple source DICompileUnits found".to_string();
788+
let msg = "multiple source DICompileUnits found";
789789
return Err(write::llvm_err(&diag_handler, msg))
790790
}
791791

@@ -806,25 +806,25 @@ impl ThinModule {
806806
// You can find some more comments about these functions in the LLVM
807807
// bindings we've got (currently `PassWrapper.cpp`)
808808
if !llvm::LLVMRustPrepareThinLTORename(self.shared.data.0, llmod) {
809-
let msg = "failed to prepare thin LTO module".to_string();
809+
let msg = "failed to prepare thin LTO module";
810810
return Err(write::llvm_err(&diag_handler, msg))
811811
}
812812
cgcx.save_temp_bitcode(&module, "thin-lto-after-rename");
813813
timeline.record("rename");
814814
if !llvm::LLVMRustPrepareThinLTOResolveWeak(self.shared.data.0, llmod) {
815-
let msg = "failed to prepare thin LTO module".to_string();
815+
let msg = "failed to prepare thin LTO module";
816816
return Err(write::llvm_err(&diag_handler, msg))
817817
}
818818
cgcx.save_temp_bitcode(&module, "thin-lto-after-resolve");
819819
timeline.record("resolve");
820820
if !llvm::LLVMRustPrepareThinLTOInternalize(self.shared.data.0, llmod) {
821-
let msg = "failed to prepare thin LTO module".to_string();
821+
let msg = "failed to prepare thin LTO module";
822822
return Err(write::llvm_err(&diag_handler, msg))
823823
}
824824
cgcx.save_temp_bitcode(&module, "thin-lto-after-internalize");
825825
timeline.record("internalize");
826826
if !llvm::LLVMRustPrepareThinLTOImport(self.shared.data.0, llmod) {
827-
let msg = "failed to prepare thin LTO module".to_string();
827+
let msg = "failed to prepare thin LTO module";
828828
return Err(write::llvm_err(&diag_handler, msg))
829829
}
830830
cgcx.save_temp_bitcode(&module, "thin-lto-after-import");

src/librustc_codegen_llvm/back/write.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub const TLS_MODEL_ARGS : [(&'static str, llvm::ThreadLocalMode); 4] = [
9090

9191
const PRE_THIN_LTO_BC_EXT: &str = "pre-thin-lto.bc";
9292

93-
pub fn llvm_err(handler: &errors::Handler, msg: String) -> FatalError {
93+
pub fn llvm_err(handler: &errors::Handler, msg: &str) -> FatalError {
9494
match llvm::last_error() {
9595
Some(err) => handler.fatal(&format!("{}: {}", msg, err)),
9696
None => handler.fatal(&msg),
@@ -109,7 +109,7 @@ pub fn write_output_file(
109109
let result = llvm::LLVMRustWriteOutputFile(target, pm, m, output_c.as_ptr(), file_type);
110110
if result.into_result().is_err() {
111111
let msg = format!("could not write output to {}", output.display());
112-
Err(llvm_err(handler, msg))
112+
Err(llvm_err(handler, &msg))
113113
} else {
114114
Ok(())
115115
}
@@ -139,7 +139,7 @@ pub fn create_target_machine(
139139
find_features: bool,
140140
) -> &'static mut llvm::TargetMachine {
141141
target_machine_factory(sess, find_features)().unwrap_or_else(|err| {
142-
llvm_err(sess.diagnostic(), err).raise()
142+
llvm_err(sess.diagnostic(), &err).raise()
143143
})
144144
}
145145

0 commit comments

Comments
 (0)