Skip to content

Commit c1eeb69

Browse files
committed
rustc_codegen_llvm: use safe references for RustString.
1 parent 44ae6f1 commit c1eeb69

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

src/librustc_codegen_llvm/llvm/diagnostic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ impl OptimizationDiagnostic<'ll> {
7373
&mut column,
7474
filename,
7575
message)
76-
)
77-
)
78-
);
76+
).ok()
77+
).ok()
78+
).ok();
7979

8080
let mut filename = filename.unwrap_or(String::new());
8181
if filename.is_empty() {

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use libc::{c_ulonglong, c_void};
2626

2727
use std::ptr::NonNull;
2828

29-
use super::RustStringRef;
29+
use super::RustString;
3030

3131
pub type Bool = c_uint;
3232

@@ -1402,8 +1402,8 @@ extern "C" {
14021402
pub fn LLVMRustDIBuilderCreateOpDeref() -> i64;
14031403
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> i64;
14041404

1405-
pub fn LLVMRustWriteTypeToString(Type: &Type, s: RustStringRef);
1406-
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: RustStringRef);
1405+
pub fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
1406+
pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
14071407

14081408
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
14091409
pub fn LLVMIsAConstantFP(value_ref: &Value) -> Option<&Value>;
@@ -1478,32 +1478,32 @@ extern "C" {
14781478

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

1481-
pub fn LLVMRustWriteTwineToString(T: &Twine, s: RustStringRef);
1481+
pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
14821482

14831483
pub fn LLVMContextSetDiagnosticHandler(C: &Context,
14841484
Handler: DiagnosticHandler,
14851485
DiagnosticContext: *mut c_void);
14861486

14871487
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo,
1488-
pass_name_out: RustStringRef,
1489-
function_out: *mut Option<&'a Value>,
1490-
loc_line_out: *mut c_uint,
1491-
loc_column_out: *mut c_uint,
1492-
loc_filename_out: RustStringRef,
1493-
message_out: RustStringRef);
1488+
pass_name_out: &RustString,
1489+
function_out: &mut Option<&'a Value>,
1490+
loc_line_out: &mut c_uint,
1491+
loc_column_out: &mut c_uint,
1492+
loc_filename_out: &RustString,
1493+
message_out: &RustString);
14941494
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo,
14951495
cookie_out: *mut c_uint,
14961496
message_out: *mut Option<&'a Twine>,
14971497
instruction_out: *mut Option<&'a Value>);
14981498

1499-
pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: RustStringRef);
1499+
pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: &RustString);
15001500
pub fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
15011501

15021502
pub fn LLVMRustSetInlineAsmDiagnosticHandler(C: &Context,
15031503
H: InlineAsmDiagHandler,
15041504
CX: *mut c_void);
15051505

1506-
pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: RustStringRef);
1506+
pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: &RustString);
15071507

15081508
pub fn LLVMRustWriteArchive(Dst: *const c_char,
15091509
NumMembers: size_t,

src/librustc_codegen_llvm/llvm/mod.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use self::CallConv::*;
2222
pub use self::Linkage::*;
2323

2424
use std::str::FromStr;
25+
use std::string::FromUtf8Error;
2526
use std::slice;
2627
use std::ffi::{CString, CStr};
2728
use std::cell::RefCell;
@@ -92,20 +93,19 @@ impl FromStr for ArchiveKind {
9293
}
9394
}
9495

95-
#[allow(missing_copy_implementations)]
96-
extern { pub type RustString; }
97-
type RustStringRef = *mut RustString;
98-
type RustStringRepr = *mut RefCell<Vec<u8>>;
96+
#[repr(C)]
97+
pub struct RustString {
98+
bytes: RefCell<Vec<u8>>,
99+
}
99100

100101
/// Appending to a Rust string -- used by RawRustStringOstream.
101102
#[no_mangle]
102-
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: RustStringRef,
103+
pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
103104
ptr: *const c_char,
104105
size: size_t) {
105106
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
106107

107-
let sr = sr as RustStringRepr;
108-
(*sr).borrow_mut().extend_from_slice(slice);
108+
sr.bytes.borrow_mut().extend_from_slice(slice);
109109
}
110110

111111
pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
@@ -229,16 +229,19 @@ pub fn get_param(llfn: &'a Value, index: c_uint) -> &'a Value {
229229
}
230230
}
231231

232-
pub fn build_string<F>(f: F) -> Option<String>
233-
where F: FnOnce(RustStringRef)
234-
{
235-
let mut buf = RefCell::new(Vec::new());
236-
f(&mut buf as RustStringRepr as RustStringRef);
237-
String::from_utf8(buf.into_inner()).ok()
232+
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
233+
let sr = RustString {
234+
bytes: RefCell::new(Vec::new()),
235+
};
236+
f(&sr);
237+
String::from_utf8(sr.bytes.into_inner())
238238
}
239239

240-
pub unsafe fn twine_to_string(tr: &Twine) -> String {
241-
build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
240+
pub fn twine_to_string(tr: &Twine) -> String {
241+
unsafe {
242+
build_string(|s| LLVMRustWriteTwineToString(tr, s))
243+
.expect("got a non-UTF8 Twine from LLVM")
244+
}
242245
}
243246

244247
pub fn last_error() -> Option<String> {

0 commit comments

Comments
 (0)