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,
Expression,
Statement,
AssocConst,
- Method { body: bool },
+ Method(MethodKind),
AssocTy,
+ ForeignFn,
+ ForeignStatic,
+ ForeignTy,
}
impl Display for Target {
Target::Expression => "expression",
Target::Statement => "statement",
Target::AssocConst => "associated const",
- Target::Method { .. } => "method",
+ Target::Method(_) => "method",
Target::AssocTy => "associated type",
+ Target::ForeignFn => "foreign function",
+ Target::ForeignStatic => "foreign static item",
+ Target::ForeignTy => "foreign type",
})
}
}
match trait_item.kind {
TraitItemKind::Const(..) => Target::AssocConst,
TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
- Target::Method { body: false }
+ Target::Method(MethodKind::Trait { body: false })
}
TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
- Target::Method { body: true }
+ 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,
}
}
}
} else if attr.check_name(sym::target_feature) {
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
};
/// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
match target {
- Target::Fn | Target::Closure | Target::Method { body: true } => true,
- Target::Method { body: false } | Target::ForeignFn => {
+ 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,
).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 \
+ <https://github.com/rust-lang/rust/issues/65833>")
+ .emit();
+ 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")
+ 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<Attribute>,
+ 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 `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
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")
// 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 && item.map(|item| is_c_like_enum(item)).unwrap_or(false)) {
+ || (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");
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);
intravisit::walk_stmt(self, stmt)