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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
use either::Either;
use proc_macro as pm;
use proc_macro2 as pm2;
use quote::quote;
use syn::parse_macro_input;
use syn::spanned::Spanned;

mod kw {
    syn::custom_keyword!(equal);
    syn::custom_keyword!(equal_with);
    syn::custom_keyword!(methods);
    syn::custom_keyword!(model);
    syn::custom_keyword!(post);
    syn::custom_keyword!(pre);
    syn::custom_keyword!(tested);
    syn::custom_keyword!(type_parameters);
}

#[allow(clippy::enum_variant_names)]
enum PassingMode {
    ByValue,
    ByRef,
    ByRefMut,
}

struct Argument {
    name: syn::Ident,
    ty: syn::Type,
    passing_mode: PassingMode,
}

struct Method {
    name: syn::Ident,
    // self_mut: bool,
    inputs: Vec<Argument>,
    process_result: Option<syn::Path>,
    // output: syn::Type
}

impl syn::parse::Parse for Method {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let method_item: syn::TraitItemMethod = input.parse()?;

        if let Some(ref defaultness) = method_item.default {
            return Err(syn::Error::new(defaultness.span(), "unexpected `default`"));
        }
        if let Some(ref constness) = method_item.sig.constness {
            return Err(syn::Error::new(constness.span(), "unexpected `const`"));
        }
        if let Some(ref asyncness) = method_item.sig.asyncness {
            return Err(syn::Error::new(asyncness.span(), "unexpected `async`"));
        }
        if let Some(ref unsafety) = method_item.sig.unsafety {
            return Err(syn::Error::new(unsafety.span(), "unexpected `unsafe`"));
        }

        let (receivers, args) = method_item
            .sig
            .inputs
            .iter()
            .map(|input| match input {
                syn::FnArg::Receiver(receiver) => Either::Left(receiver),
                syn::FnArg::Typed(syn::PatType { ty, pat, .. }) => {
                    let ident = match **pat {
                        syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => ident.clone(),
                        ref pat => syn::Ident::new("_", pat.span()),
                    };
                    match **ty {
                        syn::Type::Reference(syn::TypeReference {
                            ref mutability,
                            ref elem,
                            ..
                        }) => Either::Right(Argument {
                            name: ident,
                            ty: (**elem).clone(),
                            passing_mode: if mutability.is_some() {
                                PassingMode::ByRefMut
                            } else {
                                PassingMode::ByRef
                            },
                        }),
                        ref ty => Either::Right(Argument {
                            name: ident,
                            ty: ty.clone(),
                            passing_mode: PassingMode::ByValue,
                        }),
                    }
                }
            })
            .partition::<Vec<_>, _>(Either::is_left);

        let receivers: Vec<_> = receivers.into_iter().filter_map(Either::left).collect();
        let args: Vec<_> = args.into_iter().filter_map(Either::right).collect();

        let receiver = receivers.first();
        if let Some(receiver) = receiver {
            if receiver.reference.is_none() {
                return Err(syn::Error::new(
                    receiver.span(),
                    "unexpected by-value receiver",
                ));
            }
        } else {
            return Err(syn::Error::new(
                method_item.span(),
                "unexpected method with no receiver",
            ));
        }

        Ok(Self {
            name: method_item.sig.ident,
            // self_mut: receiver.map_or(false, |r| r.mutability.is_some()),
            process_result: None,
            inputs: args,
            /*output: match method_item.sig.output {
                syn::ReturnType::Default =>
                    syn::parse_str("()").unwrap(),
                syn::ReturnType::Type(_, typ) =>
                    (*typ).clone()
            }*/
        })
    }
}

struct Specification {
    model: syn::Path,
    tested: syn::Path,
    type_params: Vec<syn::TypeParam>,
    methods: Vec<Method>,
    post: Vec<syn::Stmt>,
    pre: Vec<syn::Stmt>,
}

impl syn::parse::Parse for Specification {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        use syn::{braced, parenthesized, Token};

        let mut model: Option<syn::Path> = None;
        let mut tested: Option<syn::Path> = None;
        let mut type_params: Vec<syn::TypeParam> = vec![];
        let mut methods: Vec<Method> = vec![];
        let mut post: Vec<syn::Stmt> = vec![];
        let mut pre: Vec<syn::Stmt> = vec![];

        while !input.is_empty() {
            let lookahead = input.lookahead1();
            if lookahead.peek(kw::model) {
                let _: kw::model = input.parse()?;
                let _: Token![=] = input.parse()?;
                model = Some(input.parse()?);
            } else if lookahead.peek(kw::tested) {
                let _: kw::tested = input.parse()?;
                let _: Token![=] = input.parse()?;
                tested = Some(input.parse()?);
            } else if lookahead.peek(kw::type_parameters) {
                let _: kw::type_parameters = input.parse()?;
                let _: Token![=] = input.parse()?;
                let generics: syn::Generics = input.parse()?;
                type_params = generics.type_params().cloned().collect();
            } else if lookahead.peek(kw::methods) {
                let outer;
                let mut inner;
                let _: kw::methods = input.parse()?;
                braced!(outer in input);

                while !outer.is_empty() {
                    let lookahead = outer.lookahead1();
                    let process = if lookahead.peek(kw::equal) {
                        let _: kw::equal = outer.parse()?;
                        None
                    } else if lookahead.peek(kw::equal_with) {
                        let _: kw::equal_with = outer.parse()?;
                        let path;
                        parenthesized!(path in outer);
                        Some(path.parse()?)
                    } else {
                        return Err(lookahead.error());
                    };

                    braced!(inner in outer);
                    while !inner.is_empty() {
                        let mut method: Method = inner.parse()?;
                        method.process_result = process.clone();
                        methods.push(method);
                    }
                }
            } else if lookahead.peek(kw::post) {
                let inner;
                let _: kw::post = input.parse()?;
                braced!(inner in input);
                while !inner.is_empty() {
                    post.push(inner.parse()?);
                }
            } else if lookahead.peek(kw::pre) {
                let inner;
                let _: kw::pre = input.parse()?;
                braced!(inner in input);
                while !inner.is_empty() {
                    pre.push(inner.parse()?);
                }
            } else {
                return Err(lookahead.error());
            }

            if input.peek(Token![,]) {
                let _: Token![,] = input.parse()?;
            }
        }

        let model = match model {
            Some(model) => model,
            None => return Err(input.error("missing `model`")),
        };

        let tested = match tested {
            Some(tested) => tested,
            None => return Err(input.error("missing `tested`")),
        };

        Ok(Self {
            model,
            tested,
            type_params,
            methods,
            post,
            pre,
        })
    }
}

impl quote::ToTokens for Method {
    fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
        use pm2::{Delimiter, Group, Punct, Spacing};
        use quote::TokenStreamExt;

        tokens.append(self.name.clone());

        if !self.inputs.is_empty() {
            let mut fields = pm2::TokenStream::new();
            for input in &self.inputs {
                fields.append(input.name.clone());
                fields.append(Punct::new(':', Spacing::Joint));
                input.ty.to_tokens(&mut fields);
                fields.append(Punct::new(',', Spacing::Joint));
            }
            tokens.append(Group::new(Delimiter::Brace, fields));
        }
    }
}

struct MethodTest<'s> {
    method: &'s Method,
    compare: bool,
}

impl<'s> quote::ToTokens for MethodTest<'s> {
    #[allow(clippy::too_many_lines)]
    fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
        let args: Vec<_> = self
            .method
            .inputs
            .iter()
            .map(|input| {
                let input_name = &input.name;
                match input.passing_mode {
                    PassingMode::ByValue => quote! { #input_name.clone() },
                    PassingMode::ByRef => quote! { #input_name },
                    PassingMode::ByRefMut => quote! { &mut *#input_name },
                }
            })
            .collect();

        let method_name = &self.method.name;

        let keys: Vec<_> = self.method.inputs.iter().map(|input| &input.name).collect();
        let pattern = if keys.is_empty() {
            quote! { Op::#method_name }
        } else {
            quote! { Op::#method_name { #(ref #keys),* } }
        };

        let process_tested_ret_value = self
            .method
            .process_result
            .as_ref()
            .map(|p| quote! { #p(tested_ret_value) })
            .unwrap_or(quote! { tested_ret_value });

        if self.compare {
            let process_model_ret_value = self
                .method
                .process_result
                .as_ref()
                .map(|p| quote! { #p(model_ret_value) })
                .unwrap_or(quote! { model_ret_value });
            tokens.extend(quote! {
                #pattern => {
                    enum Outcome {
                        Equal,
                        #[cfg(not(fuzzing_debug))]
                        Unequal,
                        #[cfg(fuzzing_debug)]
                        Unequal {
                            model_ret_value_debug: String,
                            tested_ret_value_debug: String,
                        },
                    }

                    enum WhichFailed {
                        None(Outcome),
                        First,
                        Second,
                    }

                    struct GalaxyBrain<'a> {
                        value: WhichFailed,
                        to_update: &'a mut WhichFailed,
                    }

                    impl<'a> Drop for GalaxyBrain<'a> {
                        fn drop(&mut self) {
                            std::mem::swap(self.to_update, &mut self.value);
                        }
                    }

                    let mut f = WhichFailed::First;

                    {
                        let mut guard = GalaxyBrain {
                            value: WhichFailed::First,
                            to_update: &mut f,
                        };

                        let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                            let model_ret_value = model.#method_name(#(#args),*);
                            guard.value = WhichFailed::Second;
                            let tested_ret_value = tested.#method_name(#(#args),*);

                            let model_ret_value = #process_model_ret_value;
                            let tested_ret_value = #process_tested_ret_value;

                            let outcome = if model_ret_value == tested_ret_value {
                                Outcome::Equal
                            } else {
                                #[cfg(fuzzing_debug)]
                                {
                                    Outcome::Unequal {
                                        model_ret_value_debug: format!("{:?}", model_ret_value),
                                        tested_ret_value_debug: format!("{:?}", tested_ret_value),
                                    }
                                }
                                #[cfg(not(fuzzing_debug))]
                                Outcome::Unequal
                            };
                            guard.value = WhichFailed::None(outcome);
                        }));
                    }

                    match f {
                        WhichFailed::None(outcome) => {
                            #[cfg(fuzzing_debug)]
                            if let Outcome::Unequal { model_ret_value_debug, tested_ret_value_debug } = outcome {
                                rutenspitz::panic!(
                                    "The return values aren't equal: `{}` != `{}`",
                                    model_ret_value_debug,
                                    tested_ret_value_debug
                                );
                            }
                            #[cfg(not(fuzzing_debug))]
                            if let Outcome::Unequal = outcome {
                                rutenspitz::panic!("The return values aren't equal");
                            }
                        }
                        WhichFailed::First => {
                            // First paniced, see if the second one also does
                            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                                let _ = tested.#method_name(#(#args),*);
                            }));
                            if result.is_ok() {
                                rutenspitz::panic!("Implementation did not panic while the model did");
                            }
                        }
                        WhichFailed::Second => {
                            rutenspitz::panic!("Implementation panicked while the model did not");
                        }
                    }
                }
            });
        } else {
            tokens.extend(quote! {
                #pattern => {
                    let _ = tested.#method_name(#(#args),*);
                }
            });
        }
    }
}

struct OperationEnum<'s> {
    spec: &'s Specification,
}

impl<'s> quote::ToTokens for OperationEnum<'s> {
    #[allow(clippy::cognitive_complexity)]
    fn to_tokens(&self, tokens: &mut pm2::TokenStream) {
        let type_params_with_bounds = &self.spec.type_params;
        let type_params: Vec<_> = type_params_with_bounds
            .iter()
            .map(|tp| tp.ident.clone())
            .collect();

        let model = &self.spec.model;
        let tested = &self.spec.tested;
        let variants = &self.spec.methods;

        let comp_method_tests: Vec<_> = self
            .spec
            .methods
            .iter()
            .map(|method| MethodTest {
                method,
                compare: true,
            })
            .collect();

        let method_tests: Vec<_> = self
            .spec
            .methods
            .iter()
            .map(|method| MethodTest {
                method,
                compare: false,
            })
            .collect();

        let format_calls: Vec<_> = self
            .spec
            .methods
            .iter()
            .map(|method| {
                let args: Vec<_> = method
                    .inputs
                    .iter()
                    .map(|input| match input.passing_mode {
                        PassingMode::ByValue => "{:?}",
                        PassingMode::ByRef => "&{:?}",
                        PassingMode::ByRefMut => "&mut {:?}",
                    })
                    .collect();

                let method_name = &method.name;
                let format_str = format!("v.{}({});", method_name, args.join(", "));
                let keys: Vec<_> = method.inputs.iter().map(|input| &input.name).collect();
                let pattern = if keys.is_empty() {
                    quote! { Op::#method_name }
                } else {
                    quote! { Op::#method_name { #(#keys),* } }
                };

                quote! { #pattern =>
                    write!(f, #format_str, #(#keys),*)
                }
            })
            .collect();

        let post = &self.spec.post;
        let pre = &self.spec.pre;

        tokens.extend(quote! {
            #[allow(non_camel_case_types)]
            #[derive(rutenspitz::derive::Arbitrary, rutenspitz::derive::IntoStaticStr, Clone, Debug, PartialEq)]
            pub enum Op<#(#type_params_with_bounds),*> {
                #(#variants),*
            }

            impl<#(#type_params_with_bounds),*> std::fmt::Display for Op<#(#type_params),*> {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    match self {
                        #(#format_calls),*
                    }
                }
            }

            impl<#(#type_params_with_bounds),*> Op<#(#type_params),*> {
                pub fn execute(self, tested: &mut #tested) {
                    match &self {
                        #(#method_tests),*
                    }
                }

                pub fn execute_and_compare(self, model: &mut #model, tested: &mut #tested) {
                    #[cfg(not(fuzzing_debug))]
                    rutenspitz::lazy_static::initialize(&rutenspitz::NON_DEBUG_PANIC_HOOK);

                    let op_name: &'static str = From::from(&self);
                    #(#pre)*
                    match &self {
                        #(#comp_method_tests),*
                    }
                    #(#post)*
                }

                #[inline(always)]
                pub fn append_to_trace(&self, trace: &mut String) {
                    #[cfg(fuzzing_debug)]
                    trace.push_str(&format!("{}\n", self.to_string()));
                }
            }
        })
    }
}

#[proc_macro]
pub fn arbitrary_stateful_operations(input: pm::TokenStream) -> pm::TokenStream {
    let parsed_spec = parse_macro_input!(input as Specification);

    let operation_enum = OperationEnum { spec: &parsed_spec };

    let output = quote! {
        mod op {
            use super::*;
            #operation_enum
        }
    };

    output.into()
}