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.
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
21 use syntax::visit::Visitor;
23 #[derive(Copy, Clone, PartialEq)]
33 fn from_item(item: &ast::Item) -> Target {
35 ast::ItemKind::Fn(..) => Target::Fn,
36 ast::ItemKind::Struct(..) => Target::Struct,
37 ast::ItemKind::Union(..) => Target::Union,
38 ast::ItemKind::Enum(..) => Target::Enum,
44 struct CheckAttrVisitor<'a> {
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),
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")
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() {
78 let mut conflicting_reprs = 0;
79 let mut found_packed = false;
80 let mut found_align = false;
84 let name = match word.name() {
89 let (message, label) = match &*name.as_str() {
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")
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",
114 conflicting_reprs += 1;
115 if target != Target::Struct {
116 ("attribute should be applied to struct",
124 if target != Target::Struct {
125 ("attribute should be applied to struct",
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",
144 struct_span_err!(self.sess, attr.span, E0517, "{}", message)
145 .span_label(attr.span, format!("requires {}", label))
148 if conflicting_reprs > 1 {
149 span_warn!(self.sess, attr.span, E0566,
150 "conflicting representation hints");
152 if found_align && found_packed {
153 struct_span_err!(self.sess, attr.span, E0587,
154 "conflicting packed and align representation hints").emit();
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);
165 visit::walk_item(self, item);
169 pub fn check_crate(sess: &Session, krate: &ast::Crate) {
170 visit::walk_crate(&mut CheckAttrVisitor { sess: sess }, krate);