Skip to content

Commit 1d36721

Browse files
committed
Change create_object_file to a Result to better indicate failure. Fixes rust-lang#102
1 parent 2b574e3 commit 1d36721

File tree

4 files changed

+10
-12
lines changed

4 files changed

+10
-12
lines changed

src/memory_buffer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl MemoryBuffer {
9898
MemoryBuffer::new(memory_buffer)
9999
}
100100

101+
/// Gets a byte slice of this `MemoryBuffer`.
101102
pub fn as_slice(&self) -> &[u8] {
102103
unsafe {
103104
let start = LLVMGetBufferStart(self.memory_buffer);
@@ -106,28 +107,27 @@ impl MemoryBuffer {
106107
}
107108
}
108109

110+
/// Gets the byte size of this `MemoryBuffer`.
109111
pub fn get_size(&self) -> usize {
110112
unsafe {
111113
LLVMGetBufferSize(self.memory_buffer)
112114
}
113115
}
114116

115-
// REVIEW: I haven't yet been able to find docs or other wrappers that confirm, but my suspicion
116-
// is that the method needs to take ownership of the MemoryBuffer... otherwise I see what looks like
117-
// a double free in valgrind when the MemoryBuffer drops so we are `forget`ting MemoryBuffer here
118-
// for now until we can confirm this is the correct thing to do
119-
pub fn create_object_file(self) -> Option<ObjectFile> {
117+
/// Convert this `MemoryBuffer` into an `ObjectFile`. LLVM does not currently
118+
/// provide any way to determine the cause of error if conversion fails.
119+
pub fn create_object_file(self) -> Result<ObjectFile, ()> {
120120
let object_file = unsafe {
121121
LLVMCreateObjectFile(self.memory_buffer)
122122
};
123123

124124
forget(self);
125125

126126
if object_file.is_null() {
127-
return None;
127+
return Err(());
128128
}
129129

130-
Some(ObjectFile::new(object_file))
130+
Ok(ObjectFile::new(object_file))
131131
}
132132
}
133133

src/values/fn_value.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ impl FunctionValue {
290290
// but not in all other versions
291291
#[cfg(feature = "llvm3-8")]
292292
pub unsafe fn get_personality_function(&self) -> Option<FunctionValue> {
293-
let value = unsafe {
294-
LLVMGetPersonalityFn(self.as_value_ref())
295-
};
293+
let value = LLVMGetPersonalityFn(self.as_value_ref());
296294

297295
FunctionValue::new(value)
298296
}

tests/all/test_module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn test_write_and_load_memory_buffer() {
123123
let memory_buffer2 = module.write_bitcode_to_memory();
124124
let object_file = memory_buffer2.create_object_file();
125125

126-
assert!(object_file.is_none());
126+
assert!(object_file.is_err());
127127
}
128128

129129
#[test]

tests/all/test_values.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn test_verify_fn() {
325325

326326
#[cfg(not(any(feature = "llvm3-9", feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0", feature = "llvm7-0", feature = "llvm8-0")))]
327327
assert!(!function.verify(false));
328-
// REVIEW: Why does 3.9 -> 8.0 return true here? LLVM bug? Bugfix?
328+
// REVIEW: Why does 3.9 -> 8.0 return true here? LLVM bug? Bugfix?
329329
#[cfg(any(feature = "llvm3-9", feature = "llvm4-0", feature = "llvm5-0", feature = "llvm6-0", feature = "llvm7-0", feature = "llvm8-0"))]
330330
assert!(function.verify(false));
331331

0 commit comments

Comments
 (0)