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, NestedMetaItem};
12 use rustc_errors::struct_span_err;
14 use rustc_hir::def_id::LocalDefId;
15 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
16 use rustc_hir::{self, HirId, Item, ItemKind, TraitItem};
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 struct CheckAttrVisitor<'tcx> {
50 impl CheckAttrVisitor<'tcx> {
51 /// Checks any attribute.
55 attrs: &'hir [Attribute],
58 item: Option<&Item<'_>>,
60 let mut is_valid = true;
62 is_valid &= if self.tcx.sess.check_name(attr, sym::inline) {
63 self.check_inline(hir_id, attr, span, target)
64 } else if self.tcx.sess.check_name(attr, sym::non_exhaustive) {
65 self.check_non_exhaustive(attr, span, target)
66 } else if self.tcx.sess.check_name(attr, sym::marker) {
67 self.check_marker(attr, span, target)
68 } else if self.tcx.sess.check_name(attr, sym::target_feature) {
69 self.check_target_feature(hir_id, attr, span, target)
70 } else if self.tcx.sess.check_name(attr, sym::track_caller) {
71 self.check_track_caller(&attr.span, attrs, span, target)
72 } else if self.tcx.sess.check_name(attr, sym::doc) {
73 self.check_doc_alias(attr, hir_id, target)
74 } else if self.tcx.sess.check_name(attr, sym::no_link) {
75 self.check_no_link(&attr, span, target)
76 } else if self.tcx.sess.check_name(attr, sym::export_name) {
77 self.check_export_name(&attr, span, target)
80 if self.tcx.sess.check_name(attr, sym::cold) {
81 self.check_cold(hir_id, attr, span, target);
82 } else if self.tcx.sess.check_name(attr, sym::link_name) {
83 self.check_link_name(hir_id, attr, span, target);
84 } else if self.tcx.sess.check_name(attr, sym::link_section) {
85 self.check_link_section(hir_id, attr, span, target);
95 if matches!(target, Target::Fn | Target::Method(_) | Target::ForeignFn) {
96 self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
99 self.check_repr(attrs, span, target, item, hir_id);
100 self.check_used(attrs, target);
103 /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
104 fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
108 | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
109 Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
110 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
111 lint.build("`#[inline]` is ignored on function prototypes").emit()
115 // FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
116 // just a lint, because we previously erroneously allowed it and some crates used it
117 // accidentally, to to be compatible with crates depending on them, we can't throw an
119 Target::AssocConst => {
120 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
121 lint.build("`#[inline]` is ignored on constants")
123 "this was previously accepted by the compiler but is \
124 being phased out; it will become a hard error in \
128 "see issue #65833 <https://github.com/rust-lang/rust/issues/65833> \
129 for more information",
140 "attribute should be applied to function or closure",
142 .span_label(*span, "not a function or closure")
149 /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
150 fn check_track_caller(
153 attrs: &'hir [Attribute],
158 _ if self.tcx.sess.contains_name(attrs, sym::naked) => {
163 "cannot use `#[track_caller]` with `#[naked]`",
168 Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true,
174 "attribute should be applied to function"
176 .span_label(*span, "not a function")
183 /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
184 fn check_non_exhaustive(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
186 Target::Struct | Target::Enum => true,
192 "attribute can only be applied to a struct or enum"
194 .span_label(*span, "not a struct or enum")
201 /// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
202 fn check_marker(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
204 Target::Trait => true,
208 .struct_span_err(attr.span, "attribute can only be applied to a trait")
209 .span_label(*span, "not a trait")
216 /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
217 fn check_target_feature(
226 | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
227 // FIXME: #[target_feature] was previously erroneously allowed on statements and some
228 // crates used this, so only emit a warning.
229 Target::Statement => {
230 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
231 lint.build("attribute should be applied to a function")
233 "this was previously accepted by the compiler but is \
234 being phased out; it will become a hard error in \
237 .span_label(*span, "not a function")
245 .struct_span_err(attr.span, "attribute should be applied to a function")
246 .span_label(*span, "not a function")
253 fn check_doc_alias(&self, attr: &Attribute, hir_id: HirId, target: Target) -> bool {
254 if let Some(mi) = attr.meta() {
255 if let Some(list) = mi.meta_item_list() {
257 if meta.has_name(sym::alias) {
258 if !meta.is_value_str()
261 .map(|s| s.to_string())
262 .unwrap_or_else(String::new)
269 "doc alias attribute expects a string: #[doc(alias = \"0\")]",
274 if let Some(err) = match target {
275 Target::Impl => Some("implementation block"),
276 Target::ForeignMod => Some("extern block"),
278 let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
279 let containing_item = self.tcx.hir().expect_item(parent_hir_id);
280 if Target::from_item(containing_item) == Target::Impl {
281 Some("type alias in implementation block")
286 Target::AssocConst => {
287 let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
288 let containing_item = self.tcx.hir().expect_item(parent_hir_id);
289 // We can't link to trait impl's consts.
290 let err = "associated constant in trait implementation block";
291 match containing_item.kind {
292 ItemKind::Impl { of_trait: Some(_), .. } => Some(err),
302 &format!("`#[doc(alias = \"...\")]` isn't allowed on {}", err),
313 /// Checks if `#[cold]` is applied to a non-function. Returns `true` if valid.
314 fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
316 Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {}
318 // FIXME: #[cold] was previously allowed on non-functions and some crates used
319 // this, so only emit a warning.
320 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
321 lint.build("attribute should be applied to a function")
323 "this was previously accepted by the compiler but is \
324 being phased out; it will become a hard error in \
327 .span_label(*span, "not a function")
334 /// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
335 fn check_link_name(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
337 Target::ForeignFn | Target::ForeignStatic => {}
339 // FIXME: #[cold] was previously allowed on non-functions/statics and some crates
340 // used this, so only emit a warning.
341 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
343 lint.build("attribute should be applied to a foreign function or static");
345 "this was previously accepted by the compiler but is \
346 being phased out; it will become a hard error in \
351 if let Target::ForeignMod = target {
352 if let Some(value) = attr.value_str() {
355 &format!(r#"try `#[link(name = "{}")]` instead"#, value),
358 diag.span_help(attr.span, r#"try `#[link(name = "...")]` instead"#);
362 diag.span_label(*span, "not a foreign function or static");
369 /// Checks if `#[no_link]` is applied to an `extern crate`. Returns `true` if valid.
370 fn check_no_link(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
371 if target == Target::ExternCrate {
376 .struct_span_err(attr.span, "attribute should be applied to an `extern crate` item")
377 .span_label(*span, "not an `extern crate` item")
383 /// Checks if `#[export_name]` is applied to a function or static. Returns `true` if valid.
384 fn check_export_name(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
386 Target::Static | Target::Fn | Target::Method(..) => true,
392 "attribute should be applied to a function or static",
394 .span_label(*span, "not a function or static")
401 /// Checks if `#[link_section]` is applied to a function or static.
402 fn check_link_section(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
404 Target::Static | Target::Fn | Target::Method(..) => {}
406 // FIXME: #[link_section] was previously allowed on non-functions/statics and some
407 // crates used this, so only emit a warning.
408 self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
409 lint.build("attribute should be applied to a function or static")
411 "this was previously accepted by the compiler but is \
412 being phased out; it will become a hard error in \
415 .span_label(*span, "not a function or static")
422 /// Checks if the `#[repr]` attributes on `item` are valid.
425 attrs: &'hir [Attribute],
428 item: Option<&Item<'_>>,
431 // Extract the names of all repr hints, e.g., [foo, bar, align] for:
434 // #[repr(bar, align(8))]
436 let hints: Vec<_> = attrs
438 .filter(|attr| self.tcx.sess.check_name(attr, sym::repr))
439 .filter_map(|attr| attr.meta_item_list())
443 let mut int_reprs = 0;
444 let mut is_c = false;
445 let mut is_simd = false;
446 let mut is_transparent = false;
449 let (article, allowed_targets) = match hint.name_or_empty() {
450 name @ sym::C | name @ sym::align => {
451 is_c |= name == sym::C;
453 Target::Struct | Target::Union | Target::Enum => continue,
454 _ => ("a", "struct, enum, or union"),
458 if target != Target::Struct && target != Target::Union {
459 ("a", "struct or union")
466 if target != Target::Struct {
472 sym::transparent => {
473 is_transparent = true;
475 Target::Struct | Target::Union | Target::Enum => continue,
476 _ => ("a", "struct, enum, or union"),
480 if !self.tcx.features().enabled(sym::no_niche) {
482 &self.tcx.sess.parse_sess,
485 "the attribute `repr(no_niche)` is currently unstable",
490 Target::Struct | Target::Enum => continue,
491 _ => ("a", "struct or enum"),
507 if target != Target::Enum {
515 self.emit_repr_error(
518 &format!("attribute should be applied to {}", allowed_targets),
519 &format!("not {} {}", article, allowed_targets),
523 // Just point at all repr hints if there are any incompatibilities.
524 // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
525 let hint_spans = hints.iter().map(|hint| hint.span());
527 // Error on repr(transparent, <anything else apart from no_niche>).
528 let non_no_niche = |hint: &&NestedMetaItem| hint.name_or_empty() != sym::no_niche;
529 let non_no_niche_count = hints.iter().filter(non_no_niche).count();
530 if is_transparent && non_no_niche_count > 1 {
531 let hint_spans: Vec<_> = hint_spans.clone().collect();
536 "transparent {} cannot have other repr hints",
541 // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
544 || (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item)))
546 self.tcx.struct_span_lint_hir(
547 CONFLICTING_REPR_HINTS,
549 hint_spans.collect::<Vec<Span>>(),
551 lint.build("conflicting representation hints")
552 .code(rustc_errors::error_code!(E0566))
566 struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
567 .span_label(label_span, label_message)
571 fn check_stmt_attributes(&self, stmt: &hir::Stmt<'_>) {
572 // When checking statements ignore expressions, they will be checked later
573 if let hir::StmtKind::Local(ref l) = stmt.kind {
574 self.check_attributes(l.hir_id, &l.attrs, &stmt.span, Target::Statement, None);
575 for attr in l.attrs.iter() {
576 if self.tcx.sess.check_name(attr, sym::repr) {
577 self.emit_repr_error(
580 "attribute should not be applied to a statement",
581 "not a struct, enum, or union",
588 fn check_expr_attributes(&self, expr: &hir::Expr<'_>) {
589 let target = match expr.kind {
590 hir::ExprKind::Closure(..) => Target::Closure,
591 _ => Target::Expression,
593 self.check_attributes(expr.hir_id, &expr.attrs, &expr.span, target, None);
594 for attr in expr.attrs.iter() {
595 if self.tcx.sess.check_name(attr, sym::repr) {
596 self.emit_repr_error(
599 "attribute should not be applied to an expression",
600 "not defining a struct, enum, or union",
604 if target == Target::Closure {
605 self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(expr.hir_id));
609 fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
611 if self.tcx.sess.check_name(attr, sym::used) && target != Target::Static {
614 .span_err(attr.span, "attribute must be applied to a `static` variable");
620 impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
621 type Map = Map<'tcx>;
623 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
624 NestedVisitorMap::OnlyBodies(self.tcx.hir())
627 fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
628 let target = Target::from_item(item);
629 self.check_attributes(item.hir_id, item.attrs, &item.span, target, Some(item));
630 intravisit::walk_item(self, item)
633 fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
634 let target = Target::from_trait_item(trait_item);
635 self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None);
636 intravisit::walk_trait_item(self, trait_item)
639 fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem<'tcx>) {
640 let target = Target::from_foreign_item(f_item);
641 self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None);
642 intravisit::walk_foreign_item(self, f_item)
645 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
646 let target = target_from_impl_item(self.tcx, impl_item);
647 self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
648 intravisit::walk_impl_item(self, impl_item)
651 fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
652 self.check_stmt_attributes(stmt);
653 intravisit::walk_stmt(self, stmt)
656 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
657 self.check_expr_attributes(expr);
658 intravisit::walk_expr(self, expr)
662 fn is_c_like_enum(item: &Item<'_>) -> bool {
663 if let ItemKind::Enum(ref def, _) = item.kind {
664 for variant in def.variants {
666 hir::VariantData::Unit(..) => { /* continue */ }
676 fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
678 .visit_item_likes_in_module(module_def_id, &mut CheckAttrVisitor { tcx }.as_deep_visitor());
681 pub(crate) fn provide(providers: &mut Providers) {
682 *providers = Providers { check_mod_attrs, ..*providers };