Skip to content

Commit faec0be

Browse files
Mark BasicBlock::get_address as unsafe
1 parent c07d7f0 commit faec0be

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/basic_block.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,11 @@ impl<'ctx> BasicBlock<'ctx> {
515515
}
516516
}
517517

518-
/// Gets the address of this `BasicBlock` if possible.
518+
/// Gets the address of this `BasicBlock` if possible. Returns `None` if `self` is the entry block to a function.
519+
///
520+
/// # Safety
521+
///
522+
/// The returned PointerValue may only be used for `call` and `indirect_branch` instructions
519523
///
520524
/// # Example
521525
///
@@ -526,16 +530,19 @@ impl<'ctx> BasicBlock<'ctx> {
526530
/// let void_type = context.void_type();
527531
/// let fn_type = void_type.fn_type(&[], false);
528532
/// let fn_val = module.add_function("my_fn", fn_type, None);
529-
/// let bb = context.append_basic_block(fn_val, "entry");
533+
/// let entry_bb = context.append_basic_block(fn_val, "entry");
534+
/// let next_bb = context.append_basic_block(fn_val, "next");
530535
///
531-
/// assert!(bb.get_address().is_some());
536+
/// assert!(unsafe { entry_bb.get_address() }.is_none());
537+
/// assert!(unsafe { next_bb.get_address() }.is_some());
532538
/// ```
533-
pub fn get_address(self) -> Option<PointerValue<'ctx>> {
539+
pub unsafe fn get_address(self) -> Option<PointerValue<'ctx>> {
534540
let parent = self.get_parent()?;
535541

536-
let value = unsafe {
537-
PointerValue::new(LLVMBlockAddress(parent.as_value_ref(), self.basic_block))
538-
};
542+
// Taking the address of the entry block is illegal.
543+
self.get_previous_basic_block()?;
544+
545+
let value = PointerValue::new(LLVMBlockAddress(parent.as_value_ref(), self.basic_block));
539546

540547
if value.is_null() {
541548
return None;

tests/all/test_basic_block.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ fn test_get_address() {
209209
let void_type = context.void_type();
210210
let fn_type = void_type.fn_type(&[], false);
211211
let fn_val = module.add_function("my_fn", fn_type, None);
212-
let bb = context.append_basic_block(fn_val, "entry");
212+
let entry_bb = context.append_basic_block(fn_val, "entry");
213+
let next_bb = context.append_basic_block(fn_val, "next");
213214

214-
assert!(bb.get_address().is_some());
215+
assert!(unsafe { entry_bb.get_address() }.is_none());
216+
assert!(unsafe { next_bb.get_address() }.is_some());
215217
}

0 commit comments

Comments
 (0)