]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/check_attr.rs
Alias std::cmp::max/min to Ord::max/min
[rust.git] / src / librustc / hir / check_attr.rs
1 // Copyright 2015 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 session::Session;
12
13 use syntax::ast;
14 use syntax::visit;
15 use syntax::visit::Visitor;
16
17 #[derive(Copy, Clone, PartialEq)]
18 enum Target {
19     Fn,
20     Struct,
21     Union,
22     Enum,
23     Other,
24 }
25
26 impl Target {
27     fn from_item(item: &ast::Item) -> Target {
28         match item.node {
29             ast::ItemKind::Fn(..) => Target::Fn,
30             ast::ItemKind::Struct(..) => Target::Struct,
31             ast::ItemKind::Union(..) => Target::Union,
32             ast::ItemKind::Enum(..) => Target::Enum,
33             _ => Target::Other,
34         }
35     }
36 }
37
38 struct CheckAttrVisitor<'a> {
39     sess: &'a Session,
40 }
41
42 impl<'a> CheckAttrVisitor<'a> {
43     fn check_inline(&self, attr: &ast::Attribute, target: Target) {
44         if target != Target::Fn {
45             struct_span_err!(self.sess, attr.span, E0518, "attribute should be applied to function")
46                 .span_label(attr.span, "requires a function")
47                 .emit();
48         }
49     }
50
51     fn check_repr(&self, attr: &ast::Attribute, target: Target) {
52         let words = match attr.meta_item_list() {
53             Some(words) => words,
54             None => {
55                 return;
56             }
57         };
58
59         let mut conflicting_reprs = 0;
60         let mut found_packed = false;
61         let mut found_align = false;
62
63         for word in words {
64
65             let name = match word.name() {
66                 Some(word) => word,
67                 None => continue,
68             };
69
70             let (message, label) = match &*name.as_str() {
71                 "C" => {
72                     conflicting_reprs += 1;
73                     if target != Target::Struct &&
74                             target != Target::Union &&
75                             target != Target::Enum {
76                                 ("attribute should be applied to struct, enum or union",
77                                  "a struct, enum or union")
78                     } else {
79                         continue
80                     }
81                 }
82                 "packed" => {
83                     // Do not increment conflicting_reprs here, because "packed"
84                     // can be used to modify another repr hint
85                     if target != Target::Struct &&
86                             target != Target::Union {
87                                 ("attribute should be applied to struct or union",
88                                  "a struct or union")
89                     } else {
90                         found_packed = true;
91                         continue
92                     }
93                 }
94                 "simd" => {
95                     conflicting_reprs += 1;
96                     if target != Target::Struct {
97                         ("attribute should be applied to struct",
98                          "a struct")
99                     } else {
100                         continue
101                     }
102                 }
103                 "align" => {
104                     found_align = true;
105                     if target != Target::Struct {
106                         ("attribute should be applied to struct",
107                          "a struct")
108                     } else {
109                         continue
110                     }
111                 }
112                 "i8" | "u8" | "i16" | "u16" |
113                 "i32" | "u32" | "i64" | "u64" |
114                 "isize" | "usize" => {
115                     conflicting_reprs += 1;
116                     if target != Target::Enum {
117                         ("attribute should be applied to enum",
118                          "an enum")
119                     } else {
120                         continue
121                     }
122                 }
123                 _ => continue,
124             };
125             struct_span_err!(self.sess, attr.span, E0517, "{}", message)
126                 .span_label(attr.span, format!("requires {}", label))
127                 .emit();
128         }
129         if conflicting_reprs > 1 {
130             span_warn!(self.sess, attr.span, E0566,
131                        "conflicting representation hints");
132         }
133         if found_align && found_packed {
134             struct_span_err!(self.sess, attr.span, E0587,
135                              "conflicting packed and align representation hints").emit();
136         }
137     }
138
139     fn check_attribute(&self, attr: &ast::Attribute, target: Target) {
140         if let Some(name) = attr.name() {
141             match &*name.as_str() {
142                 "inline" => self.check_inline(attr, target),
143                 "repr" => self.check_repr(attr, target),
144                 _ => (),
145             }
146         }
147     }
148 }
149
150 impl<'a> Visitor<'a> for CheckAttrVisitor<'a> {
151     fn visit_item(&mut self, item: &'a ast::Item) {
152         let target = Target::from_item(item);
153         for attr in &item.attrs {
154             self.check_attribute(attr, target);
155         }
156         visit::walk_item(self, item);
157     }
158 }
159
160 pub fn check_crate(sess: &Session, krate: &ast::Crate) {
161     visit::walk_crate(&mut CheckAttrVisitor { sess: sess }, krate);
162 }