Skip to content

Commit 0ab3444

Browse files
committed
rustc_codegen_llvm: use safe references for OperandBundleDef.
1 parent 92af969 commit 0ab3444

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

src/librustc_codegen_llvm/builder.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::borrow::Cow;
2323
use std::ffi::CString;
2424
use std::ops::Range;
2525
use std::ptr;
26-
use std::ptr::NonNull;
2726

2827
// All Builders must have an llfn associated with them
2928
#[must_use]
@@ -176,15 +175,15 @@ impl Builder<'a, 'll, 'tcx> {
176175
args: &[&'ll Value],
177176
then: &'ll BasicBlock,
178177
catch: &'ll BasicBlock,
179-
bundle: Option<&OperandBundleDef>) -> &'ll Value {
178+
bundle: Option<&OperandBundleDef<'ll>>) -> &'ll Value {
180179
self.count_insn("invoke");
181180

182181
debug!("Invoke {:?} with args ({:?})",
183182
llfn,
184183
args);
185184

186185
let args = self.check_call("invoke", llfn, args);
187-
let bundle = bundle.as_ref().and_then(|b| NonNull::new(b.raw()));
186+
let bundle = bundle.map(|b| &*b.raw);
188187

189188
unsafe {
190189
llvm::LLVMRustBuildInvoke(self.llbuilder,
@@ -724,15 +723,15 @@ impl Builder<'a, 'll, 'tcx> {
724723
}
725724

726725
pub fn call(&self, llfn: &'ll Value, args: &[&'ll Value],
727-
bundle: Option<&OperandBundleDef>) -> &'ll Value {
726+
bundle: Option<&OperandBundleDef<'ll>>) -> &'ll Value {
728727
self.count_insn("call");
729728

730729
debug!("Call {:?} with args ({:?})",
731730
llfn,
732731
args);
733732

734733
let args = self.check_call("call", llfn, args);
735-
let bundle = bundle.as_ref().and_then(|b| NonNull::new(b.raw()));
734+
let bundle = bundle.map(|b| &*b.raw);
736735

737736
unsafe {
738737
llvm::LLVMRustBuildCall(self.llbuilder, llfn, args.as_ptr(),

src/librustc_codegen_llvm/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn type_is_freeze<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> bo
9292
/// the `OperandBundleDef` value created for MSVC landing pads.
9393
pub struct Funclet<'ll> {
9494
cleanuppad: &'ll Value,
95-
operand: OperandBundleDef,
95+
operand: OperandBundleDef<'ll>,
9696
}
9797

9898
impl Funclet<'ll> {
@@ -107,7 +107,7 @@ impl Funclet<'ll> {
107107
self.cleanuppad
108108
}
109109

110-
pub fn bundle(&self) -> &OperandBundleDef {
110+
pub fn bundle(&self) -> &OperandBundleDef<'ll> {
111111
&self.operand
112112
}
113113
}

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use super::debuginfo::{
2424
use libc::{c_uint, c_int, size_t, c_char};
2525
use libc::{c_ulonglong, c_void};
2626

27+
use std::marker::PhantomData;
2728
use std::ptr::NonNull;
2829

2930
use super::RustString;
@@ -381,6 +382,12 @@ pub enum ThreadLocalMode {
381382
LocalExec
382383
}
383384

385+
extern { type Opaque; }
386+
struct InvariantOpaque<'a> {
387+
_marker: PhantomData<&'a mut &'a ()>,
388+
_opaque: Opaque,
389+
}
390+
384391
// Opaque pointer types
385392
extern { pub type Module; }
386393
extern { pub type Context; }
@@ -408,8 +415,7 @@ extern { pub type DiagnosticInfo; }
408415
extern { pub type SMDiagnostic; }
409416
extern { pub type RustArchiveMember; }
410417
pub type RustArchiveMemberRef = *mut RustArchiveMember;
411-
extern { pub type OperandBundleDef; }
412-
pub type OperandBundleDefRef = *mut OperandBundleDef;
418+
pub struct OperandBundleDef<'a>(InvariantOpaque<'a>);
413419
extern { pub type Linker; }
414420
pub type LinkerRef = *mut Linker;
415421

@@ -706,7 +712,7 @@ extern "C" {
706712
NumArgs: c_uint,
707713
Then: &'a BasicBlock,
708714
Catch: &'a BasicBlock,
709-
Bundle: Option<NonNull<OperandBundleDef>>,
715+
Bundle: Option<&OperandBundleDef<'a>>,
710716
Name: *const c_char)
711717
-> &'a Value;
712718
pub fn LLVMBuildLandingPad(B: &'a Builder,
@@ -975,7 +981,7 @@ extern "C" {
975981
Fn: &'a Value,
976982
Args: *const &'a Value,
977983
NumArgs: c_uint,
978-
Bundle: Option<NonNull<OperandBundleDef>>,
984+
Bundle: Option<&OperandBundleDef<'a>>,
979985
Name: *const c_char)
980986
-> &'a Value;
981987
pub fn LLVMBuildSelect(B: &'a Builder,
@@ -1520,10 +1526,10 @@ extern "C" {
15201526
pub fn LLVMRustSetDataLayoutFromTargetMachine(M: &'a Module, TM: &'a TargetMachine);
15211527

15221528
pub fn LLVMRustBuildOperandBundleDef(Name: *const c_char,
1523-
Inputs: *const &Value,
1529+
Inputs: *const &'a Value,
15241530
NumInputs: c_uint)
1525-
-> OperandBundleDefRef;
1526-
pub fn LLVMRustFreeOperandBundleDef(Bundle: OperandBundleDefRef);
1531+
-> &'a mut OperandBundleDef<'a>;
1532+
pub fn LLVMRustFreeOperandBundleDef(Bundle: &'a mut OperandBundleDef<'a>);
15271533

15281534
pub fn LLVMRustPositionBuilderAtStart(B: &'a Builder, BB: &'a BasicBlock);
15291535

src/librustc_codegen_llvm/llvm/mod.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,28 +258,24 @@ pub fn last_error() -> Option<String> {
258258
}
259259
}
260260

261-
pub struct OperandBundleDef {
262-
inner: OperandBundleDefRef,
261+
pub struct OperandBundleDef<'a> {
262+
pub raw: &'a mut ffi::OperandBundleDef<'a>,
263263
}
264264

265-
impl OperandBundleDef {
266-
pub fn new(name: &str, vals: &[&'a Value]) -> OperandBundleDef {
265+
impl OperandBundleDef<'a> {
266+
pub fn new(name: &str, vals: &[&'a Value]) -> Self {
267267
let name = CString::new(name).unwrap();
268268
let def = unsafe {
269269
LLVMRustBuildOperandBundleDef(name.as_ptr(), vals.as_ptr(), vals.len() as c_uint)
270270
};
271-
OperandBundleDef { inner: def }
272-
}
273-
274-
pub fn raw(&self) -> OperandBundleDefRef {
275-
self.inner
271+
OperandBundleDef { raw: def }
276272
}
277273
}
278274

279-
impl Drop for OperandBundleDef {
275+
impl Drop for OperandBundleDef<'a> {
280276
fn drop(&mut self) {
281277
unsafe {
282-
LLVMRustFreeOperandBundleDef(self.inner);
278+
LLVMRustFreeOperandBundleDef(&mut *(self.raw as *mut _));
283279
}
284280
}
285281
}

0 commit comments

Comments
 (0)