-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
222 lines (192 loc) · 5.97 KB
/
lib.rs
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
//! All procedural macros used for the various tests, most procedural macros
//! here are pretty self-contained
#![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::*;
// First up, a bunch of helpers to create tokens. These are just meant to make
// the test cases below a little more ergonomic to read
fn tt(kind: TokenNode) -> TokenTree {
TokenTree {
span: Span::default(), // by default we're testing the `default` span
// everywhere because... well... it's default
kind,
}
}
fn call_site_all(t: TokenStream) -> TokenStream {
t.into_iter().map(|mut tt| {
tt.span = Span::call_site();
tt.kind = match tt.kind {
TokenNode::Group(d, ts) => TokenNode::Group(d, call_site_all(ts)),
node => node,
};
tt
}).collect()
}
fn term(s: &str) -> TokenTree {
tt(TokenNode::Term(Term::intern(s)))
}
fn op(c: char, s: Spacing) -> TokenTree {
tt(TokenNode::Op(c, s))
}
fn braces(v: Vec<TokenTree>) -> TokenTree {
tt(TokenNode::Group(Delimiter::Brace, v.into_iter().collect()))
}
fn parse(s: &str) -> TokenTree {
s.parse::<TokenStream>().unwrap().into_iter().next().unwrap()
}
// ============================================================================
//
// Spans of tokens like `super` are very sensitive. This example shows that if
// you have a "bug" (is it a bug?) like [1] it can get super weird.
//
// [1]: https://github.com/dtolnay/quote/pull/51
#[proc_macro]
pub fn failure1(_input: TokenStream) -> TokenStream {
// mod foo {
// use super::*;
// }
let v = vec![
term("mod"), term("foo"),
braces(vec! [
term("use"),
parse("super"), // note that this is `parse` ...
op(':', Spacing::Joint),
op(':', Spacing::Alone),
op('*', Spacing::Alone),
op(';', Spacing::Alone),
]),
];
return v.into_iter().collect()
}
#[proc_macro]
pub fn success1(_input: TokenStream) -> TokenStream {
let v = vec![
term("mod"), term("foo"),
braces(vec! [
term("use"),
term("super"), // ... and this is `term`
op(':', Spacing::Joint),
op(':', Spacing::Alone),
op('*', Spacing::Alone),
op(';', Spacing::Alone),
]),
];
return v.into_iter().collect()
}
// ============================================================================
//
// If you use the default span you can't import your own structs!
#[proc_macro]
pub fn failure2(_input: TokenStream) -> TokenStream {
// struct A;
// mod foo {
// use super::A;
// }
let v = vec![
term("struct"), term("A"), op(';', Spacing::Alone),
term("mod"), term("foo"),
braces(vec! [
term("use"),
term("super"),
op(':', Spacing::Joint),
op(':', Spacing::Alone),
term("A"),
op(';', Spacing::Alone),
]),
];
return v.into_iter().collect()
}
#[proc_macro]
pub fn success2(input: TokenStream) -> TokenStream {
call_site_all(failure2(input))
}
// ============================================================================
// If you use the default span then outside code can't import from the generated
// code, see the test case for more info.
#[proc_macro]
pub fn failure3(_input: TokenStream) -> TokenStream {
let v = vec![
term("pub"), term("struct"), term("A"), op(';', Spacing::Alone),
];
return v.into_iter().collect()
}
#[proc_macro]
pub fn success3(input: TokenStream) -> TokenStream {
call_site_all(failure3(input))
}
// ============================================================================
// If you use the default span you can't import from `std`.
#[proc_macro]
pub fn failure4(_input: TokenStream) -> TokenStream {
let v = vec![
term("use"),
term("std"),
op(':', Spacing::Joint),
op(':', Spacing::Alone),
term("mem"),
op(';', Spacing::Alone),
];
return v.into_iter().collect()
}
#[proc_macro]
pub fn success4(input: TokenStream) -> TokenStream {
call_site_all(failure4(input))
}
// ============================================================================
// If you use the default span you can't import from your own modules
#[proc_macro]
pub fn failure5(_input: TokenStream) -> TokenStream {
// use foo::*;
// mod foo {}
let v = vec![
term("use"),
term("foo"),
op(':', Spacing::Joint),
op(':', Spacing::Alone),
op('*', Spacing::Alone),
op(';', Spacing::Alone),
term("mod"),
term("foo"),
braces(vec![]),
];
return v.into_iter().collect()
}
#[proc_macro]
pub fn success5(input: TokenStream) -> TokenStream {
call_site_all(failure5(input))
}
// ============================================================================
// Here we're using a "malformed" macro named `a` from our local crate `foo`.
// This malformed macro requires the surrounding code to import `std::mem`. If
// we do that, however, it doesn't work!
#[proc_macro]
pub fn failure6(_input: TokenStream) -> TokenStream {
// mod another {
// extern crate std;
// use self::std::mem;
// a! {}
// }
let v = vec![
term("mod"), term("another"), braces(vec![
term("extern"), term("crate"), term("std"), op(';', Spacing::Alone),
term("use"), term("self"),
op(':', Spacing::Joint),
op(':', Spacing::Alone),
term("std"),
op(':', Spacing::Joint),
op(':', Spacing::Alone),
term("mem"),
op(';', Spacing::Alone),
term("a"),
op('!', Spacing::Alone),
braces(vec! [
term("_A"),
]),
]),
];
return v.into_iter().collect()
}
#[proc_macro]
pub fn success6(input: TokenStream) -> TokenStream {
call_site_all(failure6(input))
}