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 hir::intravisit::{self, Visitor, NestedVisitorMap};
23 #[derive(Copy, Clone, PartialEq)]
39 fn from_item(item: &hir::Item) -> Target {
41 hir::ItemFn(..) => Target::Fn,
42 hir::ItemStruct(..) => Target::Struct,
43 hir::ItemUnion(..) => Target::Union,
44 hir::ItemEnum(..) => Target::Enum,
45 hir::ItemConst(..) => Target::Const,
46 hir::ItemForeignMod(..) => Target::ForeignMod,
47 hir::ItemStatic(..) => Target::Static,
53 struct CheckAttrVisitor<'a, 'tcx: 'a> {
54 tcx: TyCtxt<'a, 'tcx, 'tcx>,
57 impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
58 /// Check any attribute.
59 fn check_attributes(&self, item: &hir::Item, target: Target) {
60 if target == Target::Fn {
61 self.tcx.codegen_fn_attrs(self.tcx.hir.local_def_id(item.id));
62 } else if let Some(a) = item.attrs.iter().find(|a| a.check_name("target_feature")) {
63 self.tcx.sess.struct_span_err(a.span, "attribute should be applied to a function")
64 .span_label(item.span, "not a function")
68 let mut has_wasm_import_module = false;
69 for attr in &item.attrs {
70 if attr.check_name("inline") {
71 self.check_inline(attr, &item.span, target)
72 } else if attr.check_name("non_exhaustive") {
73 self.check_non_exhaustive(attr, item, target)
74 } else if attr.check_name("wasm_import_module") {
75 has_wasm_import_module = true;
76 if attr.value_str().is_none() {
77 self.tcx.sess.span_err(attr.span, "\
78 must be of the form #[wasm_import_module = \"...\"]");
80 if target != Target::ForeignMod {
81 self.tcx.sess.span_err(attr.span, "\
82 must only be attached to foreign modules");
84 } else if attr.check_name("wasm_custom_section") {
85 if target != Target::Const {
86 self.tcx.sess.span_err(attr.span, "only allowed on consts");
89 if attr.value_str().is_none() {
90 self.tcx.sess.span_err(attr.span, "must be of the form \
91 #[wasm_custom_section = \"foo\"]");
96 if target == Target::ForeignMod &&
97 !has_wasm_import_module &&
98 self.tcx.sess.target.target.arch == "wasm32" &&
99 false // FIXME: eventually enable this warning when stable
101 self.tcx.sess.span_warn(item.span, "\
102 must have a #[wasm_import_module = \"...\"] attribute, this \
103 will become a hard error before too long");
106 self.check_repr(item, target);
107 self.check_used(item, target);
110 /// Check if an `#[inline]` is applied to a function or a closure.
111 fn check_inline(&self, attr: &hir::Attribute, span: &Span, target: Target) {
112 if target != Target::Fn && target != Target::Closure {
113 struct_span_err!(self.tcx.sess,
116 "attribute should be applied to function or closure")
117 .span_label(*span, "not a function or closure")
122 /// Check if the `#[non_exhaustive]` attribute on an `item` is valid.
123 fn check_non_exhaustive(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) {
125 Target::Struct | Target::Enum => { /* Valid */ },
127 struct_span_err!(self.tcx.sess,
130 "attribute can only be applied to a struct or enum")
131 .span_label(item.span, "not a struct or enum")
137 if attr.meta_item_list().is_some() || attr.value_str().is_some() {
138 struct_span_err!(self.tcx.sess,
141 "attribute should be empty")
142 .span_label(item.span, "not empty")
147 /// Check if the `#[repr]` attributes on `item` are valid.
148 fn check_repr(&self, item: &hir::Item, target: Target) {
149 // Extract the names of all repr hints, e.g., [foo, bar, align] for:
152 // #[repr(bar, align(8))]
154 let hints: Vec<_> = item.attrs
156 .filter(|attr| attr.name() == "repr")
157 .filter_map(|attr| attr.meta_item_list())
158 .flat_map(|hints| hints)
161 let mut int_reprs = 0;
162 let mut is_c = false;
163 let mut is_simd = false;
164 let mut is_transparent = false;
167 let name = if let Some(name) = hint.name() {
170 // Invalid repr hint like repr(42). We don't check for unrecognized hints here
171 // (libsyntax does that), so just ignore it.
175 let (article, allowed_targets) = match &*name.as_str() {
178 if target != Target::Struct &&
179 target != Target::Union &&
180 target != Target::Enum {
181 ("a", "struct, enum or union")
187 if target != Target::Struct &&
188 target != Target::Union {
189 ("a", "struct or union")
196 if target != Target::Struct {
203 if target != Target::Struct &&
204 target != Target::Union {
205 ("a", "struct or union")
211 is_transparent = true;
212 if target != Target::Struct {
218 "i8" | "u8" | "i16" | "u16" |
219 "i32" | "u32" | "i64" | "u64" |
220 "isize" | "usize" => {
222 if target != Target::Enum {
230 self.emit_repr_error(
233 &format!("attribute should be applied to {}", allowed_targets),
234 &format!("not {} {}", article, allowed_targets),
238 // Just point at all repr hints if there are any incompatibilities.
239 // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
240 let hint_spans = hints.iter().map(|hint| hint.span);
242 // Error on repr(transparent, <anything else>).
243 if is_transparent && hints.len() > 1 {
244 let hint_spans: Vec<_> = hint_spans.clone().collect();
245 span_err!(self.tcx.sess, hint_spans, E0692,
246 "transparent struct cannot have other repr hints");
248 // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
251 || (int_reprs == 1 && is_c && is_c_like_enum(item)) {
252 let hint_spans: Vec<_> = hint_spans.collect();
253 span_warn!(self.tcx.sess, hint_spans, E0566,
254 "conflicting representation hints");
265 struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
266 .span_label(label_span, label_message)
270 fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
271 // When checking statements ignore expressions, they will be checked later
272 if let hir::Stmt_::StmtDecl(_, _) = stmt.node {
273 for attr in stmt.node.attrs() {
274 if attr.check_name("inline") {
275 self.check_inline(attr, &stmt.span, Target::Statement);
277 if attr.check_name("repr") {
278 self.emit_repr_error(
281 &format!("attribute should not be applied to a statement"),
282 &format!("not a struct, enum or union"),
289 fn check_expr_attributes(&self, expr: &hir::Expr) {
290 let target = match expr.node {
291 hir::ExprClosure(..) => Target::Closure,
292 _ => Target::Expression,
294 for attr in expr.attrs.iter() {
295 if attr.check_name("inline") {
296 self.check_inline(attr, &expr.span, target);
298 if attr.check_name("repr") {
299 self.emit_repr_error(
302 &format!("attribute should not be applied to an expression"),
303 &format!("not defining a struct, enum or union"),
309 fn check_used(&self, item: &hir::Item, target: Target) {
310 for attr in &item.attrs {
311 if attr.name() == "used" && target != Target::Static {
313 .span_err(attr.span, "attribute must be applied to a `static` variable");
319 impl<'a, 'tcx> Visitor<'tcx> for CheckAttrVisitor<'a, 'tcx> {
320 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
321 NestedVisitorMap::OnlyBodies(&self.tcx.hir)
324 fn visit_item(&mut self, item: &'tcx hir::Item) {
325 let target = Target::from_item(item);
326 self.check_attributes(item, target);
327 intravisit::walk_item(self, item)
331 fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
332 self.check_stmt_attributes(stmt);
333 intravisit::walk_stmt(self, stmt)
336 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
337 self.check_expr_attributes(expr);
338 intravisit::walk_expr(self, expr)
342 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
343 let mut checker = CheckAttrVisitor { tcx };
344 tcx.hir.krate().visit_all_item_likes(&mut checker.as_deep_visitor());
347 fn is_c_like_enum(item: &hir::Item) -> bool {
348 if let hir::ItemEnum(ref def, _) = item.node {
349 for variant in &def.variants {
350 match variant.node.data {
351 hir::VariantData::Unit(_) => { /* continue */ }
352 _ => { return false; }