Skip to content

Commit

Permalink
Merge branch 'master' into ml-aba-fix-llvm-verify-memory-leak
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDan64 committed Jul 27, 2024
2 parents 3ad8a39 + d1c0936 commit 5d65484
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/types/float_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ impl<'ctx> FloatType<'ctx> {
unsafe { FloatValue::new(LLVMConstReal(self.float_type.ty, value)) }
}

/// Create a `FloatValue` from a string. LLVM provides no error handling here,
/// so this may produce unexpected results and should not be relied upon for validation.
// We could make this safe again by doing the validation for users.
/// Create a `FloatValue` from a string. This function is marked unsafe because LLVM
/// provides no error handling here, so this may produce undefined behavior if an invalid
/// string is used.
///
/// # Example
///
Expand All @@ -109,28 +111,26 @@ impl<'ctx> FloatType<'ctx> {
///
/// let context = Context::create();
/// let f64_type = context.f64_type();
/// let f64_val = f64_type.const_float_from_string("3.6");
/// let f64_val = unsafe { f64_type.const_float_from_string("3.6") };
///
/// assert_eq!(f64_val.print_to_string().to_string(), "double 3.600000e+00");
///
/// let f64_val = f64_type.const_float_from_string("3.");
/// let f64_val = unsafe { f64_type.const_float_from_string("3.") };
///
/// assert_eq!(f64_val.print_to_string().to_string(), "double 3.000000e+00");
///
/// let f64_val = f64_type.const_float_from_string("3");
/// let f64_val = unsafe { f64_type.const_float_from_string("3") };
///
/// assert_eq!(f64_val.print_to_string().to_string(), "double 3.000000e+00");
///
/// let f64_val = f64_type.const_float_from_string("");
///
/// assert_eq!(f64_val.print_to_string().to_string(), "double 0.000000e+00");
///
/// let f64_val = f64_type.const_float_from_string("3.asd");
/// let f64_val = unsafe { f64_type.const_float_from_string("3.asd") };
///
/// assert_eq!(f64_val.print_to_string().to_string(), "double 0x7FF0000000000000");
/// ```
pub fn const_float_from_string(self, slice: &str) -> FloatValue<'ctx> {
unsafe {
pub unsafe fn const_float_from_string(self, slice: &str) -> FloatValue<'ctx> {
assert!(!slice.is_empty());

unsafe {
FloatValue::new(LLVMConstRealOfStringAndSize(
self.as_type_ref(),
slice.as_ptr() as *const ::libc::c_char,
Expand Down
18 changes: 8 additions & 10 deletions tests/all/test_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -871,26 +871,24 @@ fn test_value_from_string() {

// Floats
let f64_type = context.f64_type();
let f64_val = f64_type.const_float_from_string("3.6");
let f64_val = unsafe { f64_type.const_float_from_string("3.6") };

assert_eq!(f64_val.print_to_string().to_string(), "double 3.600000e+00");

let f64_val = f64_type.const_float_from_string("3.");
let f64_val = unsafe { f64_type.const_float_from_string("3.") };

assert_eq!(f64_val.print_to_string().to_string(), "double 3.000000e+00");

let f64_val = f64_type.const_float_from_string("3");
let f64_val = unsafe { f64_type.const_float_from_string("3") };

assert_eq!(f64_val.print_to_string().to_string(), "double 3.000000e+00");

let f64_val = f64_type.const_float_from_string("");

assert_eq!(f64_val.print_to_string().to_string(), "double 0.000000e+00");

// TODO: We should return a Result that returns Err here.
//let f64_val = f64_type.const_float_from_string("3.asd");
// TODO: We should return a Result that returns Err here. This would require
// us to implement manual validation of the input to assert it matched LLVM's
// expected format.
// let f64_val = f64_type.const_float_from_string("3.asd");
//
//assert_eq!(f64_val.print_to_string().to_string(), "double 0x7FF0000000000000");
// assert_eq!(f64_val.print_to_string().to_string(), "double 0x7FF0000000000000");
}

#[test]
Expand Down

0 comments on commit 5d65484

Please sign in to comment.