1 //! This module implements some validity checks for attributes.
2 //! In particular it verifies that `#[inline]` and `#[repr]` attributes are
3 //! attached to items that actually support them and if there are
4 //! conflicts between multiple such attributes attached to the same
8 use crate::hir::def_id::DefId;
9 use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
10 use crate::ty::TyCtxt;
11 use crate::ty::query::Providers;
13 use std::fmt::{self, Display};
14 use syntax::symbol::sym;
17 #[derive(Copy, Clone, PartialEq)]
18 pub(crate) enum Target {
40 impl Display for Target {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 write!(f, "{}", match *self {
43 Target::ExternCrate => "extern crate",
45 Target::Static => "static item",
46 Target::Const => "constant item",
47 Target::Fn => "function",
48 Target::Closure => "closure",
49 Target::Mod => "module",
50 Target::ForeignMod => "foreign module",
51 Target::GlobalAsm => "global asm",
52 Target::TyAlias => "type alias",
53 Target::OpaqueTy => "opaque type",
54 Target::Enum => "enum",
55 Target::Struct => "struct",
56 Target::Union => "union",
57 Target::Trait => "trait",
58 Target::TraitAlias => "trait alias",
59 Target::Impl => "item",
60 Target::Expression => "expression",
61 Target::Statement => "statement",
67 pub(crate) fn from_item(item: &hir::Item) -> Target {
69 hir::ItemKind::ExternCrate(..) => Target::ExternCrate,
70 hir::ItemKind::Use(..) => Target::Use,
71 hir::ItemKind::Static(..) => Target::Static,
72 hir::ItemKind::Const(..) => Target::Const,
73 hir::ItemKind::Fn(..) => Target::Fn,
74 hir::ItemKind::Mod(..) => Target::Mod,
75 hir::ItemKind::ForeignMod(..) => Target::ForeignMod,
76 hir::ItemKind::GlobalAsm(..) => Target::GlobalAsm,
77 hir::ItemKind::TyAlias(..) => Target::TyAlias,
78 hir::ItemKind::OpaqueTy(..) => Target::OpaqueTy,
79 hir::ItemKind::Enum(..) => Target::Enum,
80 hir::ItemKind::Struct(..) => Target::Struct,
81 hir::ItemKind::Union(..) => Target::Union,
82 hir::ItemKind::Trait(..) => Target::Trait,
83 hir::ItemKind::TraitAlias(..) => Target::TraitAlias,
84 hir::ItemKind::Impl(..) => Target::Impl,
89 struct CheckAttrVisitor<'tcx> {
93 impl CheckAttrVisitor<'tcx> {
94 /// Checks any attribute.
95 fn check_attributes(&self, item: &hir::Item, target: Target) {
96 let mut is_valid = true;
97 for attr in &item.attrs {
98 is_valid &= if attr.check_name(sym::inline) {
99 self.check_inline(attr, &item.span, target)
100 } else if attr.check_name(sym::non_exhaustive) {
101 self.check_non_exhaustive(attr, item, target)
102 } else if attr.check_name(sym::marker) {
103 self.check_marker(attr, item, target)
104 } else if attr.check_name(sym::target_feature) {
105 self.check_target_feature(attr, item, target)
115 if target == Target::Fn {
116 self.tcx.codegen_fn_attrs(self.tcx.hir().local_def_id(item.hir_id));
119 self.check_repr(item, target);
120 self.check_used(item, target);
123 /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
124 fn check_inline(&self, attr: &hir::Attribute, span: &Span, target: Target) -> bool {
125 if target != Target::Fn && target != Target::Closure {
126 struct_span_err!(self.tcx.sess,
129 "attribute should be applied to function or closure")
130 .span_label(*span, "not a function or closure")
138 /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
139 fn check_non_exhaustive(
141 attr: &hir::Attribute,
146 Target::Struct | Target::Enum => true,
148 struct_span_err!(self.tcx.sess,
151 "attribute can only be applied to a struct or enum")
152 .span_label(item.span, "not a struct or enum")
159 /// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
160 fn check_marker(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) -> bool {
162 Target::Trait => true,
165 .struct_span_err(attr.span, "attribute can only be applied to a trait")
166 .span_label(item.span, "not a trait")
173 /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
174 fn check_target_feature(
176 attr: &hir::Attribute,
184 .struct_span_err(attr.span, "attribute should be applied to a function")
185 .span_label(item.span, "not a function")
192 /// Checks if the `#[repr]` attributes on `item` are valid.
193 fn check_repr(&self, item: &hir::Item, target: Target) {
194 // Extract the names of all repr hints, e.g., [foo, bar, align] for:
197 // #[repr(bar, align(8))]
199 let hints: Vec<_> = item.attrs
201 .filter(|attr| attr.check_name(sym::repr))
202 .filter_map(|attr| attr.meta_item_list())
206 let mut int_reprs = 0;
207 let mut is_c = false;
208 let mut is_simd = false;
209 let mut is_transparent = false;
212 let (article, allowed_targets) = match hint.name_or_empty() {
213 name @ sym::C | name @ sym::align => {
214 is_c |= name == sym::C;
216 Target::Struct | Target::Union | Target::Enum => continue,
217 _ => ("a", "struct, enum, or union"),
221 if target != Target::Struct &&
222 target != Target::Union {
223 ("a", "struct or union")
230 if target != Target::Struct {
236 sym::transparent => {
237 is_transparent = true;
239 Target::Struct | Target::Union | Target::Enum => continue,
240 _ => ("a", "struct, enum, or union"),
243 sym::i8 | sym::u8 | sym::i16 | sym::u16 |
244 sym::i32 | sym::u32 | sym::i64 | sym::u64 |
245 sym::isize | sym::usize => {
247 if target != Target::Enum {
255 self.emit_repr_error(
258 &format!("attribute should be applied to {}", allowed_targets),
259 &format!("not {} {}", article, allowed_targets),
263 // Just point at all repr hints if there are any incompatibilities.
264 // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
265 let hint_spans = hints.iter().map(|hint| hint.span());
267 // Error on repr(transparent, <anything else>).
268 if is_transparent && hints.len() > 1 {
269 let hint_spans: Vec<_> = hint_spans.clone().collect();
270 span_err!(self.tcx.sess, hint_spans, E0692,
271 "transparent {} cannot have other repr hints", target);
273 // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
276 || (int_reprs == 1 && is_c && is_c_like_enum(item)) {
277 let hint_spans: Vec<_> = hint_spans.collect();
278 span_warn!(self.tcx.sess, hint_spans, E0566,
279 "conflicting representation hints");
290 struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
291 .span_label(label_span, label_message)
295 fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
296 // When checking statements ignore expressions, they will be checked later
297 if let hir::StmtKind::Local(ref l) = stmt.kind {
298 for attr in l.attrs.iter() {
299 if attr.check_name(sym::inline) {
300 self.check_inline(attr, &stmt.span, Target::Statement);
302 if attr.check_name(sym::repr) {
303 self.emit_repr_error(
306 "attribute should not be applied to a statement",
307 "not a struct, enum, or union",
314 fn check_expr_attributes(&self, expr: &hir::Expr) {
315 let target = match expr.kind {
316 hir::ExprKind::Closure(..) => Target::Closure,
317 _ => Target::Expression,
319 for attr in expr.attrs.iter() {
320 if attr.check_name(sym::inline) {
321 self.check_inline(attr, &expr.span, target);
323 if attr.check_name(sym::repr) {
324 self.emit_repr_error(
327 "attribute should not be applied to an expression",
328 "not defining a struct, enum, or union",
334 fn check_used(&self, item: &hir::Item, target: Target) {
335 for attr in &item.attrs {
336 if attr.check_name(sym::used) && target != Target::Static {
338 .span_err(attr.span, "attribute must be applied to a `static` variable");
344 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
345 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
346 NestedVisitorMap::OnlyBodies(&self.tcx.hir())
349 fn visit_item(&mut self, item: &'tcx hir::Item) {
350 let target = Target::from_item(item);
351 self.check_attributes(item, target);
352 intravisit::walk_item(self, item)
356 fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
357 self.check_stmt_attributes(stmt);
358 intravisit::walk_stmt(self, stmt)
361 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
362 self.check_expr_attributes(expr);
363 intravisit::walk_expr(self, expr)
367 fn is_c_like_enum(item: &hir::Item) -> bool {
368 if let hir::ItemKind::Enum(ref def, _) = item.kind {
369 for variant in &def.variants {
371 hir::VariantData::Unit(..) => { /* continue */ }
372 _ => { return false; }
381 fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: DefId) {
382 tcx.hir().visit_item_likes_in_module(
384 &mut CheckAttrVisitor { tcx }.as_deep_visitor()
388 pub(crate) fn provide(providers: &mut Providers<'_>) {
389 *providers = Providers {