Skip to content

Commit

Permalink
Merge pull request #1196 from jturner314/fix-s-unsafe
Browse files Browse the repository at this point in the history
Fix unsafe blocks in s![] macro
  • Loading branch information
bluss authored Mar 9, 2024
2 parents 58d60ee + fa195d8 commit f124a16
Showing 1 changed file with 29 additions and 32 deletions.
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

0 comments on commit f124a16

Please sign in to comment.