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 rustc_middle::hir::map::Map;
8 use rustc_middle::ty::query::Providers;
9 use rustc_middle::ty::TyCtxt;
11 use rustc_ast::{Attribute, LitKind, NestedMetaItem};
12 use rustc_errors::{pluralize, struct_span_err};
14 use rustc_hir::def_id::LocalDefId;
15 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
16 use rustc_hir::{self, FnSig, ForeignItem, ForeignItemKind, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
17 use rustc_hir::{MethodKind, Target};
18 use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES};
19 use rustc_session::parse::feature_err;
20 use rustc_span::symbol::sym;
23 pub(crate) fn target_from_impl_item<'tcx>(
25 impl_item: &hir::ImplItem<'_>,
27 match impl_item.kind {
28 hir::ImplItemKind::Const(..) => Target::AssocConst,
29 hir::ImplItemKind::Fn(..) => {
30 let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id);
31 let containing_item = tcx.hir().expect_item(parent_hir_id);
32 let containing_impl_is_for_trait = match &containing_item.kind {
33 hir::ItemKind::Impl { ref of_trait, .. } => of_trait.is_some(),
34 _ => bug!("parent of an ImplItem must be an Impl"),
36 if containing_impl_is_for_trait {
37 Target::Method(MethodKind::Trait { body: true })
39 Target::Method(MethodKind::Inherent)
42 hir::ImplItemKind::TyAlias(..) => Target::AssocTy,
46 #[derive(Clone, Copy)]
48 Item(&'tcx Item<'tcx>),
49 ForeignItem(&'tcx ForeignItem<'tcx>),
52 struct CheckAttrVisitor<'tcx> {
56 impl CheckAttrVisitor<'tcx> {
57 /// Checks any attribute.
61 attrs: &'hir [Attribute],
64 item: Option<ItemLike<'_>>,
66 let mut is_valid = true;
68 is_valid &= if self.tcx.sess.check_name(attr, sym::inline) {
69 self.check_inline(hir_id, attr, span, target)
70 } else if self.tcx.sess.check_name(attr, sym::non_exhaustive) {
71 self.check_non_exhaustive(attr, span, target)
72 } else if self.tcx.sess.check_name(attr, sym::marker) {
73 self.check_marker(attr, span, target)
74 } else if self.tcx.sess.check_name(attr, sym::target_feature) {
75 self.check_target_feature(hir_id, attr, span, target)
76 } else if self.tcx.sess.check_name(attr, sym::track_caller) {
77 self.check_track_caller(&attr.span, attrs, span, target)
78 } else if self.tcx.sess.check_name(attr, sym::doc) {
79 self.check_doc_alias(attr, hir_id, target)
80 } else if self.tcx.sess.check_name(attr, sym::no_link) {
81 self.check_no_link(&attr, span, target)
82 } else if self.tcx.sess.check_name(attr, sym::export_name) {
83 self.check_export_name(&attr, span, target)
84 } else if self.tcx.sess.check_name(attr, sym::rustc_args_required_const) {
85 self.check_rustc_args_required_const(&attr, span, target, item)
88 if self.tcx.sess.check_name(attr, sym::cold) {
89 self.check_cold(hir_id, attr, span, target);
90 } else if self.tcx.sess.check_name(attr, sym::link_name) {
91 self.check_link_name(hir_id, attr, span, target);
92 } else if self.tcx.sess.check_name(attr, sym::link_section) {
93 self.check_link_section(hir_id, attr, span, target);
94 } else if self.tcx.sess.check_name(attr, sym::no_mangle) {
95 self.check_no_mangle(hir_id, attr, span, target);
105 if matches!(target, Target::Fn | Target::Method(_) | Target::ForeignFn) {
106 self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
109 self.check_repr(attrs, span, target, item, hir_id);
110 self.check_used(attrs, target);
113 /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
114 fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
118 | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
119 Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
120 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
121 lint.build("`#[inline]` is ignored on function prototypes").emit()
125 // FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
126 // just a lint, because we previously erroneously allowed it and some crates used it
127 // accidentally, to to be compatible with crates depending on them, we can't throw an
129 Target::AssocConst => {
130 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
131 lint.build("`#[inline]` is ignored on constants")
133 "this was previously accepted by the compiler but is \
134 being phased out; it will become a hard error in \
138 "see issue #65833 <https://github.com/rust-lang/rust/issues/65833> \
139 for more information",
150 "attribute should be applied to function or closure",
152 .span_label(*span, "not a function or closure")
159 /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
160 fn check_track_caller(
163 attrs: &'hir [Attribute],
168 _ if self.tcx.sess.contains_name(attrs, sym::naked) => {
173 "cannot use `#[track_caller]` with `#[naked]`",
178 Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true,
184 "attribute should be applied to function"
186 .span_label(*span, "not a function")
193 /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
194 fn check_non_exhaustive(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
196 Target::Struct | Target::Enum => true,
202 "attribute can only be applied to a struct or enum"
204 .span_label(*span, "not a struct or enum")
211 /// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
212 fn check_marker(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
214 Target::Trait => true,
218 .struct_span_err(attr.span, "attribute can only be applied to a trait")
219 .span_label(*span, "not a trait")
226 /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
227 fn check_target_feature(
236 | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
237 // FIXME: #[target_feature] was previously erroneously allowed on statements and some
238 // crates used this, so only emit a warning.
239 Target::Statement => {
240 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
241 lint.build("attribute should be applied to a function")
243 "this was previously accepted by the compiler but is \
244 being phased out; it will become a hard error in \
247 .span_label(*span, "not a function")
255 .struct_span_err(attr.span, "attribute should be applied to a function")
256 .span_label(*span, "not a function")
263 fn doc_alias_str_error(&self, meta: &NestedMetaItem) {
268 "doc alias attribute expects a string: #[doc(alias = \"0\")]",
273 fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
274 if let Some(mi) = attr.meta() {
275 if let Some(list) = mi.meta_item_list() {
277 if meta.has_name(sym::alias) {
278 if !meta.is_value_str() {
279 self.doc_alias_str_error(meta);
283 meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new);
284 if doc_alias.is_empty() {
285 self.doc_alias_str_error(meta);
289 doc_alias.chars().find(|&c| c == '"' || c == '\'' || c.is_whitespace())
296 "{:?} character isn't allowed in `#[doc(alias = \"...\")]`",
303 if let Some(err) = match target {
304 Target::Impl => Some("implementation block"),
305 Target::ForeignMod => Some("extern block"),
307 let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
308 let containing_item = self.tcx.hir().expect_item(parent_hir_id);
309 if Target::from_item(containing_item) == Target::Impl {
310 Some("type alias in implementation block")
315 Target::AssocConst => {
316 let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
317 let containing_item = self.tcx.hir().expect_item(parent_hir_id);
318 // We can't link to trait impl's consts.
319 let err = "associated constant in trait implementation block";
320 match containing_item.kind {
321 ItemKind::Impl { of_trait: Some(_), .. } => Some(err),
331 &format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
336 if CRATE_HIR_ID == hir_id {
341 "`#![doc(alias = \"...\")]` isn't allowed as a crate \
354 /// Checks if `#[cold]` is applied to a non-function. Returns `true` if valid.
355 fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
357 Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {}
359 // FIXME: #[cold] was previously allowed on non-functions and some crates used
360 // this, so only emit a warning.
361 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
362 lint.build("attribute should be applied to a function")
364 "this was previously accepted by the compiler but is \
365 being phased out; it will become a hard error in \
368 .span_label(*span, "not a function")
375 /// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
376 fn check_link_name(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
378 Target::ForeignFn | Target::ForeignStatic => {}
380 // FIXME: #[cold] was previously allowed on non-functions/statics and some crates
381 // used this, so only emit a warning.
382 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
384 lint.build("attribute should be applied to a foreign function or static");
386 "this was previously accepted by the compiler but is \
387 being phased out; it will become a hard error in \
392 if let Target::ForeignMod = target {
393 if let Some(value) = attr.value_str() {
396 &format!(r#"try `#[link(name = "{}")]` instead"#, value),
399 diag.span_help(attr.span, r#"try `#[link(name = "...")]` instead"#);
403 diag.span_label(*span, "not a foreign function or static");
410 /// Checks if `#[no_link]` is applied to an `extern crate`. Returns `true` if valid.
411 fn check_no_link(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
412 if target == Target::ExternCrate {
417 .struct_span_err(attr.span, "attribute should be applied to an `extern crate` item")
418 .span_label(*span, "not an `extern crate` item")
424 /// Checks if `#[export_name]` is applied to a function or static. Returns `true` if valid.
425 fn check_export_name(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
427 Target::Static | Target::Fn | Target::Method(..) => true,
433 "attribute should be applied to a function or static",
435 .span_label(*span, "not a function or static")
442 /// Checks if `#[rustc_args_required_const]` is applied to a function and has a valid argument.
443 fn check_rustc_args_required_const(
448 item: Option<ItemLike<'_>>,
450 if let Target::Fn | Target::Method(..) | Target::ForeignFn = target {
451 let mut invalid_args = vec![];
452 for meta in attr.meta_item_list().expect("no meta item list") {
453 if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) {
454 if let Some(ItemLike::Item(Item {
455 kind: ItemKind::Fn(FnSig { decl, .. }, ..),
458 | Some(ItemLike::ForeignItem(ForeignItem {
459 kind: ForeignItemKind::Fn(decl, ..),
463 let arg_count = decl.inputs.len() as u128;
464 if *val >= arg_count {
465 let span = meta.span();
468 .struct_span_err(span, "index exceeds number of arguments")
472 "there {} only {} argument{}",
473 if arg_count != 1 { "are" } else { "is" },
475 pluralize!(arg_count)
482 bug!("should be a function item");
485 invalid_args.push(meta.span());
488 if !invalid_args.is_empty() {
491 .struct_span_err(invalid_args, "arguments should be non-negative integers")
500 .struct_span_err(attr.span, "attribute should be applied to a function")
501 .span_label(*span, "not a function")
507 /// Checks if `#[link_section]` is applied to a function or static.
508 fn check_link_section(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
510 Target::Static | Target::Fn | Target::Method(..) => {}
512 // FIXME: #[link_section] was previously allowed on non-functions/statics and some
513 // crates used this, so only emit a warning.
514 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
515 lint.build("attribute should be applied to a function or static")
517 "this was previously accepted by the compiler but is \
518 being phased out; it will become a hard error in \
521 .span_label(*span, "not a function or static")
528 /// Checks if `#[no_mangle]` is applied to a function or static.
529 fn check_no_mangle(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
531 Target::Static | Target::Fn | Target::Method(..) => {}
533 // FIXME: #[no_mangle] was previously allowed on non-functions/statics and some
534 // crates used this, so only emit a warning.
535 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
536 lint.build("attribute should be applied to a function or static")
538 "this was previously accepted by the compiler but is \
539 being phased out; it will become a hard error in \
542 .span_label(*span, "not a function or static")
549 /// Checks if the `#[repr]` attributes on `item` are valid.
552 attrs: &'hir [Attribute],
555 item: Option<ItemLike<'_>>,
558 // Extract the names of all repr hints, e.g., [foo, bar, align] for:
561 // #[repr(bar, align(8))]
563 let hints: Vec<_> = attrs
565 .filter(|attr| self.tcx.sess.check_name(attr, sym::repr))
566 .filter_map(|attr| attr.meta_item_list())
570 let mut int_reprs = 0;
571 let mut is_c = false;
572 let mut is_simd = false;
573 let mut is_transparent = false;
576 let (article, allowed_targets) = match hint.name_or_empty() {
577 name @ sym::C | name @ sym::align => {
578 is_c |= name == sym::C;
580 Target::Struct | Target::Union | Target::Enum => continue,
581 _ => ("a", "struct, enum, or union"),
585 if target != Target::Struct && target != Target::Union {
586 ("a", "struct or union")
593 if target != Target::Struct {
599 sym::transparent => {
600 is_transparent = true;
602 Target::Struct | Target::Union | Target::Enum => continue,
603 _ => ("a", "struct, enum, or union"),
607 if !self.tcx.features().enabled(sym::no_niche) {
609 &self.tcx.sess.parse_sess,
612 "the attribute `repr(no_niche)` is currently unstable",
617 Target::Struct | Target::Enum => continue,
618 _ => ("a", "struct or enum"),
634 if target != Target::Enum {
642 self.emit_repr_error(
645 &format!("attribute should be applied to {}", allowed_targets),
646 &format!("not {} {}", article, allowed_targets),
650 // Just point at all repr hints if there are any incompatibilities.
651 // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
652 let hint_spans = hints.iter().map(|hint| hint.span());
654 // Error on repr(transparent, <anything else apart from no_niche>).
655 let non_no_niche = |hint: &&NestedMetaItem| hint.name_or_empty() != sym::no_niche;
656 let non_no_niche_count = hints.iter().filter(non_no_niche).count();
657 if is_transparent && non_no_niche_count > 1 {
658 let hint_spans: Vec<_> = hint_spans.clone().collect();
663 "transparent {} cannot have other repr hints",
668 // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
673 && item.map_or(false, |item| {
674 if let ItemLike::Item(item) = item {
675 return is_c_like_enum(item);
680 self.tcx.struct_span_lint_hir(
681 CONFLICTING_REPR_HINTS,
683 hint_spans.collect::<Vec<Span>>(),
685 lint.build("conflicting representation hints")
686 .code(rustc_errors::error_code!(E0566))
700 struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
701 .span_label(label_span, label_message)
705 fn check_stmt_attributes(&self, stmt: &hir::Stmt<'_>) {
706 // When checking statements ignore expressions, they will be checked later
707 if let hir::StmtKind::Local(ref l) = stmt.kind {
708 self.check_attributes(l.hir_id, &l.attrs, &stmt.span, Target::Statement, None);
709 for attr in l.attrs.iter() {
710 if self.tcx.sess.check_name(attr, sym::repr) {
711 self.emit_repr_error(
714 "attribute should not be applied to a statement",
715 "not a struct, enum, or union",
722 fn check_expr_attributes(&self, expr: &hir::Expr<'_>) {
723 let target = match expr.kind {
724 hir::ExprKind::Closure(..) => Target::Closure,
725 _ => Target::Expression,
727 self.check_attributes(expr.hir_id, &expr.attrs, &expr.span, target, None);
728 for attr in expr.attrs.iter() {
729 if self.tcx.sess.check_name(attr, sym::repr) {
730 self.emit_repr_error(
733 "attribute should not be applied to an expression",
734 "not defining a struct, enum, or union",
738 if target == Target::Closure {
739 self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(expr.hir_id));
743 fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
745 if self.tcx.sess.check_name(attr, sym::used) && target != Target::Static {
748 .span_err(attr.span, "attribute must be applied to a `static` variable");
754 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
755 type Map = Map<'tcx>;
757 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
758 NestedVisitorMap::OnlyBodies(self.tcx.hir())
761 fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
762 let target = Target::from_item(item);
763 self.check_attributes(
768 Some(ItemLike::Item(item)),
770 intravisit::walk_item(self, item)
773 fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
774 let target = Target::from_trait_item(trait_item);
775 self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
776 intravisit::walk_trait_item(self, trait_item)
779 fn visit_foreign_item(&mut self, f_item: &'tcx ForeignItem<'tcx>) {
780 let target = Target::from_foreign_item(f_item);
781 self.check_attributes(
786 Some(ItemLike::ForeignItem(f_item)),
788 intravisit::walk_foreign_item(self, f_item)
791 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
792 let target = target_from_impl_item(self.tcx, impl_item);
793 self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
794 intravisit::walk_impl_item(self, impl_item)
797 fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
798 self.check_stmt_attributes(stmt);
799 intravisit::walk_stmt(self, stmt)
802 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
803 self.check_expr_attributes(expr);
804 intravisit::walk_expr(self, expr)
808 fn is_c_like_enum(item: &Item<'_>) -> bool {
809 if let ItemKind::Enum(ref def, _) = item.kind {
810 for variant in def.variants {
812 hir::VariantData::Unit(..) => { /* continue */ }
822 fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
824 .visit_item_likes_in_module(module_def_id, &mut CheckAttrVisitor { tcx }.as_deep_visitor());
825 if module_def_id.is_top_level_module() {
826 for attr in tcx.hir().krate_attrs() {
827 CheckAttrVisitor { tcx }.check_doc_alias(attr, CRATE_HIR_ID, Target::Mod);
832 pub(crate) fn provide(providers: &mut Providers) {
833 *providers = Providers { check_mod_attrs, ..*providers };