]> git.lizzy.rs Git - rust.git/blob - src/librustc_expand/build.rs
Rollup merge of #67837 - GuillaumeGomez:clean-up-err-codes, r=Dylan-DPC
[rust.git] / src / librustc_expand / build.rs
1 use crate::base::ExtCtxt;
2
3 use rustc_span::source_map::{respan, Spanned};
4 use rustc_span::symbol::{kw, sym, Symbol};
5 use syntax::ast::{self, AttrVec, BlockCheckMode, Expr, Ident, PatKind, UnOp};
6 use syntax::attr;
7 use syntax::ptr::P;
8
9 use rustc_span::{Pos, Span};
10
11 impl<'a> ExtCtxt<'a> {
12     pub fn path(&self, span: Span, strs: Vec<ast::Ident>) -> ast::Path {
13         self.path_all(span, false, strs, vec![])
14     }
15     pub fn path_ident(&self, span: Span, id: ast::Ident) -> ast::Path {
16         self.path(span, vec![id])
17     }
18     pub fn path_global(&self, span: Span, strs: Vec<ast::Ident>) -> ast::Path {
19         self.path_all(span, true, strs, vec![])
20     }
21     pub fn path_all(
22         &self,
23         span: Span,
24         global: bool,
25         mut idents: Vec<ast::Ident>,
26         args: Vec<ast::GenericArg>,
27     ) -> ast::Path {
28         assert!(!idents.is_empty());
29         let add_root = global && !idents[0].is_path_segment_keyword();
30         let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
31         if add_root {
32             segments.push(ast::PathSegment::path_root(span));
33         }
34         let last_ident = idents.pop().unwrap();
35         segments.extend(
36             idents.into_iter().map(|ident| ast::PathSegment::from_ident(ident.with_span_pos(span))),
37         );
38         let args = if !args.is_empty() {
39             ast::AngleBracketedArgs { args, constraints: Vec::new(), span }.into()
40         } else {
41             None
42         };
43         segments.push(ast::PathSegment {
44             ident: last_ident.with_span_pos(span),
45             id: ast::DUMMY_NODE_ID,
46             args,
47         });
48         ast::Path { span, segments }
49     }
50
51     pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
52         ast::MutTy { ty, mutbl }
53     }
54
55     pub fn ty(&self, span: Span, kind: ast::TyKind) -> P<ast::Ty> {
56         P(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind })
57     }
58
59     pub fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
60         self.ty(path.span, ast::TyKind::Path(None, path))
61     }
62
63     // Might need to take bounds as an argument in the future, if you ever want
64     // to generate a bounded existential trait type.
65     pub fn ty_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Ty> {
66         self.ty_path(self.path_ident(span, ident))
67     }
68
69     pub fn anon_const(&self, span: Span, kind: ast::ExprKind) -> ast::AnonConst {
70         ast::AnonConst {
71             id: ast::DUMMY_NODE_ID,
72             value: P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new() }),
73         }
74     }
75
76     pub fn const_ident(&self, span: Span, ident: ast::Ident) -> ast::AnonConst {
77         self.anon_const(span, ast::ExprKind::Path(None, self.path_ident(span, ident)))
78     }
79
80     pub fn ty_rptr(
81         &self,
82         span: Span,
83         ty: P<ast::Ty>,
84         lifetime: Option<ast::Lifetime>,
85         mutbl: ast::Mutability,
86     ) -> P<ast::Ty> {
87         self.ty(span, ast::TyKind::Rptr(lifetime, self.ty_mt(ty, mutbl)))
88     }
89
90     pub fn ty_ptr(&self, span: Span, ty: P<ast::Ty>, mutbl: ast::Mutability) -> P<ast::Ty> {
91         self.ty(span, ast::TyKind::Ptr(self.ty_mt(ty, mutbl)))
92     }
93
94     pub fn typaram(
95         &self,
96         span: Span,
97         ident: ast::Ident,
98         attrs: Vec<ast::Attribute>,
99         bounds: ast::GenericBounds,
100         default: Option<P<ast::Ty>>,
101     ) -> ast::GenericParam {
102         ast::GenericParam {
103             ident: ident.with_span_pos(span),
104             id: ast::DUMMY_NODE_ID,
105             attrs: attrs.into(),
106             bounds,
107             kind: ast::GenericParamKind::Type { default },
108             is_placeholder: false,
109         }
110     }
111
112     pub fn trait_ref(&self, path: ast::Path) -> ast::TraitRef {
113         ast::TraitRef { path, ref_id: ast::DUMMY_NODE_ID }
114     }
115
116     pub fn poly_trait_ref(&self, span: Span, path: ast::Path) -> ast::PolyTraitRef {
117         ast::PolyTraitRef {
118             bound_generic_params: Vec::new(),
119             trait_ref: self.trait_ref(path),
120             span,
121         }
122     }
123
124     pub fn trait_bound(&self, path: ast::Path) -> ast::GenericBound {
125         ast::GenericBound::Trait(
126             self.poly_trait_ref(path.span, path),
127             ast::TraitBoundModifier::None,
128         )
129     }
130
131     pub fn lifetime(&self, span: Span, ident: ast::Ident) -> ast::Lifetime {
132         ast::Lifetime { id: ast::DUMMY_NODE_ID, ident: ident.with_span_pos(span) }
133     }
134
135     pub fn lifetime_def(
136         &self,
137         span: Span,
138         ident: ast::Ident,
139         attrs: Vec<ast::Attribute>,
140         bounds: ast::GenericBounds,
141     ) -> ast::GenericParam {
142         let lifetime = self.lifetime(span, ident);
143         ast::GenericParam {
144             ident: lifetime.ident,
145             id: lifetime.id,
146             attrs: attrs.into(),
147             bounds,
148             kind: ast::GenericParamKind::Lifetime,
149             is_placeholder: false,
150         }
151     }
152
153     pub fn stmt_expr(&self, expr: P<ast::Expr>) -> ast::Stmt {
154         ast::Stmt { id: ast::DUMMY_NODE_ID, span: expr.span, kind: ast::StmtKind::Expr(expr) }
155     }
156
157     pub fn stmt_let(
158         &self,
159         sp: Span,
160         mutbl: bool,
161         ident: ast::Ident,
162         ex: P<ast::Expr>,
163     ) -> ast::Stmt {
164         let pat = if mutbl {
165             let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Mut);
166             self.pat_ident_binding_mode(sp, ident, binding_mode)
167         } else {
168             self.pat_ident(sp, ident)
169         };
170         let local = P(ast::Local {
171             pat,
172             ty: None,
173             init: Some(ex),
174             id: ast::DUMMY_NODE_ID,
175             span: sp,
176             attrs: AttrVec::new(),
177         });
178         ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span: sp }
179     }
180
181     // Generates `let _: Type;`, which is usually used for type assertions.
182     pub fn stmt_let_type_only(&self, span: Span, ty: P<ast::Ty>) -> ast::Stmt {
183         let local = P(ast::Local {
184             pat: self.pat_wild(span),
185             ty: Some(ty),
186             init: None,
187             id: ast::DUMMY_NODE_ID,
188             span,
189             attrs: AttrVec::new(),
190         });
191         ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Local(local), span }
192     }
193
194     pub fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> ast::Stmt {
195         ast::Stmt { id: ast::DUMMY_NODE_ID, kind: ast::StmtKind::Item(item), span: sp }
196     }
197
198     pub fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
199         self.block(
200             expr.span,
201             vec![ast::Stmt {
202                 id: ast::DUMMY_NODE_ID,
203                 span: expr.span,
204                 kind: ast::StmtKind::Expr(expr),
205             }],
206         )
207     }
208     pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
209         P(ast::Block { stmts, id: ast::DUMMY_NODE_ID, rules: BlockCheckMode::Default, span })
210     }
211
212     pub fn expr(&self, span: Span, kind: ast::ExprKind) -> P<ast::Expr> {
213         P(ast::Expr { id: ast::DUMMY_NODE_ID, kind, span, attrs: AttrVec::new() })
214     }
215
216     pub fn expr_path(&self, path: ast::Path) -> P<ast::Expr> {
217         self.expr(path.span, ast::ExprKind::Path(None, path))
218     }
219
220     pub fn expr_ident(&self, span: Span, id: ast::Ident) -> P<ast::Expr> {
221         self.expr_path(self.path_ident(span, id))
222     }
223     pub fn expr_self(&self, span: Span) -> P<ast::Expr> {
224         self.expr_ident(span, Ident::with_dummy_span(kw::SelfLower))
225     }
226
227     pub fn expr_binary(
228         &self,
229         sp: Span,
230         op: ast::BinOpKind,
231         lhs: P<ast::Expr>,
232         rhs: P<ast::Expr>,
233     ) -> P<ast::Expr> {
234         self.expr(sp, ast::ExprKind::Binary(Spanned { node: op, span: sp }, lhs, rhs))
235     }
236
237     pub fn expr_deref(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
238         self.expr(sp, ast::ExprKind::Unary(UnOp::Deref, e))
239     }
240
241     pub fn expr_addr_of(&self, sp: Span, e: P<ast::Expr>) -> P<ast::Expr> {
242         self.expr(sp, ast::ExprKind::AddrOf(ast::BorrowKind::Ref, ast::Mutability::Not, e))
243     }
244
245     pub fn expr_call(
246         &self,
247         span: Span,
248         expr: P<ast::Expr>,
249         args: Vec<P<ast::Expr>>,
250     ) -> P<ast::Expr> {
251         self.expr(span, ast::ExprKind::Call(expr, args))
252     }
253     pub fn expr_call_ident(
254         &self,
255         span: Span,
256         id: ast::Ident,
257         args: Vec<P<ast::Expr>>,
258     ) -> P<ast::Expr> {
259         self.expr(span, ast::ExprKind::Call(self.expr_ident(span, id), args))
260     }
261     pub fn expr_call_global(
262         &self,
263         sp: Span,
264         fn_path: Vec<ast::Ident>,
265         args: Vec<P<ast::Expr>>,
266     ) -> P<ast::Expr> {
267         let pathexpr = self.expr_path(self.path_global(sp, fn_path));
268         self.expr_call(sp, pathexpr, args)
269     }
270     pub fn expr_method_call(
271         &self,
272         span: Span,
273         expr: P<ast::Expr>,
274         ident: ast::Ident,
275         mut args: Vec<P<ast::Expr>>,
276     ) -> P<ast::Expr> {
277         args.insert(0, expr);
278         let segment = ast::PathSegment::from_ident(ident.with_span_pos(span));
279         self.expr(span, ast::ExprKind::MethodCall(segment, args))
280     }
281     pub fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
282         self.expr(b.span, ast::ExprKind::Block(b, None))
283     }
284     pub fn field_imm(&self, span: Span, ident: Ident, e: P<ast::Expr>) -> ast::Field {
285         ast::Field {
286             ident: ident.with_span_pos(span),
287             expr: e,
288             span,
289             is_shorthand: false,
290             attrs: AttrVec::new(),
291             id: ast::DUMMY_NODE_ID,
292             is_placeholder: false,
293         }
294     }
295     pub fn expr_struct(
296         &self,
297         span: Span,
298         path: ast::Path,
299         fields: Vec<ast::Field>,
300     ) -> P<ast::Expr> {
301         self.expr(span, ast::ExprKind::Struct(path, fields, None))
302     }
303     pub fn expr_struct_ident(
304         &self,
305         span: Span,
306         id: ast::Ident,
307         fields: Vec<ast::Field>,
308     ) -> P<ast::Expr> {
309         self.expr_struct(span, self.path_ident(span, id), fields)
310     }
311
312     pub fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> {
313         let lit = ast::Lit::from_lit_kind(lit_kind, span);
314         self.expr(span, ast::ExprKind::Lit(lit))
315     }
316     pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> {
317         self.expr_lit(
318             span,
319             ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
320         )
321     }
322     pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> {
323         self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32)))
324     }
325     pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> {
326         self.expr_lit(sp, ast::LitKind::Bool(value))
327     }
328
329     pub fn expr_vec(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
330         self.expr(sp, ast::ExprKind::Array(exprs))
331     }
332     pub fn expr_vec_slice(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
333         self.expr_addr_of(sp, self.expr_vec(sp, exprs))
334     }
335     pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> {
336         self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked))
337     }
338
339     pub fn expr_cast(&self, sp: Span, expr: P<ast::Expr>, ty: P<ast::Ty>) -> P<ast::Expr> {
340         self.expr(sp, ast::ExprKind::Cast(expr, ty))
341     }
342
343     pub fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
344         let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
345         self.expr_call_global(sp, some, vec![expr])
346     }
347
348     pub fn expr_tuple(&self, sp: Span, exprs: Vec<P<ast::Expr>>) -> P<ast::Expr> {
349         self.expr(sp, ast::ExprKind::Tup(exprs))
350     }
351
352     pub fn expr_fail(&self, span: Span, msg: Symbol) -> P<ast::Expr> {
353         let loc = self.source_map().lookup_char_pos(span.lo());
354         let expr_file = self.expr_str(span, Symbol::intern(&loc.file.name.to_string()));
355         let expr_line = self.expr_u32(span, loc.line as u32);
356         let expr_col = self.expr_u32(span, loc.col.to_usize() as u32 + 1);
357         let expr_loc_tuple = self.expr_tuple(span, vec![expr_file, expr_line, expr_col]);
358         let expr_loc_ptr = self.expr_addr_of(span, expr_loc_tuple);
359         self.expr_call_global(
360             span,
361             [sym::std, sym::rt, sym::begin_panic].iter().map(|s| Ident::new(*s, span)).collect(),
362             vec![self.expr_str(span, msg), expr_loc_ptr],
363         )
364     }
365
366     pub fn expr_unreachable(&self, span: Span) -> P<ast::Expr> {
367         self.expr_fail(span, Symbol::intern("internal error: entered unreachable code"))
368     }
369
370     pub fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
371         let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
372         self.expr_call_global(sp, ok, vec![expr])
373     }
374
375     pub fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
376         let ok = self.std_path(&[sym::result, sym::Result, sym::Ok]);
377         let ok_path = self.path_global(sp, ok);
378         let err = self.std_path(&[sym::result, sym::Result, sym::Err]);
379         let err_path = self.path_global(sp, err);
380
381         let binding_variable = self.ident_of("__try_var", sp);
382         let binding_pat = self.pat_ident(sp, binding_variable);
383         let binding_expr = self.expr_ident(sp, binding_variable);
384
385         // `Ok(__try_var)` pattern
386         let ok_pat = self.pat_tuple_struct(sp, ok_path, vec![binding_pat.clone()]);
387
388         // `Err(__try_var)` (pattern and expression respectively)
389         let err_pat = self.pat_tuple_struct(sp, err_path.clone(), vec![binding_pat]);
390         let err_inner_expr =
391             self.expr_call(sp, self.expr_path(err_path), vec![binding_expr.clone()]);
392         // `return Err(__try_var)`
393         let err_expr = self.expr(sp, ast::ExprKind::Ret(Some(err_inner_expr)));
394
395         // `Ok(__try_var) => __try_var`
396         let ok_arm = self.arm(sp, ok_pat, binding_expr);
397         // `Err(__try_var) => return Err(__try_var)`
398         let err_arm = self.arm(sp, err_pat, err_expr);
399
400         // `match head { Ok() => ..., Err() => ... }`
401         self.expr_match(sp, head, vec![ok_arm, err_arm])
402     }
403
404     pub fn pat(&self, span: Span, kind: PatKind) -> P<ast::Pat> {
405         P(ast::Pat { id: ast::DUMMY_NODE_ID, kind, span })
406     }
407     pub fn pat_wild(&self, span: Span) -> P<ast::Pat> {
408         self.pat(span, PatKind::Wild)
409     }
410     pub fn pat_lit(&self, span: Span, expr: P<ast::Expr>) -> P<ast::Pat> {
411         self.pat(span, PatKind::Lit(expr))
412     }
413     pub fn pat_ident(&self, span: Span, ident: ast::Ident) -> P<ast::Pat> {
414         let binding_mode = ast::BindingMode::ByValue(ast::Mutability::Not);
415         self.pat_ident_binding_mode(span, ident, binding_mode)
416     }
417
418     pub fn pat_ident_binding_mode(
419         &self,
420         span: Span,
421         ident: ast::Ident,
422         bm: ast::BindingMode,
423     ) -> P<ast::Pat> {
424         let pat = PatKind::Ident(bm, ident.with_span_pos(span), None);
425         self.pat(span, pat)
426     }
427     pub fn pat_path(&self, span: Span, path: ast::Path) -> P<ast::Pat> {
428         self.pat(span, PatKind::Path(None, path))
429     }
430     pub fn pat_tuple_struct(
431         &self,
432         span: Span,
433         path: ast::Path,
434         subpats: Vec<P<ast::Pat>>,
435     ) -> P<ast::Pat> {
436         self.pat(span, PatKind::TupleStruct(path, subpats))
437     }
438     pub fn pat_struct(
439         &self,
440         span: Span,
441         path: ast::Path,
442         field_pats: Vec<ast::FieldPat>,
443     ) -> P<ast::Pat> {
444         self.pat(span, PatKind::Struct(path, field_pats, false))
445     }
446     pub fn pat_tuple(&self, span: Span, pats: Vec<P<ast::Pat>>) -> P<ast::Pat> {
447         self.pat(span, PatKind::Tuple(pats))
448     }
449
450     pub fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
451         let some = self.std_path(&[sym::option, sym::Option, sym::Some]);
452         let path = self.path_global(span, some);
453         self.pat_tuple_struct(span, path, vec![pat])
454     }
455
456     pub fn pat_none(&self, span: Span) -> P<ast::Pat> {
457         let some = self.std_path(&[sym::option, sym::Option, sym::None]);
458         let path = self.path_global(span, some);
459         self.pat_path(span, path)
460     }
461
462     pub fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
463         let some = self.std_path(&[sym::result, sym::Result, sym::Ok]);
464         let path = self.path_global(span, some);
465         self.pat_tuple_struct(span, path, vec![pat])
466     }
467
468     pub fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
469         let some = self.std_path(&[sym::result, sym::Result, sym::Err]);
470         let path = self.path_global(span, some);
471         self.pat_tuple_struct(span, path, vec![pat])
472     }
473
474     pub fn arm(&self, span: Span, pat: P<ast::Pat>, expr: P<ast::Expr>) -> ast::Arm {
475         ast::Arm {
476             attrs: vec![],
477             pat,
478             guard: None,
479             body: expr,
480             span,
481             id: ast::DUMMY_NODE_ID,
482             is_placeholder: false,
483         }
484     }
485
486     pub fn arm_unreachable(&self, span: Span) -> ast::Arm {
487         self.arm(span, self.pat_wild(span), self.expr_unreachable(span))
488     }
489
490     pub fn expr_match(&self, span: Span, arg: P<ast::Expr>, arms: Vec<ast::Arm>) -> P<Expr> {
491         self.expr(span, ast::ExprKind::Match(arg, arms))
492     }
493
494     pub fn expr_if(
495         &self,
496         span: Span,
497         cond: P<ast::Expr>,
498         then: P<ast::Expr>,
499         els: Option<P<ast::Expr>>,
500     ) -> P<ast::Expr> {
501         let els = els.map(|x| self.expr_block(self.block_expr(x)));
502         self.expr(span, ast::ExprKind::If(cond, self.block_expr(then), els))
503     }
504
505     pub fn lambda_fn_decl(
506         &self,
507         span: Span,
508         fn_decl: P<ast::FnDecl>,
509         body: P<ast::Expr>,
510         fn_decl_span: Span,
511     ) -> P<ast::Expr> {
512         self.expr(
513             span,
514             ast::ExprKind::Closure(
515                 ast::CaptureBy::Ref,
516                 ast::IsAsync::NotAsync,
517                 ast::Movability::Movable,
518                 fn_decl,
519                 body,
520                 fn_decl_span,
521             ),
522         )
523     }
524
525     pub fn lambda(&self, span: Span, ids: Vec<ast::Ident>, body: P<ast::Expr>) -> P<ast::Expr> {
526         let fn_decl = self.fn_decl(
527             ids.iter().map(|id| self.param(span, *id, self.ty(span, ast::TyKind::Infer))).collect(),
528             ast::FunctionRetTy::Default(span),
529         );
530
531         // FIXME -- We are using `span` as the span of the `|...|`
532         // part of the lambda, but it probably (maybe?) corresponds to
533         // the entire lambda body. Probably we should extend the API
534         // here, but that's not entirely clear.
535         self.expr(
536             span,
537             ast::ExprKind::Closure(
538                 ast::CaptureBy::Ref,
539                 ast::IsAsync::NotAsync,
540                 ast::Movability::Movable,
541                 fn_decl,
542                 body,
543                 span,
544             ),
545         )
546     }
547
548     pub fn lambda0(&self, span: Span, body: P<ast::Expr>) -> P<ast::Expr> {
549         self.lambda(span, Vec::new(), body)
550     }
551
552     pub fn lambda1(&self, span: Span, body: P<ast::Expr>, ident: ast::Ident) -> P<ast::Expr> {
553         self.lambda(span, vec![ident], body)
554     }
555
556     pub fn lambda_stmts_1(
557         &self,
558         span: Span,
559         stmts: Vec<ast::Stmt>,
560         ident: ast::Ident,
561     ) -> P<ast::Expr> {
562         self.lambda1(span, self.expr_block(self.block(span, stmts)), ident)
563     }
564
565     pub fn param(&self, span: Span, ident: ast::Ident, ty: P<ast::Ty>) -> ast::Param {
566         let arg_pat = self.pat_ident(span, ident);
567         ast::Param {
568             attrs: AttrVec::default(),
569             id: ast::DUMMY_NODE_ID,
570             pat: arg_pat,
571             span,
572             ty,
573             is_placeholder: false,
574         }
575     }
576
577     // FIXME: unused `self`
578     pub fn fn_decl(&self, inputs: Vec<ast::Param>, output: ast::FunctionRetTy) -> P<ast::FnDecl> {
579         P(ast::FnDecl { inputs, output })
580     }
581
582     pub fn item(
583         &self,
584         span: Span,
585         name: Ident,
586         attrs: Vec<ast::Attribute>,
587         kind: ast::ItemKind,
588     ) -> P<ast::Item> {
589         // FIXME: Would be nice if our generated code didn't violate
590         // Rust coding conventions
591         P(ast::Item {
592             ident: name,
593             attrs,
594             id: ast::DUMMY_NODE_ID,
595             kind,
596             vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
597             span,
598             tokens: None,
599         })
600     }
601
602     pub fn variant(&self, span: Span, ident: Ident, tys: Vec<P<ast::Ty>>) -> ast::Variant {
603         let vis_span = span.shrink_to_lo();
604         let fields: Vec<_> = tys
605             .into_iter()
606             .map(|ty| ast::StructField {
607                 span: ty.span,
608                 ty,
609                 ident: None,
610                 vis: respan(vis_span, ast::VisibilityKind::Inherited),
611                 attrs: Vec::new(),
612                 id: ast::DUMMY_NODE_ID,
613                 is_placeholder: false,
614             })
615             .collect();
616
617         let vdata = if fields.is_empty() {
618             ast::VariantData::Unit(ast::DUMMY_NODE_ID)
619         } else {
620             ast::VariantData::Tuple(fields, ast::DUMMY_NODE_ID)
621         };
622
623         ast::Variant {
624             attrs: Vec::new(),
625             data: vdata,
626             disr_expr: None,
627             id: ast::DUMMY_NODE_ID,
628             ident,
629             vis: respan(vis_span, ast::VisibilityKind::Inherited),
630             span,
631             is_placeholder: false,
632         }
633     }
634
635     pub fn item_static(
636         &self,
637         span: Span,
638         name: Ident,
639         ty: P<ast::Ty>,
640         mutbl: ast::Mutability,
641         expr: P<ast::Expr>,
642     ) -> P<ast::Item> {
643         self.item(span, name, Vec::new(), ast::ItemKind::Static(ty, mutbl, expr))
644     }
645
646     pub fn item_const(
647         &self,
648         span: Span,
649         name: Ident,
650         ty: P<ast::Ty>,
651         expr: P<ast::Expr>,
652     ) -> P<ast::Item> {
653         self.item(span, name, Vec::new(), ast::ItemKind::Const(ty, expr))
654     }
655
656     pub fn attribute(&self, mi: ast::MetaItem) -> ast::Attribute {
657         attr::mk_attr_outer(mi)
658     }
659
660     pub fn meta_word(&self, sp: Span, w: ast::Name) -> ast::MetaItem {
661         attr::mk_word_item(Ident::new(w, sp))
662     }
663 }