Skip to content

Fix unsafe blocks in s![] macro #1196

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

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Changes from all commits
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
61 changes: 29 additions & 32 deletions src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,14 +780,11 @@ macro_rules! s(
r => {
let in_dim = $crate::SliceNextDim::next_in_dim(&r, $in_dim);
let out_dim = $crate::SliceNextDim::next_out_dim(&r, $out_dim);
#[allow(unsafe_code)]
unsafe {
$crate::SliceInfo::new_unchecked(
[$($stack)* $crate::s!(@convert r, $s)],
in_dim,
out_dim,
)
}
(
[$($stack)* $crate::s!(@convert r, $s)],
in_dim,
out_dim,
)
}
}
};
Expand All @@ -797,14 +794,11 @@ macro_rules! s(
r => {
let in_dim = $crate::SliceNextDim::next_in_dim(&r, $in_dim);
let out_dim = $crate::SliceNextDim::next_out_dim(&r, $out_dim);
#[allow(unsafe_code)]
unsafe {
$crate::SliceInfo::new_unchecked(
[$($stack)* $crate::s!(@convert r)],
in_dim,
out_dim,
)
}
(
[$($stack)* $crate::s!(@convert r)],
in_dim,
out_dim,
)
}
}
};
Expand Down Expand Up @@ -844,16 +838,11 @@ macro_rules! s(
};
// empty call, i.e. `s![]`
(@parse ::core::marker::PhantomData::<$crate::Ix0>, ::core::marker::PhantomData::<$crate::Ix0>, []) => {
{
#[allow(unsafe_code)]
unsafe {
$crate::SliceInfo::new_unchecked(
[],
::core::marker::PhantomData::<$crate::Ix0>,
::core::marker::PhantomData::<$crate::Ix0>,
)
}
}
(
[],
::core::marker::PhantomData::<$crate::Ix0>,
::core::marker::PhantomData::<$crate::Ix0>,
)
};
// Catch-all clause for syntax errors
(@parse $($t:tt)*) => { compile_error!("Invalid syntax in s![] call.") };
Expand All @@ -868,12 +857,20 @@ macro_rules! s(
)
};
($($t:tt)*) => {
$crate::s![@parse
::core::marker::PhantomData::<$crate::Ix0>,
::core::marker::PhantomData::<$crate::Ix0>,
[]
$($t)*
]
{
let (indices, in_dim, out_dim) = $crate::s![@parse
::core::marker::PhantomData::<$crate::Ix0>,
::core::marker::PhantomData::<$crate::Ix0>,
[]
$($t)*
];
// Safety: The `s![@parse ...]` above always constructs the correct
// values to meet the constraints of `SliceInfo::new_unchecked`.
#[allow(unsafe_code)]
unsafe {
$crate::SliceInfo::new_unchecked(indices, in_dim, out_dim)
}
}
};
);

Expand Down