Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,15 +842,11 @@ macro_rules! s(
}
};
// empty call, i.e. `s![]`
(@parse ::core::marker::PhantomData::<$crate::Ix0>, ::core::marker::PhantomData::<$crate::Ix0>, []) => {
(@parse $in_dim:expr, $out_dim:expr, []) => {
{
#[allow(unsafe_code)]
unsafe {
$crate::SliceInfo::new_unchecked(
[],
::core::marker::PhantomData::<$crate::Ix0>,
::core::marker::PhantomData::<$crate::Ix0>,
)
$crate::SliceInfo::new_unchecked([], $in_dim, $out_dim)
Copy link
Member

Choose a reason for hiding this comment

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

what's the impact on the unsafe code block here?

Copy link
Author

@CAD97 CAD97 Aug 13, 2022

Choose a reason for hiding this comment

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

It's still necessary, as SliceInfo::new_unchecked is unsafe.

Technically, this means that someone using the macro could write

s! { @parse
    {
        // statements in an unsafe context
        ::core::marker::PhantomData::<::ndarray::Ix0>
    },
    ::core::marker::PhantomData<::ndaray::Ix0>,
    []
}

to get an unsafe context without writing unsafe, but this is an internal rule, so that's not too worrisome. However, it is reasonable to evaluate the arguments outside of the unsafe block just for thoroughness.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for bringing this up. Some of the other @parse arms had safety issues if called directly. I've created #1196 to fix that. With that change, we can change this PR to modify the "empty case" arm to the following:

    // empty call, i.e. `s![]`
    (@parse $in_dim:expr, $out_dim:expr, []) => {
        {
            debug_assert_eq!($in_dim, ::core::marker::PhantomData::<$crate::Ix0>);
            debug_assert_eq!($out_dim, ::core::marker::PhantomData::<$crate::Ix0>);
            (
                [],
                ::core::marker::PhantomData::<$crate::Ix0>,
                ::core::marker::PhantomData::<$crate::Ix0>,
            )
        }
    };

This is safe, since it always expands to valid values for the SliceInfo::new_unchecked call (which is in the ($($t:tt)*) arm with #1196). It has debug assertions to ensure that $in_dim and $out_dim are the expected values. (They'll always be the expected values, unless the user calls s![@parse ...] directly.

}
}
};
Expand Down