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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
extern crate proc_macro;

use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::parse::{Error, Parse, ParseStream};
use syn::spanned::Spanned;
use syn::token::{Brace, Bracket, Paren};
use syn::{
    self, Attribute, Data, DeriveInput, Field, Fields, GenericParam, Ident, Path, Token, Visibility,
};

/// Derives neutralizing traits.
///
/// There are two ways to use this procedural macro attribute.
///
/// ### `#[inert::neutralize(as Self)]`
///
/// Implements `NeutralizeUnsafe` with `type Output = Self;`. Given that this
/// can type check if and only if `Self` is `Sync`, the traits `Neutralize`
/// and `NeutralizeMut` are also automatically implemented.
///
/// ### `#[inert::neutralize(as vis? unsafe InertFoo)]`
///
/// Implements `NeutralizeUnsafe` with `type Output = InertFoo;`. `InertFoo` is
/// a wrapper generated by the macro itself, its visibility is controlled
/// by `vis`. A getter method named is generated for each field adorned with
/// `#[inert::field(vis? ident?)]`, if `ident` is missing, the getter is given
/// the same name as the field itself.
///
/// Given the following `Node` type:
///
/// ```ignore
/// #[inert::neutralize(as pub(crate) unsafe InertNode)]
/// struct Node {
///     #[inert::field(pub(crate) tag)]
///     name: String,
///     #[inert::field(pub(crate))]
///     attributes: Vec<Attribute>,
///     #[inert::field]
///     children: RefCell<Vec<Node>>,
/// }
/// ```
///
/// The following code will be generated:
///
/// ```ignore
/// pub(crate) struct InertNode {
///     value: inert::Neutralized<Node>,
/// }
///
/// unsafe impl NeutralizeUnsafe for Node {
///     type Output = InertNode;
///
///     unsafe fn neutralize_unsafe(&self) -> &InertNode {
///         &*(self as *const Self as *const Self::Output)
///     }
/// }
///
/// impl InertNode {
///     pub(crate) fn tag(&self) -> &Inert<String> {
///         unsafe { Inert::new_unchecked(&self.name) }
///     }
///
///     pub(crate) fn attributes(&self) -> &Inert<Vec<Attribute>> {
///         unsafe { Inert::new_unchecked(&self.attributes) }
///     }
///
///     fn children(&self) -> &Inert<RefCell<Vec<Node>>> {
///         unsafe { Inert::new_unchecked(&self.children) }
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn neutralize(
    attr: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let attr = syn::parse_macro_input!(attr as NeutralizeAttr);
    let input = syn::parse_macro_input!(item as DeriveInput);
    match attr {
        NeutralizeAttr::AsSelf(self_type) => neutralize_as_self(self_type, input),
        NeutralizeAttr::AsWrapper(wrapper) => {
            neutralize_as_wrapper(wrapper, input).unwrap_or_else(|e| e.to_compile_error().into())
        },
    }.into()
}

enum NeutralizeAttr {
    AsSelf(Token![Self]),
    AsWrapper(Wrapper),
}

impl Parse for NeutralizeAttr {
    fn parse(input: ParseStream) -> Result<Self, Error> {
        input.parse::<Token![as]>()?;
        if input.peek(Token![Self]) {
            return input.parse::<Token![Self]>().map(NeutralizeAttr::AsSelf);
        }
        input.parse().map(NeutralizeAttr::AsWrapper)
    }
}

struct Wrapper {
    vis: Visibility,
    name: Ident,
}

impl Parse for Wrapper {
    fn parse(input: ParseStream) -> Result<Self, Error> {
        let vis = input.parse()?;
        input.parse::<Token![unsafe]>()?;
        let name = input.parse()?;
        Ok(Self { vis, name })
    }
}

// FIXME(nox): https://github.com/dtolnay/syn/issues/589
struct AttrArgument<T> {
    argument: T,
}

// FIXME(nox): https://github.com/dtolnay/syn/issues/589
impl<T> Parse for AttrArgument<T>
where
    T: Parse,
{
    fn parse(input: ParseStream) -> Result<Self, Error> {
        let content;
        if input.peek(Paren) {
            syn::parenthesized!(content in input);
        } else if input.peek(Brace) {
            syn::braced!(content in input);
        } else if input.peek(Bracket) {
            syn::bracketed!(content in input);
        } else {
            return Ok(Self { argument: syn::parse2(quote! {})? });
        }
        let argument = T::parse(&content)?;
        Ok(Self { argument })
    }
}

struct FieldAttr {
    vis: Visibility,
    name: Option<Ident>,
}

impl Parse for FieldAttr {
    fn parse(input: ParseStream) -> Result<Self, Error> {
        let vis = input.parse()?;
        let name = input.parse()?;
        Ok(Self { vis, name })
    }
}

fn neutralize_as_self(self_type: Token![Self], input: DeriveInput) -> TokenStream {
    let type_name = &input.ident;
    let (impl_gen, ty_gen, where_) = input.generics.split_for_impl();

    // This is supposed to improve "trait bound is not satisfied" errors
    // in case Self is !Sync, but this does not actually work yet.
    //
    // https://github.com/rust-lang/rust/issues/41817
    let output = quote_spanned! {self_type.span()=> type Output = Self; };

    quote! {
        #input

        unsafe impl #impl_gen ::inert::Neutralize for #type_name #ty_gen #where_ {}
        unsafe impl #impl_gen ::inert::NeutralizeMut for #type_name #ty_gen #where_ {}

        unsafe impl #impl_gen ::inert::NeutralizeUnsafe for #type_name #ty_gen #where_ {
            #output

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &Self { self }
        }
    }
}

fn neutralize_as_wrapper(wrapper: Wrapper, mut input: DeriveInput) -> Result<TokenStream, Error> {
    check_params(&input)?;

    let methods = inert_methods(struct_fields_mut(&mut input)?)?;

    let Wrapper { vis, name } = wrapper;
    let type_name = &input.ident;
    let (impl_gen, ty_gen, where_) = input.generics.split_for_impl();

    let wrapper = quote_spanned! {name.span()=>
        #vis struct #name #ty_gen #where_ {
            value: ::inert::Neutralized<#type_name #ty_gen>,
        }
    };

    Ok(quote! {
        #input

        #wrapper

        unsafe impl #impl_gen ::inert::NeutralizeUnsafe for #type_name #ty_gen #where_ {
            type Output = #name #ty_gen;

            #[inline]
            unsafe fn neutralize_unsafe(&self) -> &#name #ty_gen {
                &*(self as *const Self as *const Self::Output)
            }
        }

        impl #impl_gen #name #ty_gen #where_ {
            #methods
        }
    })
}

fn check_params(input: &DeriveInput) -> Result<(), Error> {
    for param in &input.generics.params {
        if let GenericParam::Lifetime(_) = *param {
            continue;
        }
        return Err(Error::new(
            param.span(),
            "cannot automatically neutralize a parameterized type",
        ));
    }
    Ok(())
}

fn struct_fields_mut(input: &mut DeriveInput) -> Result<impl Iterator<Item = &mut Field>, Error> {
    // FIXME(nox): https://github.com/rust-lang/rust/issues/58910
    let span = input.span();
    if let Data::Struct(ref mut data_struct) = input.data {
        if let Fields::Named(ref mut fields_named) = data_struct.fields {
            return Ok(fields_named.named.iter_mut());
        }
    }
    Err(Error::new(span, "only structs can be automatically neutralized"))
}

#[allow(dead_code)]
fn inert_methods<'input>(
    fields: impl Iterator<Item = &'input mut Field>,
) -> Result<TokenStream, Error> {
    let mut methods = quote! {};

    for field in fields {
        let attr = match extract_inert_field(field)? {
            Some(attr) => attr,
            None => continue,
        };

        let field_name = field.ident.as_ref().expect("named field");
        let ty = &field.ty;
        let FieldAttr { vis, name } = attr;
        let getter_name = name.as_ref().unwrap_or(field_name);

        methods.extend(quote_spanned! {field.ty.span()=>
            #[allow(unsafe_code)]
            #[inline]
            #vis fn #getter_name(&self) -> &inert::Inert<#ty> {
                unsafe { inert::Inert::new_unchecked(&self.value.as_ref().#field_name) }
            }
        });
    }

    Ok(methods)
}

fn extract_inert_field(field: &mut Field) -> Result<Option<FieldAttr>, Error> {
    let (pos, attr) = if let Some(pos) = find_inert_field_attr(&field.attrs) {
        (pos, field.attrs.remove(pos))
    } else {
        return Ok(None);
    };

    let attr = syn::parse2::<AttrArgument<FieldAttr>>(attr.tts)?.argument;

    if let Some(dupe_pos) = find_inert_field_attr(&field.attrs[pos..]) {
        return Err(Error::new(
            field.attrs[pos..][dupe_pos].span(),
            "multiple #[inert::field] attributes found on the same field",
        ));
    }

    Ok(Some(attr))
}

fn find_inert_field_attr<'input>(
    attrs: impl IntoIterator<Item = &'input Attribute>,
) -> Option<usize> {
    attrs.into_iter().position(|attr| is_simple_path(&attr.path, &["inert", "field"]))
}

fn is_simple_path(path: &Path, idents: &[&str]) -> bool {
    let mut segments = path.segments.iter();
    let mut idents = idents.iter();
    segments.by_ref().zip(idents.by_ref()).all(|(s, i)| s.ident == *i && s.arguments.is_empty())
        && segments.next().is_none()
        && idents.next().is_none()
}