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
7 use crate::hir::map::Map;
8 use crate::ty::query::Providers;
11 use rustc_error_codes::*;
12 use rustc_errors::struct_span_err;
14 use rustc_hir::def_id::DefId;
15 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
16 use rustc_hir::DUMMY_HIR_ID;
17 use rustc_hir::{self, HirId, Item, ItemKind, TraitItem, TraitItemKind};
18 use rustc_session::lint::builtin::UNUSED_ATTRIBUTES;
19 use rustc_span::symbol::sym;
21 use syntax::ast::Attribute;
24 use std::fmt::{self, Display};
26 #[derive(Copy, Clone, PartialEq)]
27 pub(crate) enum MethodKind {
32 #[derive(Copy, Clone, PartialEq)]
33 pub(crate) enum Target {
61 impl Display for Target {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 Target::ExternCrate => "extern crate",
69 Target::Static => "static item",
70 Target::Const => "constant item",
71 Target::Fn => "function",
72 Target::Closure => "closure",
73 Target::Mod => "module",
74 Target::ForeignMod => "foreign module",
75 Target::GlobalAsm => "global asm",
76 Target::TyAlias => "type alias",
77 Target::OpaqueTy => "opaque type",
78 Target::Enum => "enum",
79 Target::Struct => "struct",
80 Target::Union => "union",
81 Target::Trait => "trait",
82 Target::TraitAlias => "trait alias",
83 Target::Impl => "item",
84 Target::Expression => "expression",
85 Target::Statement => "statement",
86 Target::AssocConst => "associated const",
87 Target::Method(_) => "method",
88 Target::AssocTy => "associated type",
89 Target::ForeignFn => "foreign function",
90 Target::ForeignStatic => "foreign static item",
91 Target::ForeignTy => "foreign type",
98 pub(crate) fn from_item(item: &Item<'_>) -> Target {
100 ItemKind::ExternCrate(..) => Target::ExternCrate,
101 ItemKind::Use(..) => Target::Use,
102 ItemKind::Static(..) => Target::Static,
103 ItemKind::Const(..) => Target::Const,
104 ItemKind::Fn(..) => Target::Fn,
105 ItemKind::Mod(..) => Target::Mod,
106 ItemKind::ForeignMod(..) => Target::ForeignMod,
107 ItemKind::GlobalAsm(..) => Target::GlobalAsm,
108 ItemKind::TyAlias(..) => Target::TyAlias,
109 ItemKind::OpaqueTy(..) => Target::OpaqueTy,
110 ItemKind::Enum(..) => Target::Enum,
111 ItemKind::Struct(..) => Target::Struct,
112 ItemKind::Union(..) => Target::Union,
113 ItemKind::Trait(..) => Target::Trait,
114 ItemKind::TraitAlias(..) => Target::TraitAlias,
115 ItemKind::Impl(..) => Target::Impl,
119 fn from_trait_item(trait_item: &TraitItem<'_>) -> Target {
120 match trait_item.kind {
121 TraitItemKind::Const(..) => Target::AssocConst,
122 TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
123 Target::Method(MethodKind::Trait { body: false })
125 TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
126 Target::Method(MethodKind::Trait { body: true })
128 TraitItemKind::Type(..) => Target::AssocTy,
132 fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
133 match foreign_item.kind {
134 hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
135 hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
136 hir::ForeignItemKind::Type => Target::ForeignTy,
140 fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target {
141 match impl_item.kind {
142 hir::ImplItemKind::Const(..) => Target::AssocConst,
143 hir::ImplItemKind::Method(..) => {
144 let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
145 let containing_item = tcx.hir().expect_item(parent_hir_id);
146 let containing_impl_is_for_trait = match &containing_item.kind {
147 hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(),
148 _ => bug!("parent of an ImplItem must be an Impl"),
150 if containing_impl_is_for_trait {
151 Target::Method(MethodKind::Trait { body: true })
153 Target::Method(MethodKind::Inherent)
156 hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy,
161 struct CheckAttrVisitor<'tcx> {
165 impl CheckAttrVisitor<'tcx> {
166 /// Checks any attribute.
170 attrs: &'hir [Attribute],
173 item: Option<&Item<'_>>,
175 let mut is_valid = true;
177 is_valid &= if attr.check_name(sym::inline) {
178 self.check_inline(hir_id, attr, span, target)
179 } else if attr.check_name(sym::non_exhaustive) {
180 self.check_non_exhaustive(attr, span, target)
181 } else if attr.check_name(sym::marker) {
182 self.check_marker(attr, span, target)
183 } else if attr.check_name(sym::target_feature) {
184 self.check_target_feature(attr, span, target)
185 } else if attr.check_name(sym::track_caller) {
186 self.check_track_caller(&attr.span, attrs, span, target)
196 if target == Target::Fn {
197 self.tcx.codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
200 self.check_repr(attrs, span, target, item);
201 self.check_used(attrs, target);
204 /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
205 fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
209 | Target::Method(MethodKind::Trait { body: true })
210 | Target::Method(MethodKind::Inherent) => true,
211 Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
213 .struct_span_lint_hir(
217 "`#[inline]` is ignored on function prototypes",
222 // FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
223 // just a lint, because we previously erroneously allowed it and some crates used it
224 // accidentally, to to be compatible with crates depending on them, we can't throw an
226 Target::AssocConst => {
228 .struct_span_lint_hir(
232 "`#[inline]` is ignored on constants",
235 "this was previously accepted by the compiler but is \
236 being phased out; it will become a hard error in \
240 "for more information, see issue #65833 \
241 <https://github.com/rust-lang/rust/issues/65833>",
251 "attribute should be applied to function or closure",
253 .span_label(*span, "not a function or closure")
260 /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
261 fn check_track_caller(
264 attrs: &'hir [Attribute],
269 Target::Fn if attr::contains_name(attrs, sym::naked) => {
274 "cannot use `#[track_caller]` with `#[naked]`",
279 Target::Fn | Target::Method(MethodKind::Inherent) => true,
280 Target::Method(_) => {
285 "`#[track_caller]` may not be used on trait methods",
295 "attribute should be applied to function"
297 .span_label(*span, "not a function")
304 /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
305 fn check_non_exhaustive(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
307 Target::Struct | Target::Enum => true,
313 "attribute can only be applied to a struct or enum"
315 .span_label(*span, "not a struct or enum")
322 /// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
323 fn check_marker(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
325 Target::Trait => true,
329 .struct_span_err(attr.span, "attribute can only be applied to a trait")
330 .span_label(*span, "not a trait")
337 /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
338 fn check_target_feature(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
341 | Target::Method(MethodKind::Trait { body: true })
342 | Target::Method(MethodKind::Inherent) => true,
346 .struct_span_err(attr.span, "attribute should be applied to a function")
347 .span_label(*span, "not a function")
354 /// Checks if the `#[repr]` attributes on `item` are valid.
357 attrs: &'hir [Attribute],
360 item: Option<&Item<'_>>,
362 // Extract the names of all repr hints, e.g., [foo, bar, align] for:
365 // #[repr(bar, align(8))]
367 let hints: Vec<_> = attrs
369 .filter(|attr| attr.check_name(sym::repr))
370 .filter_map(|attr| attr.meta_item_list())
374 let mut int_reprs = 0;
375 let mut is_c = false;
376 let mut is_simd = false;
377 let mut is_transparent = false;
380 let (article, allowed_targets) = match hint.name_or_empty() {
381 name @ sym::C | name @ sym::align => {
382 is_c |= name == sym::C;
384 Target::Struct | Target::Union | Target::Enum => continue,
385 _ => ("a", "struct, enum, or union"),
389 if target != Target::Struct && target != Target::Union {
390 ("a", "struct or union")
397 if target != Target::Struct { ("a", "struct") } else { continue }
399 sym::transparent => {
400 is_transparent = true;
402 Target::Struct | Target::Union | Target::Enum => continue,
403 _ => ("a", "struct, enum, or union"),
417 if target != Target::Enum { ("an", "enum") } else { continue }
421 self.emit_repr_error(
424 &format!("attribute should be applied to {}", allowed_targets),
425 &format!("not {} {}", article, allowed_targets),
429 // Just point at all repr hints if there are any incompatibilities.
430 // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
431 let hint_spans = hints.iter().map(|hint| hint.span());
433 // Error on repr(transparent, <anything else>).
434 if is_transparent && hints.len() > 1 {
435 let hint_spans: Vec<_> = hint_spans.clone().collect();
440 "transparent {} cannot have other repr hints",
445 // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
448 || (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item)))
452 hint_spans.collect::<Vec<Span>>(),
454 "conflicting representation hints",
467 struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
468 .span_label(label_span, label_message)
472 fn check_stmt_attributes(&self, stmt: &hir::Stmt<'_>) {
473 // When checking statements ignore expressions, they will be checked later
474 if let hir::StmtKind::Local(ref l) = stmt.kind {
475 for attr in l.attrs.iter() {
476 if attr.check_name(sym::inline) {
477 self.check_inline(DUMMY_HIR_ID, attr, &stmt.span, Target::Statement);
479 if attr.check_name(sym::repr) {
480 self.emit_repr_error(
483 "attribute should not be applied to a statement",
484 "not a struct, enum, or union",
491 fn check_expr_attributes(&self, expr: &hir::Expr<'_>) {
492 let target = match expr.kind {
493 hir::ExprKind::Closure(..) => Target::Closure,
494 _ => Target::Expression,
496 for attr in expr.attrs.iter() {
497 if attr.check_name(sym::inline) {
498 self.check_inline(DUMMY_HIR_ID, attr, &expr.span, target);
500 if attr.check_name(sym::repr) {
501 self.emit_repr_error(
504 "attribute should not be applied to an expression",
505 "not defining a struct, enum, or union",
511 fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
513 if attr.check_name(sym::used) && target != Target::Static {
516 .span_err(attr.span, "attribute must be applied to a `static` variable");
522 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
523 type Map = Map<'tcx>;
525 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, Self::Map> {
526 NestedVisitorMap::OnlyBodies(&self.tcx.hir())
529 fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
530 let target = Target::from_item(item);
531 self.check_attributes(item.hir_id, item.attrs, &item.span, target, Some(item));
532 intravisit::walk_item(self, item)
535 fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
536 let target = Target::from_trait_item(trait_item);
537 self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
538 intravisit::walk_trait_item(self, trait_item)
541 fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem<'tcx>) {
542 let target = Target::from_foreign_item(f_item);
543 self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None);
544 intravisit::walk_foreign_item(self, f_item)
547 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
548 let target = Target::from_impl_item(self.tcx, impl_item);
549 self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
550 intravisit::walk_impl_item(self, impl_item)
553 fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
554 self.check_stmt_attributes(stmt);
555 intravisit::walk_stmt(self, stmt)
558 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
559 self.check_expr_attributes(expr);
560 intravisit::walk_expr(self, expr)
564 fn is_c_like_enum(item: &Item<'_>) -> bool {
565 if let ItemKind::Enum(ref def, _) = item.kind {
566 for variant in def.variants {
568 hir::VariantData::Unit(..) => { /* continue */ }
578 fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: DefId) {
580 .visit_item_likes_in_module(module_def_id, &mut CheckAttrVisitor { tcx }.as_deep_visitor());
583 pub(crate) fn provide(providers: &mut Providers<'_>) {
584 *providers = Providers { check_mod_attrs, ..*providers };