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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
//! Converts optional arguments to chaining style.
//!
//! Rust doesn't have optional or named arguments. This crate provides macros to convert optional arguments given by attributes to method chaining style instead.
//!
//! # Function with optional arguments
//!
//! Specify `optarg_fn` for a function with 2 arguments, the name of builder struct and terminal method. Use `#[optarg(expr)]` to give default value for an argument. `#[optarg_default]` gives default value by [`Default::default()`](https://doc.rust-lang.org/std/default/trait.Default.html).
//! ```
//! use optarg2chain::optarg_fn;
//!
//! // specify optarg_fn attribute with builder struct name and terminal method name
//! #[optarg_fn(JoinStringBuilder, exec)]
//! fn join_strings(
//!     mut a: String,                         // Required argument, no default value
//!     #[optarg_default] b: String,           // String::default() is the default value to b
//!     #[optarg("ccc".to_owned())] c: String, // "ccc".to_owned() is the default value to c
//! ) -> String {
//!     a.push_str(&b);
//!     a.push_str(&c);
//!     a
//! }
//! ```
//! You can use the generated function as below:
//! ```
//! # use optarg2chain::optarg_fn;
//! # #[optarg_fn(JoinStringBuilder, exec)]
//! # fn join_strings(
//! #     mut a: String,                         // Required argument, no default value
//! #     #[optarg_default] b: String,           // String::default() is the default value to b
//! #     #[optarg("ccc".to_owned())] c: String, // "ccc".to_owned() is the default value to c
//! # ) -> String {
//! #     a.push_str(&b);
//! #     a.push_str(&c);
//! #     a
//! # }
//! assert_eq!(join_strings("aaa".to_owned()).exec(), "aaaccc"); // Use default values
//! assert_eq!(
//!     join_strings("xxx".to_owned())
//!         .b("yyy".to_owned()) // Pass a value to `b` explicitly
//!         .c("zzz".to_owned()) // Pass a value to `c` explicitly
//!         .exec(),
//!     "xxxyyyzzz"
//! );
//! ```
//!
//! # Method with optional arguments
//! Use `#[optarg_impl]` and `#[optarg_method(BuilderStructName, terminal_method_name)]` for methods in `impl`
//! ```
//! use optarg2chain::optarg_impl;
//!
//! struct MyVec<T> {
//!     data: Vec<T>,
//! }
//!
//! #[optarg_impl]
//! impl<T: Default + Copy> MyVec<T> {
//!     #[optarg_method(MyVecGetOr, get)]
//!     fn get_or<'a>(&'a self, i: usize, #[optarg_default] other: T) -> T {
//!         self.data.get(i).copied().unwrap_or(other)
//!     }
//! }
//! ```
//! You can use this as below:
//! ```
//! # use optarg2chain::optarg_impl;
//! # struct MyVec<T> {
//! #     data: Vec<T>,
//! # }
//! #
//! # #[optarg_impl]
//! # impl<T: Default + Copy> MyVec<T> {
//! #     #[optarg_method(MyVecGetOr, get)]
//! #     fn get_or<'a>(&'a self, i: usize, #[optarg_default] other: T) -> T {
//! #         self.data.get(i).copied().unwrap_or(other)
//! #     }
//! # }
//! let myvec = MyVec { data: vec![2, 4, 6] };
//! assert_eq!(myvec.get_or(1).get(), 4);
//! assert_eq!(myvec.get_or(10).get(), 0);
//! assert_eq!(myvec.get_or(10).other(42).get(), 42);
//! ```

extern crate proc_macro;

mod doc;
mod generics;

use generics::*;
use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::fold::Fold;
use syn::parse::{Parse, ParseStream};
use syn::spanned::Spanned;
use syn::{Error, Result};

const ATTR_PREFIX: &str = "optarg";
const ATTR_NAME_OPT_ARG: &str = "optarg";
const ATTR_NAME_DEFAULT_ARG: &str = "optarg_default";
const ATTR_NAME_METHOD: &str = "optarg_method";

const INNER_SELF_VAR: &str = "_optarg_self";

const ERR_MSG_TRAIT_IMPL: &str = "(optarg2chain) impl for traits is not supported";
const ERR_MSG_IMPLICIT_LIFETIME: &str = "(optarg2chain) explicit lifetime is neeeded";
const ERR_MSG_UNDERSCORE_ARG: &str = "(optarg2chain) `_` cannot be used for this argument name";
const ERR_MSG_UNUSABLE_PAT: &str = "(optarg2chain) unusable pattern found";
const ERR_MSG_UNSUPPORTED_FN_SIG: &str =
    "(optarg2chain) function or method with `unsafe`, `const` or `extern` is not supported";

/// Generates a builder struct and methods for the specified function.
#[proc_macro_attribute]
pub fn optarg_fn(attr: TokenStream, item: TokenStream) -> TokenStream {
    let FnAttr {
        builder_struct_name,
        terminal_method_name,
    } = syn::parse_macro_input!(attr as FnAttr);
    let item: syn::ItemFn = syn::parse_macro_input!(item);
    if let Err(e) = check_sig(&item.sig) {
        return TokenStream::from(e.to_compile_error());
    }
    let return_type = &item.sig.output;
    let struct_marker_type = generics::generate_type_holder(&item.sig.generics);
    let args: Vec<&syn::PatType> = item
        .sig
        .inputs
        .iter()
        .map(|a| match a {
            syn::FnArg::Typed(t) => t,
            syn::FnArg::Receiver(_) => panic!(),
        })
        .collect();
    let vis = &item.vis;

    let args = parse_typed_args(&args);
    let (impl_generics, ty_generics, where_clause) = item.sig.generics.split_for_impl();
    let (arg_name, _, req_ident, req_ty, opt_ident, opt_ty, opt_default_value) =
        separate_args(&args);
    let func_attrs = &item.attrs;
    let async_ = &item.sig.asyncness;
    let await_ = if async_.is_some() {
        Some(quote! { .await })
    } else {
        None
    };

    let mut inner_func = item.clone();
    erase_optarg_attr(&mut inner_func.sig);
    inner_func.vis = syn::Visibility::Inherited;
    let func_name = inner_func.sig.ident.clone();
    let inner_func_name = syn::Ident::new("_optarg_inner_func", Span::call_site());
    inner_func.sig.ident = inner_func_name.clone();
    let doc::DocAttrs {
        doc_builder_struct,
        doc_setter,
        doc_terminal_method,
    } = doc::generate_doc(&func_name, &opt_ident);

    TokenStream::from(quote! {
        #doc_builder_struct
        #vis struct #builder_struct_name #ty_generics {
            #(#req_ident: #req_ty,)*
            #(#opt_ident: core::option::Option<#opt_ty>,)*
            _optarg_marker: #struct_marker_type
        }

        impl #impl_generics #builder_struct_name #ty_generics {
            #(
                #doc_setter
                #vis fn #opt_ident<_OPTARG_VALUE: core::convert::Into<#opt_ty>>(
                    mut self, value: _OPTARG_VALUE) -> Self {
                    let value = <_OPTARG_VALUE as core::convert::Into<#opt_ty>>::into(value);
                    self.#opt_ident = Some(value);
                    self
                }
            )*

            #doc_terminal_method
            #vis #async_ fn #terminal_method_name(self) #return_type #where_clause {
                #inner_func

                #(
                    let #req_ident: #req_ty = self.#req_ident;
                )*
                #(
                    let #opt_ident: #opt_ty = self.#opt_ident.unwrap_or_else(|| { #opt_default_value });
                )*
                #inner_func_name (
                    #(
                        #arg_name,
                    )*
                )
                #await_
            }
        }

        #(#func_attrs)*
        #vis fn #func_name #ty_generics (
            #(
                #req_ident: #req_ty,
            )*
        ) -> #builder_struct_name #ty_generics #where_clause {
            #builder_struct_name {
                #(
                    #req_ident,
                )*
                #(
                    #opt_ident: core::option::Option::None,
                )*
                _optarg_marker: core::marker::PhantomData,
            }
        }
    })
}

/// This attribute is used with `optarg_method` attribute.
/// Specify `#[optarg_method(BuilderStructName, terminal_method_name)]` to target methods for code generation.
#[proc_macro_attribute]
pub fn optarg_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
    let mut item: syn::ItemImpl = syn::parse_macro_input!(item);
    if let Some(trait_) = &item.trait_ {
        let err = Error::new(trait_.1.span(), ERR_MSG_TRAIT_IMPL);
        return TokenStream::from(err.to_compile_error());
    }
    let generics = &item.generics;

    let self_ty = &item.self_ty;

    let (optarg_items, normal_items): (Vec<syn::ImplItem>, Vec<syn::ImplItem>) =
        item.items.iter().cloned().partition(|item| match item {
            syn::ImplItem::Method(method) => {
                for attr in &method.attrs {
                    if attr.path.is_ident(ATTR_NAME_METHOD) {
                        return true;
                    }
                }
                false
            }
            _ => false,
        });

    let mut optarg_methods = vec![];
    let mut optarg_structs = vec![];
    let mut optarg_struct_impls = vec![];

    for item in optarg_items {
        match item {
            syn::ImplItem::Method(method) => match optarg_method(method, generics, self_ty) {
                Ok((mut optarg_method, optarg_struct, optarg_struct_impl)) => {
                    optarg_methods.append(&mut optarg_method);
                    optarg_structs.push(optarg_struct);
                    optarg_struct_impls.push(optarg_struct_impl);
                }
                Err(e) => {
                    return TokenStream::from(e.to_compile_error());
                }
            },
            _ => unreachable!(),
        }
    }

    item.items = normal_items;
    item.items.append(&mut optarg_methods);

    let expanded = quote! {
        #item
        #(#optarg_structs)*
        #(#optarg_struct_impls)*
    };
    TokenStream::from(expanded)
}

fn optarg_method(
    input: syn::ImplItemMethod,
    impl_original_generics: &syn::Generics,
    self_ty: &syn::Type,
) -> Result<(Vec<syn::ImplItem>, syn::ItemStruct, syn::ItemImpl)> {
    check_sig(&input.sig)?;
    let (optarg_attrs, other_attrs) = separate_attrs(&input.attrs);
    let FnAttr {
        builder_struct_name,
        terminal_method_name,
    } = optarg_attrs[0].parse_args().unwrap();
    let vis = input.vis;
    let mut self_replace = SelfReplace(self_ty);
    let return_type = self_replace.fold_return_type(input.sig.output.clone());
    let method_name = &input.sig.ident;
    let merged_generics = merge_generics(impl_original_generics, &input.sig, self_ty);
    let (impl_generics, ty_generics, where_clause) = merged_generics.split_for_impl();
    let (original_receiver, receiver_ident, receiver_ty, args) =
        separate_receiver(&input.sig, self_ty)?;
    let struct_marker_type = generics::generate_type_holder(&merged_generics);

    let replaced_args: Vec<syn::PatType> = args
        .iter()
        .map(|pt| self_replace.fold_pat_type((*pt).clone()))
        .collect();
    let args: Vec<&syn::PatType> = replaced_args.iter().map(|pt| pt).collect();
    let args = parse_typed_args(&args);
    let (arg_name, arg_ty, req_ident, req_ty, opt_ident, opt_ty, opt_default_value) =
        separate_args(&args);
    let async_ = &input.sig.asyncness;
    let await_ = if async_.is_some() {
        Some(quote! { .await })
    } else {
        None
    };

    let insert_self = if receiver_ident.is_empty() {
        vec![]
    } else {
        vec![quote! { #(#receiver_ident: self)* }]
    };

    let inner_method_ident = syn::Ident::new(
        &format!("_optarg_inner_{}", method_name),
        method_name.span(),
    );
    let inner_method_block = &input.block;
    let doc::DocAttrs {
        doc_builder_struct,
        doc_setter,
        doc_terminal_method,
    } = doc::generate_doc(&method_name, &opt_ident);

    let mut inner_method: syn::ImplItemMethod = syn::parse_quote! {
        #async_ fn #inner_method_ident (
            #(#original_receiver,)*
            #(#arg_name: #arg_ty,)*) #return_type #where_clause #inner_method_block
    };
    inner_method.sig.generics = input.sig.generics.clone();
    let inner_method: syn::ImplItem = inner_method.into();

    let item_struct: syn::ItemStruct = syn::parse_quote! {
        #doc_builder_struct
        #vis struct #builder_struct_name #ty_generics {
            #(#receiver_ident: #receiver_ty,)*
            #(#req_ident: #req_ty,)*
            #(#opt_ident: core::option::Option<#opt_ty>,)*
            _optarg_marker: #struct_marker_type,
        }
    };

    let mut new_method: syn::ImplItemMethod = syn::parse_quote! {
        #(#other_attrs)*
        #vis fn #method_name (
            #(#original_receiver,)*
            #(#req_ident: #req_ty,)*
        ) -> #builder_struct_name #ty_generics {
            #builder_struct_name {
                #(#insert_self,)*
                #(
                    #req_ident,
                )*
                #(
                    #opt_ident: core::option::Option::None,
                )*
                _optarg_marker: core::marker::PhantomData,
            }
        }
    };
    new_method.sig.generics = input.sig.generics.clone();
    let new_method: syn::ImplItem = new_method.into();

    let self_ty_no_generics = erase_generics(self_ty);

    let struct_impl: syn::ItemImpl = syn::parse_quote! {
        impl #impl_generics #builder_struct_name #ty_generics {
            #(
                #doc_setter
                #vis fn #opt_ident<_OPTARG_VALUE: core::convert::Into<#opt_ty>>(
                    mut self, value: _OPTARG_VALUE) -> Self {
                    let value = <_OPTARG_VALUE as core::convert::Into<#opt_ty>>::into(value);
                    self.#opt_ident = Some(value);
                    self
                }
            )*

            #doc_terminal_method
            #vis #async_ fn #terminal_method_name(self) #return_type #where_clause {
                #(
                    let #receiver_ident: #receiver_ty = self.#receiver_ident;
                )*
                #(
                    let #req_ident: #req_ty = self.#req_ident;
                )*
                #(
                    let #opt_ident: #opt_ty = self.#opt_ident.unwrap_or_else(|| { #opt_default_value });
                )*
                #self_ty_no_generics::#inner_method_ident( #(#receiver_ident,)* #(#arg_name, )* )
                #await_
            }
        }
    };

    Ok((vec![new_method, inner_method], item_struct, struct_impl))
}

struct Arg<'a> {
    ident: &'a syn::Ident,
    ty: &'a syn::Type,
    default_value: Option<syn::Expr>,
}

struct FnAttr {
    builder_struct_name: syn::Ident,
    terminal_method_name: syn::Ident,
}

impl Parse for FnAttr {
    fn parse(input: ParseStream) -> syn::parse::Result<Self> {
        let builder_struct_name: syn::Ident = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let terminal_method_name: syn::Ident = input.parse()?;
        Ok(FnAttr {
            builder_struct_name,
            terminal_method_name,
        })
    }
}

fn parse_typed_args<'a>(args: &[&'a syn::PatType]) -> Vec<Arg<'a>> {
    args.iter()
        .map(|arg: &&syn::PatType| {
            let ident: &syn::Ident = match &*arg.pat {
                syn::Pat::Ident(ident) => &ident.ident,
                _ => panic!(),
            };
            let ty: &syn::Type = &*arg.ty;
            let default_value = parse_arg_attr(&arg.attrs, ty);
            Arg {
                ident,
                ty,
                default_value,
            }
        })
        .collect()
}

fn parse_arg_attr(attrs: &[syn::Attribute], ty: &syn::Type) -> Option<syn::Expr> {
    for attr in attrs {
        assert_eq!(attr.style, syn::AttrStyle::Outer);

        if attr.path.is_ident(ATTR_NAME_OPT_ARG) {
            return Some(attr.parse_args().unwrap());
        } else if attr.path.is_ident(ATTR_NAME_DEFAULT_ARG) {
            assert!(attr.tokens.is_empty());
            return Some(syn::parse_quote! {
                <#ty as core::default::Default>::default()
            });
        } else {
            continue;
        }
    }
    None
}

// separate args to (arg name, required ident, ty, optional ident, ty, defalut_value)
fn separate_args<'a>(
    args: &'a [Arg<'a>],
) -> (
    Vec<&'a syn::Ident>,
    Vec<&'a syn::Type>,
    Vec<&'a syn::Ident>,
    Vec<&'a syn::Type>,
    Vec<&'a syn::Ident>,
    Vec<&'a syn::Type>,
    Vec<&'a syn::Expr>,
) {
    let mut arg_name = vec![];
    let mut arg_ty = vec![];
    let mut req_ident = vec![];
    let mut req_ty = vec![];
    let mut opt_ident = vec![];
    let mut opt_ty = vec![];
    let mut opt_default_value = vec![];
    for arg in args {
        if arg.default_value.is_none() {
            req_ident.push(arg.ident);
            req_ty.push(arg.ty);
        } else {
            opt_ident.push(arg.ident);
            opt_ty.push(arg.ty);
            opt_default_value.push(arg.default_value.as_ref().unwrap());
        }
        arg_name.push(arg.ident);
        arg_ty.push(arg.ty);
    }
    (
        arg_name,
        arg_ty,
        req_ident,
        req_ty,
        opt_ident,
        opt_ty,
        opt_default_value,
    )
}

fn erase_optarg_attr(sig: &mut syn::Signature) {
    for arg in sig.inputs.iter_mut() {
        match arg {
            syn::FnArg::Typed(pt) => {
                pt.attrs.retain(|attr| {
                    !attr.path.is_ident(ATTR_NAME_DEFAULT_ARG)
                        && !attr.path.is_ident(ATTR_NAME_OPT_ARG)
                });
            }
            _ => (),
        }
    }
}

fn separate_attrs<'a>(
    attrs: &'a [syn::Attribute],
) -> (Vec<&'a syn::Attribute>, Vec<&'a syn::Attribute>) {
    let mut optarg_attrs = vec![];
    let mut other_attrs = vec![];

    for attr in attrs {
        if let Some(ident) = attr.path.get_ident().map(|ident| ident.to_string()) {
            if ident.starts_with(ATTR_PREFIX) {
                optarg_attrs.push(attr);
                continue;
            }
        }
        other_attrs.push(attr);
    }
    (optarg_attrs, other_attrs)
}

// Returns (receiver, reciever ident, receiver type, other args)
fn separate_receiver<'a>(
    sig: &'a syn::Signature,
    self_ty: &syn::Type,
) -> Result<(
    Vec<syn::FnArg>,
    Vec<syn::Ident>,
    Vec<syn::Type>,
    Vec<&'a syn::PatType>,
)> {
    let mut receiver = None;
    let mut typed_self: Option<&syn::PatType> = None;
    let mut args = vec![];
    for arg in &sig.inputs {
        match arg {
            syn::FnArg::Receiver(r) => {
                assert!(receiver.is_none());
                receiver = Some(r);
            }
            syn::FnArg::Typed(t) => {
                if let syn::Pat::Ident(syn::PatIdent { ref ident, .. }) = *t.pat {
                    if ident == "self" {
                        // Handles typed self like `self: Box<Self>`
                        assert!(typed_self.is_none());
                        typed_self = Some(t);
                        continue;
                    }
                }
                args.push(t);
            }
        }
    }
    let mut new_receiver: Vec<syn::FnArg> = vec![];
    let (receiver_ident, receiver_ty) = if let Some(receiver) = receiver {
        let self_ident = syn::Ident::new(INNER_SELF_VAR, Span::call_site());
        let receiver_ty: syn::Type = match (&receiver.reference, &receiver.mutability) {
            (Some((_, None)), _) => {
                return Err(Error::new(receiver.span(), ERR_MSG_IMPLICIT_LIFETIME));
            }
            (Some((_, Some(lifetime))), None) => {
                new_receiver = vec![syn::parse_quote! { &#lifetime self }];
                syn::parse_quote! { &#lifetime #self_ty }
            }
            (Some((_, Some(lifetime))), Some(_)) => {
                new_receiver = vec![syn::parse_quote! { &#lifetime mut self }];
                syn::parse_quote! { &#lifetime mut #self_ty }
            }
            (None, is_mut) => {
                if is_mut.is_some() {
                    new_receiver = vec![syn::parse_quote! { mut self }];
                } else {
                    new_receiver = vec![syn::parse_quote! { self }];
                }
                syn::parse_quote! { #self_ty }
            }
        };
        (vec![self_ident], vec![receiver_ty])
    } else if let Some(pt) = typed_self {
        let self_ident = syn::Ident::new(INNER_SELF_VAR, Span::call_site());
        let mut self_replace = SelfReplace(self_ty);
        let receiver_ty = self_replace.fold_type((*pt.ty).clone());
        new_receiver.push(syn::FnArg::from(pt.clone()));
        (vec![self_ident], vec![receiver_ty])
    } else {
        (vec![], vec![])
    };
    Ok((new_receiver, receiver_ident, receiver_ty, args))
}

// Checks function signature and returns error if exists
fn check_sig(sig: &syn::Signature) -> Result<()> {
    if let Some(abi) = &sig.abi {
        return Err(Error::new(abi.span(), ERR_MSG_UNSUPPORTED_FN_SIG));
    }
    if let Some(constness) = &sig.constness {
        return Err(Error::new(constness.span(), ERR_MSG_UNSUPPORTED_FN_SIG));
    }
    if let Some(unsafety) = &sig.unsafety {
        return Err(Error::new(unsafety.span(), ERR_MSG_UNSUPPORTED_FN_SIG));
    }

    for arg in &sig.inputs {
        match arg {
            syn::FnArg::Typed(t) => match *t.pat {
                syn::Pat::Ident(syn::PatIdent { ref ident, .. }) => {
                    if ident == INNER_SELF_VAR {
                        return Err(Error::new(
                            ident.span(),
                            format!("(optarg2chain) {} is reserved name", INNER_SELF_VAR),
                        ));
                    }
                }
                syn::Pat::Wild(ref w) => {
                    return Err(Error::new(w.span(), ERR_MSG_UNDERSCORE_ARG));
                }
                _ => {
                    return Err(Error::new(t.span(), ERR_MSG_UNUSABLE_PAT));
                }
            },
            _ => (),
        }
    }
    Ok(())
}