Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd debug asserts to some unsafe functions #51713
Comments
This comment has been minimized.
This comment has been minimized.
I think we would need at least three This gets out of hand quickly. Given
which makes it 2^6 = 64 different Honestly, we need something better. In my opinion, the options that a user specifies for its crate should apply to the whole dependency graph, including the |
This comment has been minimized.
This comment has been minimized.
andrewhickman
commented
Jun 26, 2018
•
|
I disagree that code that, for example, calls This absolutely makes sense for unreachable_unchecked since the invariant can be checked with no 'false positives' |
This comment has been minimized.
This comment has been minimized.
|
The idea is to explicitly construct slice which will cover valid memory region before indexing it, e.g. // method is unsafe because slice can contain uninitialized data
unsafe fn get_cap_slice(&self) -> &[T] {
slice::from_raw_parts(self.buf.ptr, self.buf.cap)
}Yes, this change will require more steps for some use-cases, but arguably it's worth it, as many consider |
This comment has been minimized.
This comment has been minimized.
PaulGrandperrin
commented
Dec 18, 2018
•
|
Hi, I just want to say that I came across the same issue: and I was thinking, wouldn't it be possible to do something like that for #[macro_export]
macro_rules! unreachable_unchecked {
() => ({
#[cfg(debug_assertions)]
panic!("internal error: entered unreachable code")
#[cfg(not(debug_assertions))]
std::hint::unreachable_unchecked()
});
($msg:expr) => ({
unreachable_unchecked!("{}", $msg)
});
($msg:expr,) => ({
unreachable_unchecked!($msg)
});
($fmt:expr, $($arg:tt)*) => ({
#[cfg(debug_assertions)]
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
#[cfg(not(debug_assertions))]
std::hint::unreachable_unchecked()
});
}As a macro, it would be expanded at crate compilation time and so would respect the (this code is inspired by the |
This comment has been minimized.
This comment has been minimized.
"crate compilation time" is the time that you're compiling std, not the downstream crate. |
newpavlov commentedJun 22, 2018
•
edited
The following functions have restrictions which (ideally) should be respected:
get_uncheckedget_unchecked_mutslice_uncheckedslice_mut_uncheckedunreachable_uncheckedIt would be nice to check these restrictions with debug asserts. The main blockers:
cargoto switch between them depending on a compilation profile.src/liballoc/vec.rs). Probably it should be rewritten in a more "correct" fashion.See internals thread for additional discussion.