diff --git a/src/librustc_codegen_llvm/back/lto.rs b/src/librustc_codegen_llvm/back/lto.rs index a5f07e46e11f..2fc8deeec8a2 100644 --- a/src/librustc_codegen_llvm/back/lto.rs +++ b/src/librustc_codegen_llvm/back/lto.rs @@ -649,7 +649,7 @@ pub unsafe fn optimize_thin_module( timeline: &mut Timeline ) -> Result, FatalError> { let diag_handler = cgcx.create_diag_handler(); - let tm = (cgcx.tm_factory)().map_err(|e| { + let tm = (cgcx.tm_factory.0)().map_err(|e| { write::llvm_err(&diag_handler, &e) })?; diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index 29bc3becd7e0..9d569a4e28c4 100644 --- a/src/librustc_codegen_llvm/lib.rs +++ b/src/librustc_codegen_llvm/lib.rs @@ -165,17 +165,6 @@ impl ExtraBackendMethods for LlvmCodegenBackend { } } -impl Clone for &'static mut llvm::TargetMachine { - fn clone(&self) -> Self { - // This method should never be called. It is put here because in - // rustc_codegen_ssa::back::write::CodegenContext, the TargetMachine is contained in a - // closure returned by a function under an Arc. The clone-deriving algorithm works when the - // struct contains the original LLVM TargetMachine type but not any more when supplied with - // a generic type. Hence this dummy Clone implementation. - panic!() - } -} - impl WriteBackendMethods for LlvmCodegenBackend { type Module = ModuleLlvm; type ModuleBuffer = back::lto::ModuleBuffer; diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index e958b5441f22..e8cf9e7f40ae 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -178,6 +178,17 @@ pub struct AssemblerCommand { cmd: Command, } +// HACK(eddyb) work around `#[derive]` producing wrong bounds for `Clone`. +pub struct TargetMachineFactory( + pub Arc Result + Send + Sync>, +); + +impl Clone for TargetMachineFactory { + fn clone(&self) -> Self { + TargetMachineFactory(self.0.clone()) + } +} + /// Additional resources used by optimize_and_codegen (not module specific) #[derive(Clone)] pub struct CodegenContext { @@ -196,8 +207,7 @@ pub struct CodegenContext { pub regular_module_config: Arc, pub metadata_module_config: Arc, pub allocator_module_config: Arc, - pub tm_factory: Arc Result + Send + Sync>, + pub tm_factory: TargetMachineFactory, pub msvc_imps_needed: bool, pub target_pointer_width: String, pub debuginfo: config::DebugInfo, @@ -962,7 +972,7 @@ fn start_executing_work( regular_module_config: modules_config, metadata_module_config: metadata_config, allocator_module_config: allocator_config, - tm_factory: backend.target_machine_factory(tcx.sess, false), + tm_factory: TargetMachineFactory(backend.target_machine_factory(tcx.sess, false)), total_cgus, msvc_imps_needed: msvc_imps_needed(tcx), target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(), diff --git a/src/librustc_codegen_ssa/interfaces/write.rs b/src/librustc_codegen_ssa/interfaces/write.rs index 3419e1c59eda..72522e19af21 100644 --- a/src/librustc_codegen_ssa/interfaces/write.rs +++ b/src/librustc_codegen_ssa/interfaces/write.rs @@ -18,7 +18,7 @@ use rustc_errors::{FatalError, Handler}; pub trait WriteBackendMethods: 'static + Sized + Clone { type Module: Send + Sync; - type TargetMachine: Clone; + type TargetMachine; type ModuleBuffer: ModuleBufferMethods; type Context: ?Sized; type ThinData: Send + Sync;