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