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
use Span;
use VisitMut;
pub
/// Replaces `self` to avoid issues around macro hygienicity.
///
/// `thin_delegate` transfers definition of a trait and a struct/enum to
/// `#[thin_delegate::fill_delegate]` by using declarative macro.
/// `#[thin_delegate::__internal__fill_delegate]` processes a token stream in the macro context.
/// If we use `self` in this token stream as is, an error like the following arise:
///
/// ```text
/// error[E0424]: expected value, found module `self`
/// --> src/main.rs:24:1
/// |
/// 3 | fn hello(&self) -> String;
/// | -- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
/// ...
/// 24 | #[thin_delegate::fill_delegate]
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `self` value is a keyword only available in methods with a `self` parameter
/// |
/// = note: this error originates in the attribute macro `::thin_delegate::__internal__fill_delegate` which comes from the expansion of the attribute macro `thin_delegate::fill_delegate` (in Nightly builds, run with -Z macro-backtrace for more info)
/// For more information about this error, try `rustc --explain E0424`.
/// ```
///
/// Rust's macro hygienicity forbids use of `self` in declarative macros.
/// We can resolve it by replacing `self` in the token stream by `self` generated in a proc macro,
/// which this `Visitor` does.
;