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,
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",
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_impl_item(impl_item: &hir::ImplItem) -> Target {
+ fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
match impl_item.kind {
- hir::ImplItemKind::Const(..) => Target::Const,
- hir::ImplItemKind::Method(..) => Target::Method { body: true },
- hir::ImplItemKind::TyAlias(..) => Target::TyAlias,
- hir::ImplItemKind::OpaqueTy(..) => Target::OpaqueTy,
+ 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,
}
}
}
/// 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
}
).emit();
false
}
- Target::Fn => true,
- Target::Method { .. } => {
+ Target::Fn | Target::Method(MethodKind::Inherent) => true,
+ Target::Method(_) => {
struct_span_err!(
self.tcx.sess,
*attr_span,
/// 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")
}
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
- let target = Target::from_impl_item(impl_item);
+ 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)
}