Skip to content

Commit 89e8caa

Browse files
committed
rustc: fail if LLVM is passed an invalid triple
This changes create_target_machine to correctly return a Result (Since the underlying LLVM function can fail and return NULL)
1 parent e05c3b7 commit 89e8caa

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/librustc/back/write.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,10 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef {
226226
}
227227
};
228228

229-
unsafe {
230-
sess.targ_cfg
231-
.target_strs
232-
.target_triple
233-
.as_slice()
234-
.with_c_str(|t| {
229+
let triple = sess.targ_cfg.target_strs.target_triple.as_slice();
230+
231+
let tm = unsafe {
232+
triple.with_c_str(|t| {
235233
sess.opts.cg.target_cpu.as_slice().with_c_str(|cpu| {
236234
target_feature(sess).with_c_str(|features| {
237235
llvm::LLVMRustCreateTargetMachine(
@@ -249,7 +247,15 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef {
249247
})
250248
})
251249
})
252-
}
250+
};
251+
252+
if tm.is_null() {
253+
llvm_err(sess.diagnostic().handler(),
254+
format!("Could not create LLVM TargetMachine for triple: {}",
255+
triple).to_string());
256+
} else {
257+
return tm;
258+
};
253259
}
254260

255261

0 commit comments

Comments
 (0)