]> git.lizzy.rs Git - rust.git/blob - src/librustc/hir/check_attr.rs
Auto merge of #35856 - phimuemue:master, r=brson
[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     Enum,
22     Other,
23 }
24
25 impl Target {
26     fn from_item(item: &ast::Item) -> Target {
27         match item.node {
28             ast::ItemKind::Fn(..) => Target::Fn,
29             ast::ItemKind::Struct(..) => Target::Struct,
30             ast::ItemKind::Enum(..) => Target::Enum,
31             _ => Target::Other,
32         }
33     }
34 }
35
36 struct CheckAttrVisitor<'a> {
37     sess: &'a Session,
38 }
39
40 impl<'a> CheckAttrVisitor<'a> {
41     fn check_inline(&self, attr: &ast::Attribute, target: Target) {
42         if target != Target::Fn {
43             span_err!(self.sess, attr.span, E0518, "attribute should be applied to function");
44         }
45     }
46
47     fn check_repr(&self, attr: &ast::Attribute, target: Target) {
48         let words = match attr.meta_item_list() {
49             Some(words) => words,
50             None => {
51                 return;
52             }
53         };
54
55         let mut conflicting_reprs = 0;
56         for word in words {
57             let name = match word.name() {
58                 Some(word) => word,
59                 None => continue,
60             };
61
62             let message = match &*name {
63                 "C" => {
64                     conflicting_reprs += 1;
65                     if target != Target::Struct && target != Target::Enum {
66                         "attribute should be applied to struct or enum"
67                     } else {
68                         continue
69                     }
70                 }
71                 "packed" => {
72                     // Do not increment conflicting_reprs here, because "packed"
73                     // can be used to modify another repr hint
74                     if target != Target::Struct {
75                         "attribute should be applied to struct"
76                     } else {
77                         continue
78                     }
79                 }
80                 "simd" => {
81                     conflicting_reprs += 1;
82                     if target != Target::Struct {
83                         "attribute should be applied to struct"
84                     } else {
85                         continue
86                     }
87                 }
88                 "i8" | "u8" | "i16" | "u16" |
89                 "i32" | "u32" | "i64" | "u64" |
90                 "isize" | "usize" => {
91                     conflicting_reprs += 1;
92                     if target != Target::Enum {
93                         "attribute should be applied to enum"
94                     } else {
95                         continue
96                     }
97                 }
98                 _ => continue,
99             };
100
101             span_err!(self.sess, attr.span, E0517, "{}", message);
102         }
103         if conflicting_reprs > 1 {
104             span_warn!(self.sess, attr.span, E0566,
105                        "conflicting representation hints");
106         }
107     }
108
109     fn check_attribute(&self, attr: &ast::Attribute, target: Target) {
110         let name: &str = &attr.name();
111         match name {
112             "inline" => self.check_inline(attr, target),
113             "repr" => self.check_repr(attr, target),
114             _ => (),
115         }
116     }
117 }
118
119 impl<'a> Visitor for CheckAttrVisitor<'a> {
120     fn visit_item(&mut self, item: &ast::Item) {
121         let target = Target::from_item(item);
122         for attr in &item.attrs {
123             self.check_attribute(attr, target);
124         }
125         visit::walk_item(self, item);
126     }
127 }
128
129 pub fn check_crate(sess: &Session, krate: &ast::Crate) {
130     visit::walk_crate(&mut CheckAttrVisitor { sess: sess }, krate);
131 }