Skip to content

Commit 41d7d8e

Browse files
committed
rustc_codegen_llvm: use safe references for Archive.
1 parent 0e3a705 commit 41d7d8e

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

src/librustc_codegen_llvm/llvm/archive_ro.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010

1111
//! A wrapper around LLVM's archive (.a) code
1212
13-
use super::ArchiveRef;
14-
1513
use std::ffi::CString;
1614
use std::marker;
1715
use std::path::Path;
1816
use std::slice;
1917
use std::str;
2018

2119
pub struct ArchiveRO {
22-
ptr: ArchiveRef,
20+
raw: &'static mut super::Archive,
2321
}
2422

2523
unsafe impl Send for ArchiveRO {}
@@ -44,12 +42,9 @@ impl ArchiveRO {
4442
pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
4543
return unsafe {
4644
let s = path2cstr(dst);
47-
let ar = super::LLVMRustOpenArchive(s.as_ptr());
48-
if ar.is_null() {
49-
Err(super::last_error().unwrap_or("failed to open archive".to_string()))
50-
} else {
51-
Ok(ArchiveRO { ptr: ar })
52-
}
45+
let ar = super::LLVMRustOpenArchive(s.as_ptr())
46+
.ok_or_else(|| super::last_error().unwrap_or("failed to open archive".to_string()))?;
47+
Ok(ArchiveRO { raw: ar })
5348
};
5449

5550
#[cfg(unix)]
@@ -65,14 +60,14 @@ impl ArchiveRO {
6560
}
6661
}
6762

68-
pub fn raw(&self) -> ArchiveRef {
69-
self.ptr
63+
pub fn raw(&self) -> &super::Archive {
64+
self.raw
7065
}
7166

7267
pub fn iter(&self) -> Iter {
7368
unsafe {
7469
Iter {
75-
ptr: super::LLVMRustArchiveIteratorNew(self.ptr),
70+
ptr: super::LLVMRustArchiveIteratorNew(self.raw),
7671
_data: marker::PhantomData,
7772
}
7873
}
@@ -82,7 +77,7 @@ impl ArchiveRO {
8277
impl Drop for ArchiveRO {
8378
fn drop(&mut self) {
8479
unsafe {
85-
super::LLVMRustDestroyArchive(self.ptr);
80+
super::LLVMRustDestroyArchive(&mut *(self.raw as *mut _));
8681
}
8782
}
8883
}

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ pub type SectionIteratorRef = *mut SectionIterator;
399399
extern { pub type Pass; }
400400
extern { pub type TargetMachine; }
401401
extern { pub type Archive; }
402-
pub type ArchiveRef = *mut Archive;
403402
extern { pub type ArchiveIterator; }
404403
pub type ArchiveIteratorRef = *mut ArchiveIterator;
405404
extern { pub type ArchiveChild; }
@@ -1471,14 +1470,14 @@ extern "C" {
14711470
pub fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);
14721471
pub fn LLVMRustMarkAllFunctionsNounwind(M: &Module);
14731472

1474-
pub fn LLVMRustOpenArchive(path: *const c_char) -> ArchiveRef;
1475-
pub fn LLVMRustArchiveIteratorNew(AR: ArchiveRef) -> ArchiveIteratorRef;
1473+
pub fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;
1474+
pub fn LLVMRustArchiveIteratorNew(AR: &Archive) -> ArchiveIteratorRef;
14761475
pub fn LLVMRustArchiveIteratorNext(AIR: ArchiveIteratorRef) -> ArchiveChildRef;
14771476
pub fn LLVMRustArchiveChildName(ACR: ArchiveChildRef, size: *mut size_t) -> *const c_char;
14781477
pub fn LLVMRustArchiveChildData(ACR: ArchiveChildRef, size: *mut size_t) -> *const c_char;
14791478
pub fn LLVMRustArchiveChildFree(ACR: ArchiveChildRef);
14801479
pub fn LLVMRustArchiveIteratorFree(AIR: ArchiveIteratorRef);
1481-
pub fn LLVMRustDestroyArchive(AR: ArchiveRef);
1480+
pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);
14821481

14831482
pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;
14841483

0 commit comments

Comments
 (0)