Skip to content

Added comments when using unsafe #1501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

danieldinu2030
Copy link

What changed?

Why?

To get issue #628 closer to being completely solved

@@ -133,6 +133,8 @@ pub fn clamp_vec<T: Number, const D: usize>(
///
/// The floating-point value's bit-level representation is preserved.
///
/// Using unsafe is sound because the bitwise representation of f32 fits in i32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documentation goes inside the function, on the unsafe block. The use of unsafety in the implementation is not public; the purpose of the comment is to help a developer or auditor who is looking at the implementation.

@@ -167,6 +169,8 @@ pub fn float_bits_to_int_vec<const D: usize>(v: &TVec<f32, D>) -> TVec<i32, D> {
///
/// The floating-point value's bit-level representation is preserved.
///
/// Using unsafe is sound because the bitwise representation of f32 fits in i32
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

@@ -161,6 +161,7 @@ where
lapack_check!(info);

// Copy lower triangle to upper triangle.
// Using unsafe to ensure the bounds i and j are always valid indices,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't make sense. The unsafe operation requires that the indices are in bounds; it does nothing to ensure that is actually the case. The comment should explain how we know it's the case.

@@ -196,6 +197,9 @@ pub trait CholeskyScalar: Scalar + Copy {
fn xpotri(uplo: u8, n: i32, a: &mut [Self], lda: i32, info: &mut i32);
}

/// This macro uses unsafe to manually ensure memory safety for external functions
/// For incorrectly sized and initialized matrices and arrays, undefined behavior will occur
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment seems to be arguing that the use of unsafety is incorrect. If that's true, then we should fix the unsoundness, not just declare its presence. Several similar looking instances below.



// Using unsafe to manually ensure memory safety for external function lapack_func
// Rust cannot check the slices' or raw poinnters' safety
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below, the comment must explain exactly what the safety invariants are, and how we know they're satisfied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants