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.
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.
15 use syntax::visit::Visitor;
17 #[derive(Copy, Clone, PartialEq)]
27 fn from_item(item: &ast::Item) -> Target {
29 ast::ItemKind::Fn(..) => Target::Fn,
30 ast::ItemKind::Struct(..) => Target::Struct,
31 ast::ItemKind::Union(..) => Target::Union,
32 ast::ItemKind::Enum(..) => Target::Enum,
38 struct CheckAttrVisitor<'a> {
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")
51 fn check_repr(&self, attr: &ast::Attribute, target: Target) {
52 let words = match attr.meta_item_list() {
59 let mut conflicting_reprs = 0;
60 let mut found_packed = false;
61 let mut found_align = false;
65 let name = match word.name() {
70 let (message, label) = match &*name.as_str() {
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")
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",
95 conflicting_reprs += 1;
96 if target != Target::Struct {
97 ("attribute should be applied to struct",
105 if target != Target::Struct {
106 ("attribute should be applied to struct",
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",
125 struct_span_err!(self.sess, attr.span, E0517, "{}", message)
126 .span_label(attr.span, format!("requires {}", label))
129 if conflicting_reprs > 1 {
130 span_warn!(self.sess, attr.span, E0566,
131 "conflicting representation hints");
133 if found_align && found_packed {
134 struct_span_err!(self.sess, attr.span, E0587,
135 "conflicting packed and align representation hints").emit();
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),
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);
156 visit::walk_item(self, item);
160 pub fn check_crate(sess: &Session, krate: &ast::Crate) {
161 visit::walk_crate(&mut CheckAttrVisitor { sess: sess }, krate);