X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc%2Fhir%2Fcheck_attr.rs;h=ea1c585176d57c837e9cb6c310fc1cb3338cf3de;hb=8be9e904549624b3bc97c0cee9ab7780647f365a;hp=c37fec982b1160bd80d505d3f6ad3432ac721545;hpb=f01ffbc7f14c5e80c130d679e99c3507591e4bab;p=rust.git diff --git a/src/librustc/hir/check_attr.rs b/src/librustc/hir/check_attr.rs index c37fec982b1..ea1c585176d 100644 --- a/src/librustc/hir/check_attr.rs +++ b/src/librustc/hir/check_attr.rs @@ -4,9 +4,11 @@ //! conflicts between multiple such attributes attached to the same //! item. -use crate::hir; +use crate::hir::{self, HirId, HirVec, Attribute, Item, ItemKind, TraitItem, TraitItemKind}; +use crate::hir::DUMMY_HIR_ID; use crate::hir::def_id::DefId; use crate::hir::intravisit::{self, Visitor, NestedVisitorMap}; +use crate::lint::builtin::UNUSED_ATTRIBUTES; use crate::ty::TyCtxt; use crate::ty::query::Providers; @@ -14,6 +16,14 @@ use syntax::{attr, symbol::sym}; use syntax_pos::Span; +use rustc_error_codes::*; + +#[derive(Copy, Clone, PartialEq)] +pub(crate) enum MethodKind { + Trait { body: bool }, + Inherent, +} + #[derive(Copy, Clone, PartialEq)] pub(crate) enum Target { ExternCrate, @@ -35,6 +45,12 @@ pub(crate) enum Target { Impl, Expression, Statement, + AssocConst, + Method(MethodKind), + AssocTy, + ForeignFn, + ForeignStatic, + ForeignTy, } impl Display for Target { @@ -59,29 +75,76 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { Target::Impl => "item", Target::Expression => "expression", Target::Statement => "statement", + Target::AssocConst => "associated const", + Target::Method(_) => "method", + Target::AssocTy => "associated type", + Target::ForeignFn => "foreign function", + Target::ForeignStatic => "foreign static item", + Target::ForeignTy => "foreign type", }) } } impl Target { - pub(crate) fn from_item(item: &hir::Item) -> Target { + pub(crate) fn from_item(item: &Item) -> Target { match item.kind { - hir::ItemKind::ExternCrate(..) => Target::ExternCrate, - hir::ItemKind::Use(..) => Target::Use, - hir::ItemKind::Static(..) => Target::Static, - hir::ItemKind::Const(..) => Target::Const, - hir::ItemKind::Fn(..) => Target::Fn, - hir::ItemKind::Mod(..) => Target::Mod, - hir::ItemKind::ForeignMod(..) => Target::ForeignMod, - hir::ItemKind::GlobalAsm(..) => Target::GlobalAsm, - hir::ItemKind::TyAlias(..) => Target::TyAlias, - hir::ItemKind::OpaqueTy(..) => Target::OpaqueTy, - hir::ItemKind::Enum(..) => Target::Enum, - hir::ItemKind::Struct(..) => Target::Struct, - hir::ItemKind::Union(..) => Target::Union, - hir::ItemKind::Trait(..) => Target::Trait, - hir::ItemKind::TraitAlias(..) => Target::TraitAlias, - hir::ItemKind::Impl(..) => Target::Impl, + ItemKind::ExternCrate(..) => Target::ExternCrate, + ItemKind::Use(..) => Target::Use, + ItemKind::Static(..) => Target::Static, + ItemKind::Const(..) => Target::Const, + ItemKind::Fn(..) => Target::Fn, + ItemKind::Mod(..) => Target::Mod, + ItemKind::ForeignMod(..) => Target::ForeignMod, + ItemKind::GlobalAsm(..) => Target::GlobalAsm, + ItemKind::TyAlias(..) => Target::TyAlias, + ItemKind::OpaqueTy(..) => Target::OpaqueTy, + ItemKind::Enum(..) => Target::Enum, + ItemKind::Struct(..) => Target::Struct, + ItemKind::Union(..) => Target::Union, + ItemKind::Trait(..) => Target::Trait, + ItemKind::TraitAlias(..) => Target::TraitAlias, + ItemKind::Impl(..) => Target::Impl, + } + } + + fn from_trait_item(trait_item: &TraitItem) -> Target { + match trait_item.kind { + TraitItemKind::Const(..) => Target::AssocConst, + TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => { + Target::Method(MethodKind::Trait { body: false }) + } + TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => { + Target::Method(MethodKind::Trait { body: true }) + } + TraitItemKind::Type(..) => Target::AssocTy, + } + } + + fn from_foreign_item(foreign_item: &hir::ForeignItem) -> Target { + match foreign_item.kind { + hir::ForeignItemKind::Fn(..) => Target::ForeignFn, + hir::ForeignItemKind::Static(..) => Target::ForeignStatic, + hir::ForeignItemKind::Type => Target::ForeignTy, + } + } + + fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target { + match impl_item.kind { + hir::ImplItemKind::Const(..) => Target::AssocConst, + hir::ImplItemKind::Method(..) => { + let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id); + let containing_item = tcx.hir().expect_item(parent_hir_id); + let containing_impl_is_for_trait = match &containing_item.kind { + hir::ItemKind::Impl(_, _, _, _, tr, _, _) => tr.is_some(), + _ => bug!("parent of an ImplItem must be an Impl"), + }; + if containing_impl_is_for_trait { + Target::Method(MethodKind::Trait { body: true }) + } else { + Target::Method(MethodKind::Inherent) + } + } + hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy, } } } @@ -92,19 +155,26 @@ struct CheckAttrVisitor<'tcx> { impl CheckAttrVisitor<'tcx> { /// Checks any attribute. - fn check_attributes(&self, item: &hir::Item, target: Target) { + fn check_attributes( + &self, + hir_id: HirId, + attrs: &HirVec, + span: &Span, + target: Target, + item: Option<&Item>, + ) { let mut is_valid = true; - for attr in &item.attrs { + for attr in attrs { is_valid &= if attr.check_name(sym::inline) { - self.check_inline(attr, &item.span, target) + self.check_inline(hir_id, attr, span, target) } else if attr.check_name(sym::non_exhaustive) { - self.check_non_exhaustive(attr, item, target) + self.check_non_exhaustive(attr, span, target) } else if attr.check_name(sym::marker) { - self.check_marker(attr, item, target) + self.check_marker(attr, span, target) } else if attr.check_name(sym::target_feature) { - self.check_target_feature(attr, item, target) + self.check_target_feature(attr, span, target) } else if attr.check_name(sym::track_caller) { - self.check_track_caller(attr, &item, target) + self.check_track_caller(&attr.span, attrs, span, target) } else { true }; @@ -115,59 +185,105 @@ fn check_attributes(&self, item: &hir::Item, target: Target) { } if target == Target::Fn { - self.tcx.codegen_fn_attrs(self.tcx.hir().local_def_id(item.hir_id)); + self.tcx.codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id)); } - self.check_repr(item, target); - self.check_used(item, target); + self.check_repr(attrs, span, target, item); + self.check_used(attrs, target); } /// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid. - fn check_inline(&self, attr: &hir::Attribute, span: &Span, target: Target) -> bool { - if target != Target::Fn && target != Target::Closure { - struct_span_err!(self.tcx.sess, - attr.span, - E0518, - "attribute should be applied to function or closure") - .span_label(*span, "not a function or closure") + fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool { + match target { + Target::Fn | Target::Closure | Target::Method(MethodKind::Trait { body: true }) + | Target::Method(MethodKind::Inherent) => true, + Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => { + self.tcx.struct_span_lint_hir( + UNUSED_ATTRIBUTES, + hir_id, + attr.span, + "`#[inline]` is ignored on function prototypes", + ).emit(); + true + } + // FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with + // just a lint, because we previously erroneously allowed it and some crates used it + // accidentally, to to be compatible with crates depending on them, we can't throw an + // error here. + Target::AssocConst => { + self.tcx.struct_span_lint_hir( + UNUSED_ATTRIBUTES, + hir_id, + attr.span, + "`#[inline]` is ignored on constants", + ).warn("this was previously accepted by the compiler but is \ + being phased out; it will become a hard error in \ + a future release!") + .note("for more information, see issue #65833 \ + ") .emit(); - false - } else { - true + true + } + _ => { + struct_span_err!( + self.tcx.sess, + attr.span, + E0518, + "attribute should be applied to function or closure", + ).span_label(*span, "not a function or closure") + .emit(); + false + } } } /// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid. - fn check_track_caller(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) -> bool { - if target != Target::Fn { - struct_span_err!( - self.tcx.sess, - attr.span, - E0739, - "attribute should be applied to function" - ) - .span_label(item.span, "not a function") - .emit(); - false - } else if attr::contains_name(&item.attrs, sym::naked) { - struct_span_err!( - self.tcx.sess, - attr.span, - E0736, - "cannot use `#[track_caller]` with `#[naked]`", - ) - .emit(); - false - } else { - true + fn check_track_caller( + &self, + attr_span: &Span, + attrs: &HirVec, + span: &Span, + target: Target, + ) -> bool { + match target { + Target::Fn if attr::contains_name(attrs, sym::naked) => { + struct_span_err!( + self.tcx.sess, + *attr_span, + E0736, + "cannot use `#[track_caller]` with `#[naked]`", + ).emit(); + false + } + Target::Fn | Target::Method(MethodKind::Inherent) => true, + Target::Method(_) => { + struct_span_err!( + self.tcx.sess, + *attr_span, + E0738, + "`#[track_caller]` may not be used on trait methods", + ).emit(); + false + } + _ => { + struct_span_err!( + self.tcx.sess, + *attr_span, + E0739, + "attribute should be applied to function" + ) + .span_label(*span, "not a function") + .emit(); + false + } } } /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid. fn check_non_exhaustive( &self, - attr: &hir::Attribute, - item: &hir::Item, + attr: &Attribute, + span: &Span, target: Target, ) -> bool { match target { @@ -177,7 +293,7 @@ fn check_non_exhaustive( attr.span, E0701, "attribute can only be applied to a struct or enum") - .span_label(item.span, "not a struct or enum") + .span_label(*span, "not a struct or enum") .emit(); false } @@ -185,13 +301,13 @@ fn check_non_exhaustive( } /// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid. - fn check_marker(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) -> bool { + fn check_marker(&self, attr: &Attribute, span: &Span, target: Target) -> bool { match target { Target::Trait => true, _ => { self.tcx.sess .struct_span_err(attr.span, "attribute can only be applied to a trait") - .span_label(item.span, "not a trait") + .span_label(*span, "not a trait") .emit(); false } @@ -199,18 +315,14 @@ fn check_marker(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) } /// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid. - fn check_target_feature( - &self, - attr: &hir::Attribute, - item: &hir::Item, - target: Target, - ) -> bool { + fn check_target_feature(&self, attr: &Attribute, span: &Span, target: Target) -> bool { match target { - Target::Fn => true, + Target::Fn | Target::Method(MethodKind::Trait { body: true }) + | Target::Method(MethodKind::Inherent) => true, _ => { self.tcx.sess .struct_span_err(attr.span, "attribute should be applied to a function") - .span_label(item.span, "not a function") + .span_label(*span, "not a function") .emit(); false }, @@ -218,13 +330,19 @@ fn check_target_feature( } /// Checks if the `#[repr]` attributes on `item` are valid. - fn check_repr(&self, item: &hir::Item, target: Target) { + fn check_repr( + &self, + attrs: &HirVec, + span: &Span, + target: Target, + item: Option<&Item>, + ) { // Extract the names of all repr hints, e.g., [foo, bar, align] for: // ``` // #[repr(foo)] // #[repr(bar, align(8))] // ``` - let hints: Vec<_> = item.attrs + let hints: Vec<_> = attrs .iter() .filter(|attr| attr.check_name(sym::repr)) .filter_map(|attr| attr.meta_item_list()) @@ -282,7 +400,7 @@ fn check_repr(&self, item: &hir::Item, target: Target) { }; self.emit_repr_error( hint.span(), - item.span, + *span, &format!("attribute should be applied to {}", allowed_targets), &format!("not {} {}", article, allowed_targets), ) @@ -301,7 +419,7 @@ fn check_repr(&self, item: &hir::Item, target: Target) { // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8) if (int_reprs > 1) || (is_simd && is_c) - || (int_reprs == 1 && is_c && is_c_like_enum(item)) { + || (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item))) { let hint_spans: Vec<_> = hint_spans.collect(); span_warn!(self.tcx.sess, hint_spans, E0566, "conflicting representation hints"); @@ -325,7 +443,7 @@ fn check_stmt_attributes(&self, stmt: &hir::Stmt) { if let hir::StmtKind::Local(ref l) = stmt.kind { for attr in l.attrs.iter() { if attr.check_name(sym::inline) { - self.check_inline(attr, &stmt.span, Target::Statement); + self.check_inline(DUMMY_HIR_ID, attr, &stmt.span, Target::Statement); } if attr.check_name(sym::repr) { self.emit_repr_error( @@ -346,7 +464,7 @@ fn check_expr_attributes(&self, expr: &hir::Expr) { }; for attr in expr.attrs.iter() { if attr.check_name(sym::inline) { - self.check_inline(attr, &expr.span, target); + self.check_inline(DUMMY_HIR_ID, attr, &expr.span, target); } if attr.check_name(sym::repr) { self.emit_repr_error( @@ -359,8 +477,8 @@ fn check_expr_attributes(&self, expr: &hir::Expr) { } } - fn check_used(&self, item: &hir::Item, target: Target) { - for attr in &item.attrs { + fn check_used(&self, attrs: &HirVec, target: Target) { + for attr in attrs { if attr.check_name(sym::used) && target != Target::Static { self.tcx.sess .span_err(attr.span, "attribute must be applied to a `static` variable"); @@ -374,12 +492,29 @@ fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { NestedVisitorMap::OnlyBodies(&self.tcx.hir()) } - fn visit_item(&mut self, item: &'tcx hir::Item) { + fn visit_item(&mut self, item: &'tcx Item) { let target = Target::from_item(item); - self.check_attributes(item, target); + self.check_attributes(item.hir_id, &item.attrs, &item.span, target, Some(item)); intravisit::walk_item(self, item) } + fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem) { + let target = Target::from_trait_item(trait_item); + self.check_attributes(trait_item.hir_id, &trait_item.attrs, &trait_item.span, target, None); + intravisit::walk_trait_item(self, trait_item) + } + + fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem) { + let target = Target::from_foreign_item(f_item); + self.check_attributes(f_item.hir_id, &f_item.attrs, &f_item.span, target, None); + intravisit::walk_foreign_item(self, f_item) + } + + fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) { + let target = Target::from_impl_item(self.tcx, impl_item); + self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None); + intravisit::walk_impl_item(self, impl_item) + } fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) { self.check_stmt_attributes(stmt); @@ -392,12 +527,12 @@ fn visit_expr(&mut self, expr: &'tcx hir::Expr) { } } -fn is_c_like_enum(item: &hir::Item) -> bool { - if let hir::ItemKind::Enum(ref def, _) = item.kind { +fn is_c_like_enum(item: &Item) -> bool { + if let ItemKind::Enum(ref def, _) = item.kind { for variant in &def.variants { match variant.data { hir::VariantData::Unit(..) => { /* continue */ } - _ => { return false; } + _ => return false, } } true