Files
addr2line
ahash
aho_corasick
arrayref
arrayvec
artemis_asset
artemis_core
artemis_erc20_app
artemis_eth_app
artemis_ethereum
backtrace
base58
bip39
bitmask
bitvec
blake2_rfc
block_buffer
block_padding
byte_slice_cast
byte_tools
byteorder
cfg_if
clear_on_drop
const_random
const_random_macro
constant_time_eq
crunchy
crypto_mac
curve25519_dalek
derive_more
digest
ed25519_dalek
either
environmental
ethabi_decode
ethbloom
ethereum_types
failure
failure_derive
fake_simd
fixed_hash
frame_metadata
frame_support
frame_support_procedural
frame_support_procedural_tools
frame_support_procedural_tools_derive
frame_system
futures
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
io
lock
sink
stream
task
generic_array
getrandom
gimli
hash256_std_hasher
hash_db
hashbrown
hex
hex_literal
hmac
hmac_drbg
impl_codec
impl_rlp
impl_serde
impl_trait_for_tuples
inflector
cases
camelcase
case
classcase
kebabcase
pascalcase
screamingsnakecase
sentencecase
snakecase
tablecase
titlecase
traincase
numbers
deordinalize
ordinalize
string
constants
deconstantize
demodulize
pluralize
singularize
suffix
foreignkey
integer_sqrt
itertools
keccak
lazy_static
libc
lock_api
log
memchr
memory_db
memory_units
merlin
nodrop
num_bigint
num_cpus
num_integer
num_rational
num_traits
object
once_cell
opaque_debug
pallet_bridge
pallet_verifier
parity_scale_codec
parity_scale_codec_derive
parity_util_mem
parity_util_mem_derive
parity_wasm
parking_lot
parking_lot_core
paste
paste_impl
pbkdf2
pin_project
pin_project_internal
pin_utils
ppv_lite86
primitive_types
proc_macro2
proc_macro_crate
proc_macro_hack
proc_macro_nested
quote
radium
rand
rand_chacha
rand_core
rand_pcg
ref_cast
ref_cast_impl
regex
regex_syntax
rental
rental_impl
rlp
rustc_demangle
rustc_hash
rustc_hex
schnorrkel
scopeguard
secp256k1
serde
serde_derive
sha2
slab
smallvec
sp_application_crypto
sp_arithmetic
sp_core
sp_debug_derive
sp_externalities
sp_inherents
sp_io
sp_panic_handler
sp_runtime
sp_runtime_interface
sp_runtime_interface_proc_macro
sp_state_machine
sp_std
sp_storage
sp_tracing
sp_trie
sp_version
sp_wasm_interface
stable_deref_trait
static_assertions
substrate_bip39
subtle
syn
synstructure
thread_local
tiny_keccak
toml
tracing
tracing_attributes
tracing_core
trie_db
trie_root
twox_hash
typenum
uint
unicode_normalization
unicode_xid
wasmi
wasmi_validation
zeroize
zeroize_derive
  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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! Implementation of the semi-automatic tuple trait implementation.
//!
//! The semi-automatic implementation uses an implementation provided by the user to generate the
//! tuple implementations. The user is able to use a special syntax `for_tuples!( #(TUPLE)* );` to
//! express the tuple access while the `TUPLE` ident can be chosen by the user.

use proc_macro2::TokenStream;

use syn::{
    bracketed,
    fold::{self, Fold},
    parenthesized,
    parse::{Parse, ParseStream},
    parse_quote,
    spanned::Spanned,
    token, Block, Error, Expr, ExprField, FnArg, Ident, ImplItem, ImplItemMethod, Index, ItemImpl,
    Lifetime, Macro, Member, Result, Stmt, Type, WhereClause, WherePredicate,
};

use quote::{quote, ToTokens};

/// By default we add the trait bound for the implemented trait to each tuple type. When this
/// attribute is given we don't add this bound.
const TUPLE_TYPES_NO_DEFAULT_TRAIT_BOUND: &str = "tuple_types_no_default_trait_bound";

/// The supported separators in the `#( Tuple::test() )SEPARATOR*` syntax.
enum Separator {
    Comma(token::Comma),
    Add(token::Add),
    Sub(token::Sub),
    Or(token::Or),
    And(token::And),
    Star(token::Star),
    Div(token::Div),
}

impl Separator {
    /// Try to parse the separator before the `*` token.
    fn parse_before_star(input: ParseStream) -> Result<Option<Self>> {
        if input.peek2(token::Star) {
            Self::parse(input).map(Some)
        } else {
            Ok(None)
        }
    }

    /// Convert into a `TokenStream`.
    ///
    /// `last` - Is this the last separator to add? Only `,` will be added on `last == true`.
    fn to_token_stream(&self, last: bool) -> TokenStream {
        let empty_on_last = |token: &dyn ToTokens| {
            if last {
                TokenStream::default()
            } else {
                token.to_token_stream()
            }
        };

        match self {
            Self::Comma(comma) => comma.to_token_stream(),
            Self::Add(add) => empty_on_last(add),
            Self::Sub(sub) => empty_on_last(sub),
            Self::Or(or) => empty_on_last(or),
            Self::And(and) => empty_on_last(and),
            Self::Star(star) => empty_on_last(star),
            Self::Div(div) => empty_on_last(div),
        }
    }
}

impl Parse for Separator {
    fn parse(input: ParseStream) -> Result<Self> {
        let lookahead1 = input.lookahead1();

        if lookahead1.peek(token::Comma) {
            Ok(Self::Comma(input.parse()?))
        } else if lookahead1.peek(token::Add) {
            Ok(Self::Add(input.parse()?))
        } else if lookahead1.peek(token::Sub) {
            Ok(Self::Sub(input.parse()?))
        } else if lookahead1.peek(token::Or) {
            Ok(Self::Or(input.parse()?))
        } else if lookahead1.peek(token::And) {
            Ok(Self::And(input.parse()?))
        } else if lookahead1.peek(token::Star) {
            Ok(Self::Star(input.parse()?))
        } else if lookahead1.peek(token::Div) {
            Ok(Self::Div(input.parse()?))
        } else {
            Err(lookahead1.error())
        }
    }
}

/// The `#( Tuple::test() )SEPARATOR*` (tuple repetition) syntax.
struct TupleRepetition {
    pub pound_token: token::Pound,
    pub paren_token: token::Paren,
    pub stmts: Vec<Stmt>,
    pub where_predicate: Option<WherePredicate>,
    pub separator: Option<Separator>,
    pub star_token: token::Star,
}

impl TupleRepetition {
    /// Parse the inner representation as stmts.
    fn parse_as_stmts(input: ParseStream) -> Result<Self> {
        let content;
        Ok(Self {
            pound_token: input.parse()?,
            paren_token: parenthesized!(content in input),
            stmts: content.call(Block::parse_within)?,
            where_predicate: None,
            separator: Separator::parse_before_star(input)?,
            star_token: input.parse()?,
        })
    }

    /// Parse the inner representation as a where predicate.
    fn parse_as_where_predicate(input: ParseStream) -> Result<Self> {
        let content;
        Ok(Self {
            pound_token: input.parse()?,
            paren_token: parenthesized!(content in input),
            stmts: Vec::new(),
            where_predicate: Some(content.parse()?),
            separator: Separator::parse_before_star(input)?,
            star_token: input.parse()?,
        })
    }

    /// Expand this repetition to the actual stmts implementation.
    fn expand_as_stmts(
        self,
        tuple_placeholder_ident: &Ident,
        tuples: &[Ident],
        use_self: bool,
    ) -> TokenStream {
        let mut generated = TokenStream::new();

        for (i, tuple) in tuples.iter().enumerate() {
            generated.extend(self.stmts.iter().cloned().map(|s| {
                ReplaceTuplePlaceholder::replace_ident_in_stmt(
                    tuple_placeholder_ident,
                    tuple,
                    use_self,
                    i,
                    s,
                )
                .map(|s| s.to_token_stream())
                .unwrap_or_else(|e| e.to_compile_error())
            }));

            if let Some(ref sep) = self.separator {
                generated.extend(sep.to_token_stream(i + 1 == tuples.len()));
            }
        }

        generated
    }

    /// Expand this to the given `where_clause`.
    /// It is expected that the instance was created with `parse_as_where_predicate`.
    fn expand_to_where_clause(
        self,
        tuple_placeholder_ident: &Ident,
        tuples: &[Ident],
        where_clause: &mut WhereClause,
    ) -> Result<()> {
        let span = self.pound_token.span();
        let predicate = self.where_predicate.ok_or_else(|| {
            Error::new(
                span,
                "Internal error, expected `where_predicate` to be set! Please report this issue!",
            )
        })?;

        for tuple in tuples.iter() {
            where_clause.predicates.push(
                ReplaceTuplePlaceholder::replace_ident_in_where_predicate(
                    tuple_placeholder_ident,
                    tuple,
                    predicate.clone(),
                )?,
            );
        }

        Ok(())
    }
}

/// Replace the tuple place holder in the ast.
struct ReplaceTuplePlaceholder<'a> {
    search: &'a Ident,
    replace: &'a Ident,
    use_self: bool,
    index: Index,
    errors: Vec<Error>,
}

impl<'a> ReplaceTuplePlaceholder<'a> {
    fn replace_ident_in_stmt(
        search: &'a Ident,
        replace: &'a Ident,
        use_self: bool,
        index: usize,
        stmt: Stmt,
    ) -> Result<Stmt> {
        let mut folder = Self {
            search,
            replace,
            use_self,
            index: index.into(),
            errors: Vec::new(),
        };

        let res = fold::fold_stmt(&mut folder, stmt);

        if let Some(first) = folder.errors.pop() {
            Err(folder.errors.into_iter().fold(first, |mut e, n| {
                e.combine(n);
                e
            }))
        } else {
            Ok(res)
        }
    }

    fn replace_ident_in_where_predicate(
        search: &'a Ident,
        replace: &'a Ident,
        where_predicate: WherePredicate,
    ) -> Result<WherePredicate> {
        let mut folder = Self {
            search,
            replace,
            use_self: false,
            index: 0.into(),
            errors: Vec::new(),
        };

        let res = fold::fold_where_predicate(&mut folder, where_predicate);

        if let Some(first) = folder.errors.pop() {
            Err(folder.errors.into_iter().fold(first, |mut e, n| {
                e.combine(n);
                e
            }))
        } else {
            Ok(res)
        }
    }
}

impl<'a> Fold for ReplaceTuplePlaceholder<'a> {
    fn fold_ident(&mut self, ident: Ident) -> Ident {
        if &ident == self.search {
            self.replace.clone()
        } else {
            ident
        }
    }

    fn fold_expr(&mut self, expr: Expr) -> Expr {
        match expr {
            Expr::MethodCall(mut call) => match *call.receiver {
                Expr::Path(ref path) if path.path.is_ident(self.search) => {
                    if self.use_self {
                        let index = &self.index;
                        call.receiver = parse_quote!( self.#index );

                        call.into()
                    } else {
                        self.errors.push(Error::new(
                            path.span(),
                            "Can not call non-static method from within a static method.",
                        ));
                        Expr::Verbatim(Default::default())
                    }
                }
                _ => fold::fold_expr_method_call(self, call).into(),
            },
            _ => fold::fold_expr(self, expr),
        }
    }

    fn fold_expr_field(&mut self, mut expr: ExprField) -> ExprField {
        match expr.member {
            Member::Named(ref ident) if ident == self.search => {
                // Replace `something.Tuple` with `something.0`, `something.1`, etc.
                expr.member = Member::Unnamed(self.index.clone());
                expr
            }
            _ => expr,
        }
    }
}

/// The `for_tuples!` macro syntax.
enum ForTuplesMacro {
    /// The macro at an item type position.
    ///
    /// `type Item =`
    ItemType {
        type_token: token::Type,
        ident: Ident,
        equal_token: token::Eq,
        paren_token: token::Paren,
        tuple_repetition: TupleRepetition,
        semi_token: token::Semi,
    },
    /// The macro at an item const position.
    ///
    /// `const Item: Type =`
    ItemConst {
        const_token: token::Const,
        ident: Ident,
        colon_token: token::Colon,
        ty: Type,
        equal_token: token::Eq,
        /// When this is a `slice` type, we need to parse `&[]`.
        slice: Option<(token::And, token::Bracket)>,
        tuple_repetition: TupleRepetition,
        semi_token: token::Semi,
    },
    /// The repetition stmt wrapped in parenthesis.
    StmtParenthesized {
        paren_token: token::Paren,
        tuple_repetition: TupleRepetition,
    },
    /// Just the repetition stmt.
    Stmt { tuple_repetition: TupleRepetition },
    /// A custom where clause.
    Where {
        _where_token: token::Where,
        tuple_repetition: TupleRepetition,
    },
}

impl Parse for ForTuplesMacro {
    fn parse(input: ParseStream) -> Result<Self> {
        let lookahead1 = input.lookahead1();

        if lookahead1.peek(token::Type) {
            let content;
            Ok(ForTuplesMacro::ItemType {
                type_token: input.parse()?,
                ident: input.parse()?,
                equal_token: input.parse()?,
                paren_token: parenthesized!(content in input),
                tuple_repetition: content.call(TupleRepetition::parse_as_stmts)?,
                semi_token: input.parse()?,
            })
        } else if lookahead1.peek(token::Const) {
            let const_token = input.parse()?;
            let ident = input.parse()?;
            let colon_token = input.parse()?;
            let ty = input.parse()?;
            let equal_token = input.parse()?;

            let lookahead1 = input.lookahead1();

            if lookahead1.peek(token::And) {
                let content;

                Ok(ForTuplesMacro::ItemConst {
                    const_token,
                    ident,
                    colon_token,
                    ty,
                    equal_token,
                    slice: Some((input.parse()?, bracketed!(content in input))),
                    tuple_repetition: content.call(TupleRepetition::parse_as_stmts)?,
                    semi_token: input.parse()?,
                })
            } else if lookahead1.peek(token::Pound) {
                Ok(ForTuplesMacro::ItemConst {
                    const_token,
                    ident,
                    colon_token,
                    ty,
                    equal_token,
                    slice: None,
                    tuple_repetition: input.call(TupleRepetition::parse_as_stmts)?,
                    semi_token: input.parse()?,
                })
            } else {
                Err(lookahead1.error())
            }
        } else if lookahead1.peek(token::Paren) {
            let content;
            Ok(ForTuplesMacro::StmtParenthesized {
                paren_token: parenthesized!(content in input),
                tuple_repetition: content.call(TupleRepetition::parse_as_stmts)?,
            })
        } else if lookahead1.peek(token::Pound) {
            Ok(ForTuplesMacro::Stmt {
                tuple_repetition: input.call(TupleRepetition::parse_as_stmts)?,
            })
        } else if lookahead1.peek(token::Where) {
            Ok(ForTuplesMacro::Where {
                _where_token: input.parse()?,
                tuple_repetition: input.call(TupleRepetition::parse_as_where_predicate)?,
            })
        } else {
            Err(lookahead1.error())
        }
    }
}

impl ForTuplesMacro {
    /// Try to parse the given macro as `Self`.
    ///
    /// `allow_where` signals that a custom where clause is allowed at this position.
    ///
    /// Returns `Ok(None)` if it is not a `for_tuples!` macro.
    fn try_from(macro_item: &Macro, allow_where: bool) -> Result<Option<Self>> {
        // Not the macro we are searching for
        if !macro_item.path.is_ident("for_tuples") {
            return Ok(None);
        }

        let res = macro_item.parse_body::<Self>()?;

        if !allow_where && res.is_where() {
            Err(Error::new(
                macro_item.span(),
                "Custom where clause not allowed at this position!",
            ))
        } else {
            Ok(Some(res))
        }
    }

    /// Is this a custom where clause?
    fn is_where(&self) -> bool {
        match self {
            Self::Where { .. } => true,
            _ => false,
        }
    }

    /// Convert this into the where clause tuple repetition.
    fn into_where(self) -> Option<TupleRepetition> {
        match self {
            Self::Where {
                tuple_repetition, ..
            } => Some(tuple_repetition),
            _ => None,
        }
    }

    /// Expand `self` to the actual implementation without the `for_tuples!` macro.
    ///
    /// This will unroll the repetition by replacing the placeholder identifier in each iteration
    /// with the one given in `tuples`. If `use_self` is `true`, the tuple will be access by using
    /// `self.x`.
    ///
    /// Returns the generated code.
    fn expand(
        self,
        tuple_placeholder_ident: &Ident,
        tuples: &[Ident],
        use_self: bool,
    ) -> TokenStream {
        match self {
            Self::ItemType {
                type_token,
                ident,
                equal_token,
                paren_token,
                tuple_repetition,
                semi_token,
            } => {
                let mut token_stream = type_token.to_token_stream();
                let repetition =
                    tuple_repetition.expand_as_stmts(tuple_placeholder_ident, tuples, use_self);

                ident.to_tokens(&mut token_stream);
                equal_token.to_tokens(&mut token_stream);
                paren_token.surround(&mut token_stream, |tokens| tokens.extend(repetition));
                semi_token.to_tokens(&mut token_stream);

                token_stream
            }
            Self::ItemConst {
                const_token,
                ident,
                colon_token,
                ty,
                equal_token,
                slice,
                tuple_repetition,
                semi_token,
            } => {
                let mut token_stream = const_token.to_token_stream();
                let repetition =
                    tuple_repetition.expand_as_stmts(tuple_placeholder_ident, tuples, use_self);

                ident.to_tokens(&mut token_stream);
                colon_token.to_tokens(&mut token_stream);
                ty.to_tokens(&mut token_stream);
                equal_token.to_tokens(&mut token_stream);
                match slice {
                    Some((ref_token, brackets)) => {
                        ref_token.to_tokens(&mut token_stream);
                        brackets.surround(&mut token_stream, |tokens| tokens.extend(repetition));
                    }
                    None => token_stream.extend(repetition),
                }
                semi_token.to_tokens(&mut token_stream);

                token_stream
            }
            Self::StmtParenthesized {
                paren_token,
                tuple_repetition,
            } => {
                let mut token_stream = TokenStream::new();
                let repetition =
                    tuple_repetition.expand_as_stmts(tuple_placeholder_ident, tuples, use_self);

                paren_token.surround(&mut token_stream, |tokens| tokens.extend(repetition));

                token_stream
            }
            Self::Stmt { tuple_repetition } => {
                tuple_repetition.expand_as_stmts(tuple_placeholder_ident, tuples, use_self)
            }
            Self::Where { .. } => TokenStream::new(),
        }
    }
}

/// Add the tuple elements as generic parameters to the given trait implementation.
fn add_tuple_elements_generics(
    tuples: &[Ident],
    mut trait_impl: ItemImpl,
    add_bound: bool,
) -> Result<ItemImpl> {
    let trait_ = trait_impl.trait_.clone().map(|t| t.1).ok_or_else(|| {
        Error::new(
            trait_impl.span(),
            "The semi-automatic implementation is required to implement a trait!",
        )
    })?;

    let bound = if add_bound {
        Some(quote!( #trait_ ))
    } else {
        None
    };
    crate::utils::add_tuple_element_generics(tuples, bound, &mut trait_impl.generics);
    Ok(trait_impl)
}

/// Fold a given trait implementation into a tuple implementation of the given trait.
struct ToTupleImplementation<'a> {
    /// The tuple idents to use while expanding the repetitions.
    tuples: &'a [Ident],
    /// The placeholder ident given by the user.
    ///
    /// This placeholder ident while be replaced in the expansion with the correct tuple identifiers.
    tuple_placeholder_ident: &'a Ident,
    /// Any errors found while doing the conversion.
    errors: Vec<Error>,
    /// This is set to `true`, when folding in a function block that has a `self` parameter.
    has_self_parameter: bool,
    /// A custom where clause provided by the user.
    custom_where_clause: Option<TupleRepetition>,
}

impl<'a> ToTupleImplementation<'a> {
    /// Generate the tuple implementation for the given `tuples`.
    fn generate_implementation(
        trait_impl: &ItemImpl,
        tuple_placeholder_ident: &'a Ident,
        tuples: &'a [Ident],
    ) -> Result<TokenStream> {
        let mut to_tuple = ToTupleImplementation {
            tuples,
            errors: Vec::new(),
            tuple_placeholder_ident,
            has_self_parameter: false,
            custom_where_clause: None,
        };

        let mut res = fold::fold_item_impl(&mut to_tuple, trait_impl.clone());

        // Check if we should add the bound to the implemented trait for each tuple type.
        let add_bound = if let Some(pos) = res
            .attrs
            .iter()
            .position(|a| a.path.is_ident(TUPLE_TYPES_NO_DEFAULT_TRAIT_BOUND))
        {
            res.attrs.remove(pos);
            false
        } else {
            true
        };

        // Add the tuple generics
        let mut res = add_tuple_elements_generics(tuples, res, add_bound)?;
        // Add the correct self type
        res.self_ty = parse_quote!( ( #( #tuples, )* ) );
        res.attrs.push(parse_quote!(#[allow(unused)]));

        if let Some(where_clause) = to_tuple.custom_where_clause.take() {
            where_clause.expand_to_where_clause(
                tuple_placeholder_ident,
                tuples,
                res.generics.make_where_clause(),
            )?;
        }

        if let Some(first_error) = to_tuple.errors.pop() {
            Err(to_tuple.errors.into_iter().fold(first_error, |mut e, n| {
                e.combine(n);
                e
            }))
        } else {
            Ok(res.to_token_stream())
        }
    }

    /// Fold the expr and returns the folded expr and if it was a `for_tuples!`.
    fn custom_fold_expr(&mut self, expr: Expr) -> (Expr, bool) {
        match expr {
            Expr::Macro(expr_macro) => match ForTuplesMacro::try_from(&expr_macro.mac, false) {
                Ok(Some(for_tuples)) => (
                    Expr::Verbatim(for_tuples.expand(
                        &self.tuple_placeholder_ident,
                        self.tuples,
                        self.has_self_parameter,
                    )),
                    true,
                ),
                Ok(None) => (fold::fold_expr_macro(self, expr_macro).into(), false),
                Err(e) => {
                    self.errors.push(e);
                    (Expr::Verbatim(Default::default()), false)
                }
            },
            _ => (fold::fold_expr(self, expr), false),
        }
    }
}

impl<'a> Fold for ToTupleImplementation<'a> {
    fn fold_impl_item(&mut self, i: ImplItem) -> ImplItem {
        match i {
            ImplItem::Macro(macro_item) => match ForTuplesMacro::try_from(&macro_item.mac, true) {
                Ok(Some(for_tuples)) => {
                    if for_tuples.is_where() {
                        if self.custom_where_clause.is_some() {
                            self.errors.push(Error::new(
                                macro_item.span(),
                                "Only one custom where clause is supported!",
                            ));
                        } else {
                            self.custom_where_clause = for_tuples.into_where();
                        }

                        ImplItem::Verbatim(Default::default())
                    } else {
                        ImplItem::Verbatim(for_tuples.expand(
                            &self.tuple_placeholder_ident,
                            self.tuples,
                            false,
                        ))
                    }
                }
                Ok(None) => fold::fold_impl_item_macro(self, macro_item).into(),
                Err(e) => {
                    self.errors.push(e);
                    ImplItem::Verbatim(Default::default())
                }
            },
            _ => fold::fold_impl_item(self, i),
        }
    }

    fn fold_expr(&mut self, expr: Expr) -> Expr {
        self.custom_fold_expr(expr).0
    }

    fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
        let (expr, trailing_semi) = match stmt {
            Stmt::Expr(expr) => (expr, None),
            Stmt::Semi(expr, semi) => (expr, Some(semi)),
            _ => return fold::fold_stmt(self, stmt),
        };

        let (expr, expanded) = self.custom_fold_expr(expr);

        if expanded {
            Stmt::Expr(expr)
        } else if let Some(semi) = trailing_semi {
            Stmt::Semi(expr, semi)
        } else {
            Stmt::Expr(expr)
        }
    }

    fn fold_type(&mut self, ty: Type) -> Type {
        match ty {
            Type::Macro(ty_macro) => match ForTuplesMacro::try_from(&ty_macro.mac, false) {
                Ok(Some(for_tuples)) => Type::Verbatim(for_tuples.expand(
                    &self.tuple_placeholder_ident,
                    self.tuples,
                    false,
                )),
                Ok(None) => fold::fold_type_macro(self, ty_macro).into(),
                Err(e) => {
                    self.errors.push(e);
                    Type::Verbatim(Default::default())
                }
            },
            _ => fold::fold_type(self, ty),
        }
    }

    fn fold_impl_item_method(&mut self, mut impl_item_method: ImplItemMethod) -> ImplItemMethod {
        let has_self = impl_item_method
            .sig
            .inputs
            .first()
            .map(|a| match a {
                FnArg::Receiver(_) => true,
                _ => false,
            })
            .unwrap_or(false);

        impl_item_method.sig = fold::fold_signature(self, impl_item_method.sig);

        // Store the old value and set the current one
        let old_has_self_parameter = self.has_self_parameter;
        self.has_self_parameter = has_self;

        impl_item_method.block = fold::fold_block(self, impl_item_method.block);
        self.has_self_parameter = old_has_self_parameter;

        impl_item_method
    }
}

/// Extracts the tuple placeholder ident from the given trait implementation.
fn extract_tuple_placeholder_ident(trait_impl: &ItemImpl) -> Result<Ident> {
    if let Type::Path(ref type_path) = *trait_impl.self_ty {
        if let Some(ident) = type_path.path.get_ident() {
            return Ok(ident.clone());
        }
    }

    Err(Error::new(
        trait_impl.self_ty.span(),
        "Expected an `Ident` as tuple placeholder.",
    ))
}

/// Generate the semi-automatic tuple implementations for a given trait implementation and the given tuples.
pub fn semi_automatic_impl(
    trait_impl: ItemImpl,
    tuple_elements: Vec<Ident>,
    min: Option<usize>,
) -> Result<TokenStream> {
    let placeholder_ident = extract_tuple_placeholder_ident(&trait_impl)?;

    let mut res = TokenStream::new();

    (min.unwrap_or(0)..=tuple_elements.len()).try_for_each(|i| {
        res.extend(ToTupleImplementation::generate_implementation(
            &trait_impl,
            &placeholder_ident,
            &tuple_elements[..i],
        )?);
        Ok::<_, Error>(())
    })?;

    Ok(res)
}