Skip to content
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

Cheat around the unsafe_code lint in the select macro #430

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions crossbeam-channel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ mod waker;
pub mod internal {
pub use select::SelectHandle;
pub use select::{select, select_timeout, try_select};
pub use select_macro::unsafe_transmute;
}

pub use channel::{after, never, tick};
Expand Down
34 changes: 20 additions & 14 deletions crossbeam-channel/src/select_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,7 @@ macro_rules! crossbeam_channel_internal {
let _oper = $crate::internal::select(&mut $sel);

// Erase the lifetime so that `sel` can be dropped early even without NLL.
#[allow(unsafe_code)]
unsafe { ::std::mem::transmute(_oper) }
$crate::internal::unsafe_transmute(_oper)
};

crossbeam_channel_internal! {
Expand All @@ -852,8 +851,7 @@ macro_rules! crossbeam_channel_internal {
let _oper = $crate::internal::try_select(&mut $sel);

// Erase the lifetime so that `sel` can be dropped early even without NLL.
#[allow(unsafe_code)]
unsafe { ::std::mem::transmute(_oper) }
$crate::internal::unsafe_transmute(_oper)
};

match _oper {
Expand Down Expand Up @@ -883,8 +881,7 @@ macro_rules! crossbeam_channel_internal {
let _oper = $crate::internal::select_timeout(&mut $sel, $timeout);

// Erase the lifetime so that `sel` can be dropped early even without NLL.
#[allow(unsafe_code)]
unsafe { ::std::mem::transmute(_oper) }
$crate::internal::unsafe_transmute(_oper)
};

match _oper {
Expand Down Expand Up @@ -922,13 +919,12 @@ macro_rules! crossbeam_channel_internal {
) => {{
match $r {
ref _r => {
#[allow(unsafe_code)]
let $var: &$crate::Receiver<_> = unsafe {
let $var: &$crate::Receiver<_> = {
let _r: &$crate::Receiver<_> = _r;

// Erase the lifetime so that `sel` can be dropped early even without NLL.
unsafe fn unbind<'a, T>(x: &T) -> &'a T {
::std::mem::transmute(x)
fn unbind<'a, T>(x: &T) -> &'a T {
$crate::internal::unsafe_transmute(x)
}
unbind(_r)
};
Expand All @@ -955,13 +951,12 @@ macro_rules! crossbeam_channel_internal {
) => {{
match $s {
ref _s => {
#[allow(unsafe_code)]
let $var: &$crate::Sender<_> = unsafe {
let $var: &$crate::Sender<_> = {
let _s: &$crate::Sender<_> = _s;

// Erase the lifetime so that `sel` can be dropped early even without NLL.
unsafe fn unbind<'a, T>(x: &T) -> &'a T {
::std::mem::transmute(x)
fn unbind<'a, T>(x: &T) -> &'a T {
$crate::internal::unsafe_transmute(x)
}
unbind(_s)
};
Expand Down Expand Up @@ -1212,3 +1207,14 @@ macro_rules! select {
)
};
}

/// Same as `std::mem::transmute`, except the function is not marked with `unsafe`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Pedantically speaking, undefined behavior may be invoked because of the absence of unsafe. Not that I'm opposing to this change, but how about saying it's a HACK?

///
/// If the user crate contains `#![forbid(unsafe_code)]`, we must make sure the `select!` macro
/// does not emit any unsafe code or else it won't be usable. Since the macro does have some unsafe
/// code, we need to cheat around the lint by using this "safe" transmute function.
pub fn unsafe_transmute<T, U>(t: T) -> U {
let u = unsafe { std::mem::transmute_copy(&t) };
std::mem::forget(t);
u
}
2 changes: 1 addition & 1 deletion crossbeam-channel/tests/select_macro.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Tests for the `select!` macro.

#![deny(unsafe_code)]
#![forbid(unsafe_code)]

#[macro_use]
extern crate crossbeam_channel;
Expand Down