1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#![doc(test(attr(
    deny(warnings),
    allow(unused),
    deny(unused_unsafe),
)))]
#![cfg_attr(feature = "nightly",
    feature(external_doc),
)]
#![cfg_attr(feature = "nightly",
    doc(include = "../README.md")
)]

#[cfg(not(all(test, feature = "unit-tests")))]
extern crate proc_macro;
#[cfg(not(all(test, feature = "unit-tests")))]
use ::proc_macro::TokenStream;
#[cfg(not(all(test, feature = "unit-tests")))]
use ::syn::parse;
#[cfg(all(test, feature = "unit-tests"))]
use ::proc_macro2::TokenStream;
#[cfg(all(test, feature = "unit-tests"))]
use ::syn::parse2 as parse;

use ::proc_macro2::{
    Span,
};
use ::quote::{
    ToTokens,
};
use ::syn::*;

// Reimplement parse_macro_input to use the imported `parse`
// function. This way parse_macro_input will parse a TokenStream2 when
// unit-testing.
macro_rules! parse_macro_input {
    (
        $token_stream:ident as $T:ty
    ) => (
        match parse::<$T>($token_stream) {
            | Ok(data) => data,
            | Err(err) => {
                return TokenStream::from(err.to_compile_error());
            }
        }
    );

    (
        $token_stream:ident
    ) => (
        parse_macro_input!($token_stream as _)
    );
}

#[cfg_attr(feature = "nightly",
    doc(include = "docs/require_unsafe_in_body.md")
)]
#[proc_macro_attribute] pub
fn require_unsafe_in_body (
    attrs: TokenStream,
    input: TokenStream,
) -> TokenStream
{
    let _: parse::Nothing = parse_macro_input!(attrs);
    ::func_wrap::parse_and_func_wrap_with(input, edit_function_in_place)
        .map_or_else(
            |err| err.to_compile_error(),
            |item| {
                if let Item::Fn(_) = item {
                    item.into_token_stream()
                } else {
                    Error::new(Span::call_site(), "\
                        `#[require_unsafe_in_body]` must be applied to \
                        a function.\
                    ").to_compile_error()
                }
            },
        )
        .into()
}

#[cfg_attr(feature = "nightly",
    doc(include = "docs/require_unsafe_in_bodies.md")
)]
#[proc_macro_attribute] pub
fn require_unsafe_in_bodies (
    attrs: TokenStream,
    input: TokenStream,
) -> TokenStream
{
    let _: parse::Nothing = parse_macro_input!(attrs);
    ::func_wrap::parse_and_func_wrap_with(input, edit_function_in_place)
        .map_or_else(
            |err| err.to_compile_error(),
            |item| {
                if let Item::Trait(_) | Item::Impl(_) = item {
                    item.into_token_stream()
                } else {
                    Error::new(Span::call_site(), "\
                    `#[require_unsafe_in_bodies]` must be applied to \
                    an `impl` block or a `trait` definition.\
                    ").to_compile_error()
                }
            },
        )
        .into()
}

fn edit_function_in_place (
    func: &'_ mut ImplItemMethod,
    wrapped_func_call: Option<::func_wrap::WrappedFuncCall<'_>>,
) -> Result<()>
{
    let mut wrapped_func_call =
        if let Some(it) = wrapped_func_call {
            it
        } else {
            return Err(Error::new(Span::call_site(), "\
                Missing `#[require_unsafe_in_bodies]` on the enscoping \
                `trait` or `impl` block.
            "));
        }
    ;
    func.block = if func.sig.unsafety.is_none() {
        // If the function is not tagged `unsafe`, there is already no `unsafe`
        // hygiene in the function's body, so there is nothing to do.
        func.sig = wrapped_func_call.sig; // Unrename the function params.
        wrapped_func_call.block // Get back the original function's body.
    } else {
        // Otherwise:
        // 1 - Mark the inner function (the one with the original function's
        //     body) as non-`unsafe`, so that `unsafe { ... }` blocks are
        //     required inside.
        wrapped_func_call.sig.unsafety = None;
        // 2 - In exchange, expect a dummy `unsafe` parameter
        wrapped_func_call.sig.inputs.push(parse_quote!(
            _: r#unsafe
        ));
        wrapped_func_call.call_site_args.push(parse_quote!( r#unsafe ));
        // 3 - Rename it so that recursive calls call the public `unsafe fn`
        //     rather than this internal one.
        let fname = &wrapped_func_call.sig.ident;
        let renamed = ::quote::format_ident!("__unsafe_{}", fname);
        wrapped_func_call.sig.ident = renamed;

        // 3 - Finally, have the public-facing function call that inner one.
        parse_quote!({
            #[allow(nonstandard_style)]
            struct r#unsafe;
            #wrapped_func_call
        })
    };
    Ok(())
}

#[cfg(all(test, feature = "unit-tests"))]
mod tests;