83
83
#![ allow( non_upper_case_globals) ]
84
84
85
85
use std:: convert:: From ;
86
- use std:: ffi:: CString ;
86
+ use std:: ffi:: { CString , CStr } ;
87
87
use std:: os:: raw:: { c_char, c_int, c_uint} ;
88
88
use std:: panic:: { catch_unwind, UnwindSafe } ;
89
89
@@ -208,8 +208,6 @@ pub use rb_RCLASS_ORIGIN as RCLASS_ORIGIN;
208
208
209
209
/// Helper so we can get a Rust string for insn_name()
210
210
pub fn insn_name ( opcode : usize ) -> String {
211
- use std:: ffi:: CStr ;
212
-
213
211
unsafe {
214
212
// Look up Ruby's NULL-terminated insn name string
215
213
let op_name = raw_insn_name ( VALUE ( opcode) ) ;
@@ -608,7 +606,6 @@ pub fn rust_str_to_sym(str: &str) -> VALUE {
608
606
pub fn cstr_to_rust_string ( c_char_ptr : * const c_char ) -> Option < String > {
609
607
assert ! ( c_char_ptr != std:: ptr:: null( ) ) ;
610
608
611
- use std:: ffi:: CStr ;
612
609
let c_str: & CStr = unsafe { CStr :: from_ptr ( c_char_ptr) } ;
613
610
614
611
match c_str. to_str ( ) {
@@ -620,17 +617,20 @@ pub fn cstr_to_rust_string(c_char_ptr: *const c_char) -> Option<String> {
620
617
/// A location in Rust code for integrating with debugging facilities defined in C.
621
618
/// Use the [src_loc!] macro to crate an instance.
622
619
pub struct SourceLocation {
623
- pub file : CString ,
620
+ pub file : & ' static CStr ,
624
621
pub line : c_int ,
625
622
}
626
623
627
624
/// Make a [SourceLocation] at the current spot.
628
625
macro_rules! src_loc {
629
626
( ) => {
630
- // NOTE(alan): `CString::new` allocates so we might want to limit this to debug builds.
631
- $crate:: cruby:: SourceLocation {
632
- file: std:: ffi:: CString :: new( file!( ) ) . unwrap( ) , // ASCII source file paths
633
- line: line!( ) . try_into( ) . unwrap( ) , // not that many lines
627
+ {
628
+ // Nul-terminated string with static lifetime, make a CStr out of it safely.
629
+ let file: & ' static str = concat!( file!( ) , '\0' ) ;
630
+ $crate:: cruby:: SourceLocation {
631
+ file: unsafe { std:: ffi:: CStr :: from_ptr( file. as_ptr( ) . cast( ) ) } ,
632
+ line: line!( ) . try_into( ) . unwrap( ) ,
633
+ }
634
634
}
635
635
} ;
636
636
}
@@ -668,17 +668,16 @@ where
668
668
Err ( _) => {
669
669
// Theoretically we can recover from some of these panics,
670
670
// but it's too late if the unwind reaches here.
671
- use std:: { process, str} ;
672
671
673
672
let _ = catch_unwind ( || {
674
673
// IO functions can panic too.
675
674
eprintln ! (
676
675
"YJIT panicked while holding VM lock acquired at {}:{}. Aborting..." ,
677
- str :: from_utf8 ( loc. file. as_bytes ( ) ) . unwrap_or ( "<not utf8>" ) ,
676
+ loc. file. to_string_lossy ( ) ,
678
677
line,
679
678
) ;
680
679
} ) ;
681
- process:: abort ( ) ;
680
+ std :: process:: abort ( ) ;
682
681
}
683
682
} ;
684
683
0 commit comments