Skip to content

Commit d1410ad

Browse files
committed
[eddyb] rustc_codegen_ssa: avoid a Clone bound on TargetMachine.
1 parent 47c84c4 commit d1410ad

File tree

4 files changed

+15
-16
lines changed

4 files changed

+15
-16
lines changed

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ pub unsafe fn optimize_thin_module(
649649
timeline: &mut Timeline
650650
) -> Result<ModuleCodegen<ModuleLlvm>, FatalError> {
651651
let diag_handler = cgcx.create_diag_handler();
652-
let tm = (cgcx.tm_factory)().map_err(|e| {
652+
let tm = (cgcx.tm_factory.0)().map_err(|e| {
653653
write::llvm_err(&diag_handler, &e)
654654
})?;
655655

src/librustc_codegen_llvm/lib.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,6 @@ impl ExtraBackendMethods for LlvmCodegenBackend {
165165
}
166166
}
167167

168-
impl Clone for &'static mut llvm::TargetMachine {
169-
fn clone(&self) -> Self {
170-
// This method should never be called. It is put here because in
171-
// rustc_codegen_ssa::back::write::CodegenContext, the TargetMachine is contained in a
172-
// closure returned by a function under an Arc. The clone-deriving algorithm works when the
173-
// struct contains the original LLVM TargetMachine type but not any more when supplied with
174-
// a generic type. Hence this dummy Clone implementation.
175-
panic!()
176-
}
177-
}
178-
179168
impl WriteBackendMethods for LlvmCodegenBackend {
180169
type Module = ModuleLlvm;
181170
type ModuleBuffer = back::lto::ModuleBuffer;

src/librustc_codegen_ssa/back/write.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ pub struct AssemblerCommand {
178178
cmd: Command,
179179
}
180180

181+
// HACK(eddyb) work around `#[derive]` producing wrong bounds for `Clone`.
182+
pub struct TargetMachineFactory<B: WriteBackendMethods>(
183+
pub Arc<dyn Fn() -> Result<B::TargetMachine, String> + Send + Sync>,
184+
);
185+
186+
impl<B: WriteBackendMethods> Clone for TargetMachineFactory<B> {
187+
fn clone(&self) -> Self {
188+
TargetMachineFactory(self.0.clone())
189+
}
190+
}
191+
181192
/// Additional resources used by optimize_and_codegen (not module specific)
182193
#[derive(Clone)]
183194
pub struct CodegenContext<B: WriteBackendMethods> {
@@ -196,8 +207,7 @@ pub struct CodegenContext<B: WriteBackendMethods> {
196207
pub regular_module_config: Arc<ModuleConfig>,
197208
pub metadata_module_config: Arc<ModuleConfig>,
198209
pub allocator_module_config: Arc<ModuleConfig>,
199-
pub tm_factory: Arc<dyn Fn()
200-
-> Result<B::TargetMachine, String> + Send + Sync>,
210+
pub tm_factory: TargetMachineFactory<B>,
201211
pub msvc_imps_needed: bool,
202212
pub target_pointer_width: String,
203213
pub debuginfo: config::DebugInfo,
@@ -962,7 +972,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
962972
regular_module_config: modules_config,
963973
metadata_module_config: metadata_config,
964974
allocator_module_config: allocator_config,
965-
tm_factory: backend.target_machine_factory(tcx.sess, false),
975+
tm_factory: TargetMachineFactory(backend.target_machine_factory(tcx.sess, false)),
966976
total_cgus,
967977
msvc_imps_needed: msvc_imps_needed(tcx),
968978
target_pointer_width: tcx.sess.target.target.target_pointer_width.clone(),

src/librustc_codegen_ssa/interfaces/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_errors::{FatalError, Handler};
1818

1919
pub trait WriteBackendMethods: 'static + Sized + Clone {
2020
type Module: Send + Sync;
21-
type TargetMachine: Clone;
21+
type TargetMachine;
2222
type ModuleBuffer: ModuleBufferMethods;
2323
type Context: ?Sized;
2424
type ThinData: Send + Sync;

0 commit comments

Comments
 (0)