]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/ext/deriving/bounds.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / libsyntax / ext / deriving / bounds.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use ast::{MetaItem, MetaWord, Item};
12 use codemap::Span;
13 use ext::base::ExtCtxt;
14 use ext::deriving::generic::*;
15 use ext::deriving::generic::ty::*;
16 use ptr::P;
17
18 pub fn expand_deriving_bound<F>(cx: &mut ExtCtxt,
19                                 span: Span,
20                                 mitem: &MetaItem,
21                                 item: &Item,
22                                 push: F) where
23     F: FnOnce(P<Item>),
24 {
25     let name = match mitem.node {
26         MetaWord(ref tname) => {
27             match &tname[..] {
28                 "Copy" => "Copy",
29                 "Send" | "Sync" => {
30                     return cx.span_err(span,
31                                        &format!("{} is an unsafe trait and it \
32                                                  should be implemented explicitly",
33                                                 *tname))
34                 }
35                 ref tname => {
36                     cx.span_bug(span,
37                                 &format!("expected built-in trait name but \
38                                           found {}", *tname))
39                 }
40             }
41         },
42         _ => {
43             return cx.span_err(span, "unexpected value in deriving, expected \
44                                       a trait")
45         }
46     };
47
48     let path = Path::new(vec![
49         if cx.use_std { "std" } else { "core" },
50         "marker",
51         name
52     ]);
53
54     let trait_def = TraitDef {
55         span: span,
56         attributes: Vec::new(),
57         path: path,
58         additional_bounds: Vec::new(),
59         generics: LifetimeBounds::empty(),
60         methods: Vec::new(),
61         associated_types: Vec::new(),
62     };
63
64     trait_def.expand(cx, mitem, item, push)
65 }