2017-06-03 16:14:29 +00:00
|
|
|
//! This module implements some validity checks for attributes.
|
|
|
|
//! In particular it verifies that `#[inline]` and `#[repr]` attributes are
|
|
|
|
//! attached to items that actually support them and if there are
|
|
|
|
//! conflicts between multiple such attributes attached to the same
|
|
|
|
//! item.
|
|
|
|
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2019-12-31 20:25:16 +00:00
|
|
|
|
2021-07-29 17:00:41 +00:00
|
|
|
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, NestedMetaItem};
|
|
|
|
use rustc_data_structures::stable_set::FxHashSet;
|
2021-03-09 03:35:53 +00:00
|
|
|
use rustc_errors::{pluralize, struct_span_err, Applicability};
|
2021-07-29 17:00:41 +00:00
|
|
|
use rustc_feature::{AttributeType, BUILTIN_ATTRIBUTE_MAP};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2020-06-27 11:09:54 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-01-07 17:12:06 +00:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2021-05-09 12:21:33 +00:00
|
|
|
use rustc_hir::{self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID};
|
2020-01-30 00:24:51 +00:00
|
|
|
use rustc_hir::{MethodKind, Target};
|
2021-03-02 20:10:05 +00:00
|
|
|
use rustc_session::lint::builtin::{
|
2021-03-05 13:44:31 +00:00
|
|
|
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES,
|
2021-03-02 20:10:05 +00:00
|
|
|
};
|
2020-01-22 23:54:04 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2020-10-03 18:45:39 +00:00
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
2021-05-08 14:57:10 +00:00
|
|
|
use rustc_span::{MultiSpan, Span, DUMMY_SP};
|
2015-09-25 06:25:59 +00:00
|
|
|
|
2020-05-25 03:07:55 +00:00
|
|
|
pub(crate) fn target_from_impl_item<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
impl_item: &hir::ImplItem<'_>,
|
|
|
|
) -> Target {
|
2020-01-27 18:37:36 +00:00
|
|
|
match impl_item.kind {
|
|
|
|
hir::ImplItemKind::Const(..) => Target::AssocConst,
|
2020-03-05 15:57:34 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => {
|
2021-01-30 22:25:03 +00:00
|
|
|
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id());
|
2020-01-27 18:37:36 +00:00
|
|
|
let containing_item = tcx.hir().expect_item(parent_hir_id);
|
|
|
|
let containing_impl_is_for_trait = match &containing_item.kind {
|
2020-11-22 22:46:21 +00:00
|
|
|
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
|
2020-01-27 18:37:36 +00:00
|
|
|
_ => 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)
|
2019-10-24 21:21:30 +00:00
|
|
|
}
|
2019-10-13 15:14:59 +00:00
|
|
|
}
|
2020-05-10 11:15:51 +00:00
|
|
|
hir::ImplItemKind::TyAlias(..) => Target::AssocTy,
|
2019-10-13 15:14:59 +00:00
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:44:32 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum ItemLike<'tcx> {
|
|
|
|
Item(&'tcx Item<'tcx>),
|
|
|
|
ForeignItem(&'tcx ForeignItem<'tcx>),
|
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct CheckAttrVisitor<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl CheckAttrVisitor<'tcx> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks any attribute.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_attributes(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
2020-09-29 16:44:32 +00:00
|
|
|
item: Option<ItemLike<'_>>,
|
2019-10-11 01:33:16 +00:00
|
|
|
) {
|
2019-09-26 10:00:53 +00:00
|
|
|
let mut is_valid = true;
|
2021-05-08 14:57:10 +00:00
|
|
|
let mut specified_inline = None;
|
2021-07-29 17:00:41 +00:00
|
|
|
let mut seen = FxHashSet::default();
|
2020-11-25 22:27:23 +00:00
|
|
|
let attrs = self.tcx.hir().attrs(hir_id);
|
2019-10-11 01:33:16 +00:00
|
|
|
for attr in attrs {
|
2021-07-29 17:00:41 +00:00
|
|
|
let attr_is_valid = match attr.name_or_empty() {
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::inline => self.check_inline(hir_id, attr, span, target),
|
|
|
|
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
|
|
|
|
sym::marker => self.check_marker(hir_id, attr, span, target),
|
|
|
|
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
|
|
|
|
sym::track_caller => {
|
|
|
|
self.check_track_caller(hir_id, &attr.span, attrs, span, target)
|
2020-07-11 14:29:35 +00:00
|
|
|
}
|
2021-05-08 14:57:10 +00:00
|
|
|
sym::doc => self.check_doc_attrs(attr, hir_id, target, &mut specified_inline),
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::no_link => self.check_no_link(hir_id, &attr, span, target),
|
|
|
|
sym::export_name => self.check_export_name(hir_id, &attr, span, target),
|
|
|
|
sym::rustc_layout_scalar_valid_range_start
|
|
|
|
| sym::rustc_layout_scalar_valid_range_end => {
|
|
|
|
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
|
|
|
|
}
|
|
|
|
sym::allow_internal_unstable => {
|
|
|
|
self.check_allow_internal_unstable(hir_id, &attr, span, target, &attrs)
|
|
|
|
}
|
|
|
|
sym::rustc_allow_const_fn_unstable => {
|
|
|
|
self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target)
|
|
|
|
}
|
|
|
|
sym::naked => self.check_naked(hir_id, attr, span, target),
|
|
|
|
sym::rustc_legacy_const_generics => {
|
|
|
|
self.check_rustc_legacy_const_generics(&attr, span, target, item)
|
|
|
|
}
|
|
|
|
sym::rustc_clean
|
|
|
|
| sym::rustc_dirty
|
|
|
|
| sym::rustc_if_this_changed
|
|
|
|
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
|
2021-06-25 13:36:38 +00:00
|
|
|
sym::cmse_nonsecure_entry => self.check_cmse_nonsecure_entry(attr, span, target),
|
2021-07-10 03:13:52 +00:00
|
|
|
sym::default_method_body_is_const => {
|
|
|
|
self.check_default_method_body_is_const(attr, span, target)
|
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
sym::rustc_const_unstable
|
|
|
|
| sym::rustc_const_stable
|
|
|
|
| sym::unstable
|
|
|
|
| sym::stable
|
|
|
|
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
|
2021-04-11 00:00:00 +00:00
|
|
|
_ => true,
|
2019-09-26 10:00:53 +00:00
|
|
|
};
|
2021-07-29 17:00:41 +00:00
|
|
|
is_valid &= attr_is_valid;
|
|
|
|
|
2021-04-11 00:00:00 +00:00
|
|
|
// lint-only checks
|
|
|
|
match attr.name_or_empty() {
|
|
|
|
sym::cold => self.check_cold(hir_id, attr, span, target),
|
|
|
|
sym::link_name => self.check_link_name(hir_id, attr, span, target),
|
|
|
|
sym::link_section => self.check_link_section(hir_id, attr, span, target),
|
|
|
|
sym::no_mangle => self.check_no_mangle(hir_id, attr, span, target),
|
2021-07-29 17:00:41 +00:00
|
|
|
sym::deprecated | sym::rustc_deprecated => {
|
|
|
|
self.check_deprecated(hir_id, attr, span, target)
|
|
|
|
}
|
|
|
|
sym::macro_use | sym::macro_escape => self.check_macro_use(hir_id, attr, target),
|
|
|
|
sym::path => self.check_generic_attr(hir_id, attr, target, &[Target::Mod]),
|
|
|
|
sym::cfg_attr => self.check_cfg_attr(hir_id, attr),
|
|
|
|
sym::plugin_registrar => self.check_plugin_registrar(hir_id, attr, target),
|
|
|
|
sym::macro_export => self.check_macro_export(hir_id, attr, target),
|
|
|
|
sym::ignore | sym::should_panic | sym::proc_macro_derive => {
|
|
|
|
self.check_generic_attr(hir_id, attr, target, &[Target::Fn])
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hir_id != CRATE_HIR_ID {
|
|
|
|
if let Some((_, AttributeType::CrateLevel, ..)) =
|
|
|
|
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name))
|
|
|
|
{
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
let msg = match attr.style {
|
|
|
|
ast::AttrStyle::Outer => {
|
|
|
|
"crate-level attribute should be an inner attribute: add an exclamation \
|
|
|
|
mark: `#![foo]`"
|
|
|
|
}
|
|
|
|
ast::AttrStyle::Inner => "crate-level attribute should be in the root module",
|
|
|
|
};
|
|
|
|
lint.build(msg).emit()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Duplicate attributes
|
|
|
|
match attr.name_or_empty() {
|
|
|
|
name @ sym::macro_use => {
|
|
|
|
let args = attr.meta_item_list().unwrap_or_else(Vec::new);
|
|
|
|
let args: Vec<_> = args.iter().map(|arg| arg.name_or_empty()).collect();
|
|
|
|
if !seen.insert((name, args)) {
|
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
|lint| lint.build("unused attribute").emit(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-04-11 00:00:00 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
2019-09-26 10:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !is_valid {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-10 18:54:17 +00:00
|
|
|
if matches!(target, Target::Closure | Target::Fn | Target::Method(_) | Target::ForeignFn) {
|
2020-05-01 09:32:20 +00:00
|
|
|
self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
|
2017-06-03 16:14:29 +00:00
|
|
|
}
|
2018-01-08 21:43:42 +00:00
|
|
|
|
2020-01-27 23:27:57 +00:00
|
|
|
self.check_repr(attrs, span, target, item, hir_id);
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_used(attrs, target);
|
2017-06-03 16:14:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 14:35:53 +00:00
|
|
|
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build(&format!(
|
|
|
|
"`#[{}]` is ignored on struct fields, match arms and macro defs",
|
|
|
|
sym,
|
|
|
|
))
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
|
|
|
)
|
|
|
|
.note(
|
|
|
|
"see issue #80564 <https://github.com/rust-lang/rust/issues/80564> \
|
|
|
|
for more information",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inline_attr_str_error_without_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build(&format!("`#[{}]` is ignored on struct fields and match arms", sym))
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
|
|
|
)
|
|
|
|
.note(
|
|
|
|
"see issue #80564 <https://github.com/rust-lang/rust/issues/80564> \
|
|
|
|
for more information",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
|
|
|
|
match target {
|
2019-12-22 22:42:04 +00:00
|
|
|
Target::Fn
|
|
|
|
| Target::Closure
|
2020-04-17 00:38:52 +00:00
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
|
2020-02-01 23:47:58 +00:00
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("`#[inline]` is ignored on function prototypes").emit()
|
|
|
|
});
|
2019-10-11 01:35:22 +00:00
|
|
|
true
|
|
|
|
}
|
2019-10-25 23:46:07 +00:00
|
|
|
// 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 => {
|
2020-02-03 09:57:45 +00:00
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("`#[inline]` is ignored on constants")
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
2020-08-12 16:43:14 +00:00
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
2020-02-03 09:57:45 +00:00
|
|
|
)
|
|
|
|
.note(
|
|
|
|
"see issue #65833 <https://github.com/rust-lang/rust/issues/65833> \
|
2020-08-12 16:43:14 +00:00
|
|
|
for more information",
|
2020-02-03 09:57:45 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
});
|
2019-10-25 23:46:07 +00:00
|
|
|
true
|
|
|
|
}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): Same for fields, arms, and macro defs
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "inline");
|
|
|
|
true
|
|
|
|
}
|
2019-10-11 01:33:16 +00:00
|
|
|
_ => {
|
2019-10-25 23:46:07 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
attr.span,
|
|
|
|
E0518,
|
|
|
|
"attribute should be applied to function or closure",
|
2019-12-22 22:42:04 +00:00
|
|
|
)
|
|
|
|
.span_label(*span, "not a function or closure")
|
|
|
|
.emit();
|
2019-10-11 01:33:16 +00:00
|
|
|
false
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:00:41 +00:00
|
|
|
fn check_generic_attr(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
|
|
|
target: Target,
|
|
|
|
allowed_targets: &[Target],
|
|
|
|
) {
|
|
|
|
if !allowed_targets.iter().any(|t| t == &target) {
|
|
|
|
let name = attr.name_or_empty();
|
|
|
|
let mut i = allowed_targets.iter();
|
|
|
|
// Pluralize
|
|
|
|
let b = i.next().map_or_else(String::new, |t| t.to_string() + "s");
|
|
|
|
let supported_names = i.enumerate().fold(b, |mut b, (i, allowed_target)| {
|
|
|
|
if allowed_targets.len() > 2 && i == allowed_targets.len() - 2 {
|
|
|
|
b.push_str(", and ");
|
|
|
|
} else if allowed_targets.len() == 2 && i == allowed_targets.len() - 2 {
|
|
|
|
b.push_str(" and ");
|
|
|
|
} else {
|
|
|
|
b.push_str(", ");
|
|
|
|
}
|
|
|
|
// Pluralize
|
|
|
|
b.push_str(&(allowed_target.to_string() + "s"));
|
|
|
|
b
|
|
|
|
});
|
|
|
|
//let supported_names = allowed_targets.iter().fold(String::new(), |msg, t| msg + ", " + &t.to_string());
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build(&format!("`#[{name}]` only has an effect on {}", supported_names))
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:00:00 +00:00
|
|
|
/// Checks if `#[naked]` is applied to a function definition.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_naked(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
|
2020-11-24 00:00:00 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn
|
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[allow_internal_unstable]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "naked");
|
|
|
|
true
|
|
|
|
}
|
2020-11-24 00:00:00 +00:00
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"attribute should be applied to a function definition",
|
|
|
|
)
|
|
|
|
.span_label(*span, "not a function definition")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:36:38 +00:00
|
|
|
/// Checks if `#[cmse_nonsecure_entry]` is applied to a function definition.
|
|
|
|
fn check_cmse_nonsecure_entry(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Fn
|
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"attribute should be applied to a function definition",
|
|
|
|
)
|
|
|
|
.span_label(*span, "not a function definition")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 01:24:10 +00:00
|
|
|
/// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
|
2019-10-11 01:42:23 +00:00
|
|
|
fn check_track_caller(
|
|
|
|
&self,
|
2021-02-01 14:35:53 +00:00
|
|
|
hir_id: HirId,
|
2019-10-11 01:42:23 +00:00
|
|
|
attr_span: &Span,
|
2019-11-28 22:50:47 +00:00
|
|
|
attrs: &'hir [Attribute],
|
2019-10-11 01:42:23 +00:00
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2019-10-14 14:07:16 +00:00
|
|
|
match target {
|
2020-11-24 00:00:00 +00:00
|
|
|
_ if attrs.iter().any(|attr| attr.has_name(sym::naked)) => {
|
2019-10-14 14:07:16 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
*attr_span,
|
|
|
|
E0736,
|
|
|
|
"cannot use `#[track_caller]` with `#[naked]`",
|
2019-12-22 22:42:04 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2019-10-14 14:07:16 +00:00
|
|
|
false
|
|
|
|
}
|
2020-08-12 16:43:14 +00:00
|
|
|
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[track_caller]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
for attr in attrs {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "track_caller");
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-10-14 14:07:16 +00:00
|
|
|
_ => {
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
*attr_span,
|
|
|
|
E0739,
|
|
|
|
"attribute should be applied to function"
|
|
|
|
)
|
|
|
|
.span_label(*span, "not a function")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
2019-07-20 00:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_non_exhaustive(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2018-03-24 18:07:18 +00:00
|
|
|
match target {
|
2020-09-10 18:54:17 +00:00
|
|
|
Target::Struct | Target::Enum | Target::Variant => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[non_exhaustive]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "non_exhaustive");
|
|
|
|
true
|
|
|
|
}
|
2018-03-24 18:07:18 +00:00
|
|
|
_ => {
|
2019-12-22 22:42:04 +00:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
attr.span,
|
|
|
|
E0701,
|
|
|
|
"attribute can only be applied to a struct or enum"
|
|
|
|
)
|
|
|
|
.span_label(*span, "not a struct or enum")
|
|
|
|
.emit();
|
2019-09-26 10:00:53 +00:00
|
|
|
false
|
2018-03-24 18:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_marker(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
|
2018-08-25 07:54:41 +00:00
|
|
|
match target {
|
2019-09-26 10:00:53 +00:00
|
|
|
Target::Trait => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[marker]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "marker");
|
|
|
|
true
|
|
|
|
}
|
2018-08-25 07:54:41 +00:00
|
|
|
_ => {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
2018-09-03 09:17:20 +00:00
|
|
|
.struct_span_err(attr.span, "attribute can only be applied to a trait")
|
2019-10-11 01:33:16 +00:00
|
|
|
.span_label(*span, "not a trait")
|
2018-08-25 07:54:41 +00:00
|
|
|
.emit();
|
2019-09-26 10:00:53 +00:00
|
|
|
false
|
2018-08-25 07:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
|
2020-07-11 14:29:35 +00:00
|
|
|
fn check_target_feature(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2019-09-26 10:00:53 +00:00
|
|
|
match target {
|
2019-12-22 22:42:04 +00:00
|
|
|
Target::Fn
|
2020-04-17 00:38:52 +00:00
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
2020-07-11 14:29:35 +00:00
|
|
|
// FIXME: #[target_feature] was previously erroneously allowed on statements and some
|
|
|
|
// crates used this, so only emit a warning.
|
|
|
|
Target::Statement => {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("attribute should be applied to a function")
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
2020-08-12 16:43:14 +00:00
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
2020-07-11 14:29:35 +00:00
|
|
|
)
|
|
|
|
.span_label(*span, "not a function")
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
true
|
|
|
|
}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[target_feature]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "target_feature");
|
|
|
|
true
|
|
|
|
}
|
2019-09-26 10:00:53 +00:00
|
|
|
_ => {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
2019-09-26 10:00:53 +00:00
|
|
|
.struct_span_err(attr.span, "attribute should be applied to a function")
|
2019-10-11 01:33:16 +00:00
|
|
|
.span_label(*span, "not a function")
|
2019-09-26 10:00:53 +00:00
|
|
|
.emit();
|
|
|
|
false
|
2019-12-22 22:42:04 +00:00
|
|
|
}
|
2019-09-26 10:00:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:19:53 +00:00
|
|
|
fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
|
2020-09-16 14:41:45 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta.span(),
|
2020-12-01 13:19:53 +00:00
|
|
|
&format!("doc {0} attribute expects a string: #[doc({0} = \"a\")]", attr_name),
|
2020-09-16 14:41:45 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2021-03-06 20:59:01 +00:00
|
|
|
fn check_doc_alias_value(
|
|
|
|
&self,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
doc_alias: &str,
|
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
is_list: bool,
|
|
|
|
) -> bool {
|
2021-03-08 16:30:22 +00:00
|
|
|
let tcx = self.tcx;
|
|
|
|
let err_fn = move |span: Span, msg: &str| {
|
|
|
|
tcx.sess.span_err(
|
|
|
|
span,
|
|
|
|
&format!(
|
|
|
|
"`#[doc(alias{})]` {}",
|
|
|
|
if is_list { "(\"...\")" } else { " = \"...\"" },
|
|
|
|
msg,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
false
|
|
|
|
};
|
2020-12-01 13:11:33 +00:00
|
|
|
if doc_alias.is_empty() {
|
2021-03-08 16:30:22 +00:00
|
|
|
return err_fn(
|
|
|
|
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
|
|
|
|
"attribute cannot have empty value",
|
|
|
|
);
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
|
|
|
if let Some(c) =
|
|
|
|
doc_alias.chars().find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
|
|
|
|
{
|
2021-03-08 16:30:22 +00:00
|
|
|
self.tcx.sess.span_err(
|
|
|
|
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
|
|
|
|
&format!(
|
|
|
|
"{:?} character isn't allowed in `#[doc(alias{})]`",
|
|
|
|
c,
|
|
|
|
if is_list { "(\"...\")" } else { " = \"...\"" },
|
|
|
|
),
|
|
|
|
);
|
2020-12-01 13:11:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if doc_alias.starts_with(' ') || doc_alias.ends_with(' ') {
|
2021-03-08 16:30:22 +00:00
|
|
|
return err_fn(
|
|
|
|
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
|
|
|
|
"cannot start or end with ' '",
|
|
|
|
);
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
|
|
|
if let Some(err) = match target {
|
|
|
|
Target::Impl => Some("implementation block"),
|
|
|
|
Target::ForeignMod => Some("extern block"),
|
|
|
|
Target::AssocTy => {
|
|
|
|
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
|
|
|
|
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
|
|
|
|
if Target::from_item(containing_item) == Target::Impl {
|
|
|
|
Some("type alias in implementation block")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Target::AssocConst => {
|
|
|
|
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id);
|
|
|
|
let containing_item = self.tcx.hir().expect_item(parent_hir_id);
|
|
|
|
// We can't link to trait impl's consts.
|
|
|
|
let err = "associated constant in trait implementation block";
|
|
|
|
match containing_item.kind {
|
2020-11-22 22:46:21 +00:00
|
|
|
ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => Some(err),
|
2020-12-01 13:11:33 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-06-15 17:01:11 +00:00
|
|
|
// we check the validity of params elsewhere
|
|
|
|
Target::Param => return false,
|
2020-12-01 13:11:33 +00:00
|
|
|
_ => None,
|
|
|
|
} {
|
2021-03-08 16:30:22 +00:00
|
|
|
return err_fn(meta.span(), &format!("isn't allowed on {}", err));
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
2021-01-04 14:05:36 +00:00
|
|
|
let item_name = self.tcx.hir().name(hir_id);
|
2021-01-06 09:59:50 +00:00
|
|
|
if &*item_name.as_str() == doc_alias {
|
2021-03-08 16:30:22 +00:00
|
|
|
return err_fn(meta.span(), "is the same as the item's name");
|
2021-01-04 14:05:36 +00:00
|
|
|
}
|
2020-12-01 13:11:33 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-03-06 20:59:01 +00:00
|
|
|
fn check_doc_alias(&self, meta: &NestedMetaItem, hir_id: HirId, target: Target) -> bool {
|
|
|
|
if let Some(values) = meta.meta_item_list() {
|
|
|
|
let mut errors = 0;
|
|
|
|
for v in values {
|
|
|
|
match v.literal() {
|
|
|
|
Some(l) => match l.kind {
|
|
|
|
LitKind::Str(s, _) => {
|
|
|
|
if !self.check_doc_alias_value(v, &s.as_str(), hir_id, target, true) {
|
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
v.span(),
|
2021-03-08 16:30:22 +00:00
|
|
|
"`#[doc(alias(\"a\"))]` expects string literals",
|
2021-03-06 20:59:01 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
v.span(),
|
2021-03-08 16:30:22 +00:00
|
|
|
"`#[doc(alias(\"a\"))]` expects string literals",
|
2021-03-06 20:59:01 +00:00
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errors == 0
|
|
|
|
} else if let Some(doc_alias) = meta.value_str().map(|s| s.to_string()) {
|
|
|
|
self.check_doc_alias_value(meta, &doc_alias, hir_id, target, false)
|
|
|
|
} else {
|
2021-01-04 14:05:36 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta.span(),
|
2021-03-06 20:59:01 +00:00
|
|
|
"doc alias attribute expects a string `#[doc(alias = \"a\")]` or a list of \
|
2021-03-08 16:30:22 +00:00
|
|
|
strings `#[doc(alias(\"a\", \"b\"))]`",
|
2021-01-04 14:05:36 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2021-03-06 20:59:01 +00:00
|
|
|
false
|
2021-01-04 14:05:36 +00:00
|
|
|
}
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:19:53 +00:00
|
|
|
fn check_doc_keyword(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
|
|
|
|
let doc_keyword = meta.value_str().map(|s| s.to_string()).unwrap_or_else(String::new);
|
|
|
|
if doc_keyword.is_empty() {
|
|
|
|
self.doc_attr_str_error(meta, "keyword");
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-17 14:45:26 +00:00
|
|
|
match self.tcx.hir().find(hir_id).and_then(|node| match node {
|
|
|
|
hir::Node::Item(item) => Some(&item.kind),
|
|
|
|
_ => None,
|
|
|
|
}) {
|
|
|
|
Some(ItemKind::Mod(ref module)) => {
|
2020-12-01 13:19:53 +00:00
|
|
|
if !module.item_ids.is_empty() {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta.span(),
|
|
|
|
"`#[doc(keyword = \"...\")]` can only be used on empty modules",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta.span(),
|
|
|
|
"`#[doc(keyword = \"...\")]` can only be used on modules",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !rustc_lexer::is_ident(&doc_keyword) {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
|
|
|
|
&format!("`{}` is not a valid identifier", doc_keyword),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes. Returns `true` if valid.
|
|
|
|
///
|
|
|
|
/// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
|
|
|
|
/// if there are conflicting attributes for one item.
|
|
|
|
///
|
|
|
|
/// `specified_inline` is used to keep track of whether we have
|
|
|
|
/// already seen an inlining attribute for this item.
|
|
|
|
/// If so, `specified_inline` holds the value and the span of
|
|
|
|
/// the first `inline`/`no_inline` attribute.
|
2021-05-08 14:57:10 +00:00
|
|
|
fn check_doc_inline(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
specified_inline: &mut Option<(bool, Span)>,
|
|
|
|
) -> bool {
|
2021-06-06 01:16:19 +00:00
|
|
|
if target == Target::Use || target == Target::ExternCrate {
|
2021-05-08 14:57:10 +00:00
|
|
|
let do_inline = meta.name_or_empty() == sym::inline;
|
|
|
|
if let Some((prev_inline, prev_span)) = *specified_inline {
|
|
|
|
if do_inline != prev_inline {
|
|
|
|
let mut spans = MultiSpan::from_spans(vec![prev_span, meta.span()]);
|
|
|
|
spans.push_span_label(prev_span, String::from("this attribute..."));
|
|
|
|
spans.push_span_label(
|
|
|
|
meta.span(),
|
|
|
|
String::from("...conflicts with this attribute"),
|
|
|
|
);
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(spans, "conflicting doc inlining attributes")
|
|
|
|
.help("remove one of the conflicting attributes")
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
*specified_inline = Some((do_inline, meta.span()));
|
|
|
|
true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
meta.span(),
|
|
|
|
|lint| {
|
|
|
|
let mut err = lint.build(
|
|
|
|
"this attribute can only be applied to a `use` item",
|
|
|
|
);
|
|
|
|
err.span_label(meta.span(), "only applicable on `use` items");
|
|
|
|
if attr.style == AttrStyle::Outer {
|
|
|
|
err.span_label(
|
|
|
|
self.tcx.hir().span(hir_id),
|
|
|
|
"not a `use` item",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.note("read https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#docno_inlinedocinline for more information")
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Checks that an attribute is *not* used at the crate level. Returns `true` if valid.
|
2021-05-08 14:25:00 +00:00
|
|
|
fn check_attr_not_crate_level(
|
2020-12-01 13:11:33 +00:00
|
|
|
&self,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr_name: &str,
|
|
|
|
) -> bool {
|
|
|
|
if CRATE_HIR_ID == hir_id {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta.span(),
|
|
|
|
&format!(
|
2021-03-13 22:32:06 +00:00
|
|
|
"`#![doc({} = \"...\")]` isn't allowed as a crate-level attribute",
|
2020-12-01 13:11:33 +00:00
|
|
|
attr_name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-01 13:19:53 +00:00
|
|
|
true
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Checks that an attribute is used at the crate level. Returns `true` if valid.
|
2021-05-08 14:25:00 +00:00
|
|
|
fn check_attr_crate_level(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
) -> bool {
|
|
|
|
if hir_id != CRATE_HIR_ID {
|
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
meta.span(),
|
|
|
|
|lint| {
|
|
|
|
let mut err = lint.build(
|
|
|
|
"this attribute can only be applied at the crate level",
|
|
|
|
);
|
|
|
|
if attr.style == AttrStyle::Outer && self.tcx.hir().get_parent_item(hir_id) == CRATE_HIR_ID {
|
|
|
|
if let Ok(mut src) =
|
|
|
|
self.tcx.sess.source_map().span_to_snippet(attr.span)
|
|
|
|
{
|
|
|
|
src.insert(1, '!');
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
attr.span,
|
|
|
|
"to apply to the crate, use an inner attribute",
|
|
|
|
src,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_help(
|
|
|
|
attr.span,
|
|
|
|
"to apply to the crate, use an inner attribute",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err.note("read https://doc.rust-lang.org/nightly/rustdoc/the-doc-attribute.html#at-the-crate-level for more information")
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:04:27 +00:00
|
|
|
/// Checks that `doc(test(...))` attribute contains only valid attributes. Returns `true` if
|
|
|
|
/// valid.
|
|
|
|
fn check_test_attr(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
|
|
|
|
let mut is_valid = true;
|
|
|
|
if let Some(metas) = meta.meta_item_list() {
|
|
|
|
for i_meta in metas {
|
|
|
|
match i_meta.name_or_empty() {
|
|
|
|
sym::attr | sym::no_crate_inject => {}
|
|
|
|
_ => {
|
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span(),
|
|
|
|
|lint| {
|
|
|
|
lint.build(&format!(
|
|
|
|
"unknown `doc(test)` attribute `{}`",
|
|
|
|
rustc_ast_pretty::pprust::path_to_string(
|
|
|
|
&i_meta.meta_item().unwrap().path
|
|
|
|
),
|
|
|
|
))
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.tcx.struct_span_lint_hir(INVALID_DOC_ATTRIBUTES, hir_id, meta.span(), |lint| {
|
|
|
|
lint.build("`#[doc(test(...)]` takes a list of attributes").emit();
|
|
|
|
});
|
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
is_valid
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Runs various checks on `#[doc]` attributes. Returns `true` if valid.
|
|
|
|
///
|
|
|
|
/// `specified_inline` should be initialized to `None` and kept for the scope
|
|
|
|
/// of one item. Read the documentation of [`check_doc_inline`] for more information.
|
|
|
|
///
|
|
|
|
/// [`check_doc_inline`]: Self::check_doc_inline
|
2021-05-08 14:57:10 +00:00
|
|
|
fn check_doc_attrs(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
specified_inline: &mut Option<(bool, Span)>,
|
|
|
|
) -> bool {
|
2021-03-11 23:41:51 +00:00
|
|
|
let mut is_valid = true;
|
|
|
|
|
2021-03-13 21:13:27 +00:00
|
|
|
if let Some(list) = attr.meta().and_then(|mi| mi.meta_item_list().map(|l| l.to_vec())) {
|
2021-05-19 01:46:41 +00:00
|
|
|
for meta in &list {
|
2021-03-13 21:13:27 +00:00
|
|
|
if let Some(i_meta) = meta.meta_item() {
|
|
|
|
match i_meta.name_or_empty() {
|
|
|
|
sym::alias
|
2021-05-08 14:25:00 +00:00
|
|
|
if !self.check_attr_not_crate_level(&meta, hir_id, "alias")
|
2021-03-13 21:13:27 +00:00
|
|
|
|| !self.check_doc_alias(&meta, hir_id, target) =>
|
2020-07-08 12:48:31 +00:00
|
|
|
{
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false
|
2020-09-04 15:12:53 +00:00
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
|
|
|
sym::keyword
|
2021-05-08 14:25:00 +00:00
|
|
|
if !self.check_attr_not_crate_level(&meta, hir_id, "keyword")
|
2021-03-13 21:13:27 +00:00
|
|
|
|| !self.check_doc_keyword(&meta, hir_id) =>
|
2020-12-01 13:19:53 +00:00
|
|
|
{
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false
|
2020-12-01 13:19:53 +00:00
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
2021-05-08 14:25:00 +00:00
|
|
|
sym::html_favicon_url
|
|
|
|
| sym::html_logo_url
|
|
|
|
| sym::html_playground_url
|
|
|
|
| sym::issue_tracker_base_url
|
|
|
|
| sym::html_root_url
|
|
|
|
| sym::html_no_source
|
|
|
|
| sym::test
|
|
|
|
if !self.check_attr_crate_level(&attr, &meta, hir_id) =>
|
|
|
|
{
|
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:57:10 +00:00
|
|
|
sym::inline | sym::no_inline
|
|
|
|
if !self.check_doc_inline(
|
|
|
|
&attr,
|
|
|
|
&meta,
|
2021-03-02 20:10:05 +00:00
|
|
|
hir_id,
|
2021-05-08 14:57:10 +00:00
|
|
|
target,
|
|
|
|
specified_inline,
|
|
|
|
) =>
|
|
|
|
{
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false;
|
2021-03-02 20:10:05 +00:00
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
|
|
|
// no_default_passes: deprecated
|
|
|
|
// passes: deprecated
|
|
|
|
// plugins: removed, but rustdoc warns about it itself
|
|
|
|
sym::alias
|
|
|
|
| sym::cfg
|
|
|
|
| sym::hidden
|
|
|
|
| sym::html_favicon_url
|
|
|
|
| sym::html_logo_url
|
|
|
|
| sym::html_no_source
|
|
|
|
| sym::html_playground_url
|
|
|
|
| sym::html_root_url
|
2021-05-08 14:57:10 +00:00
|
|
|
| sym::inline
|
2021-03-13 21:13:27 +00:00
|
|
|
| sym::issue_tracker_base_url
|
|
|
|
| sym::keyword
|
|
|
|
| sym::masked
|
|
|
|
| sym::no_default_passes
|
2021-05-08 14:57:10 +00:00
|
|
|
| sym::no_inline
|
2021-03-09 03:35:53 +00:00
|
|
|
| sym::notable_trait
|
2021-03-13 21:13:27 +00:00
|
|
|
| sym::passes
|
2021-08-03 12:04:27 +00:00
|
|
|
| sym::plugins => {}
|
|
|
|
|
|
|
|
sym::test => {
|
|
|
|
if !self.check_test_attr(&meta, hir_id) {
|
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
2021-07-11 02:06:10 +00:00
|
|
|
sym::primitive => {
|
|
|
|
if !self.tcx.features().doc_primitive {
|
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span,
|
|
|
|
|lint| {
|
|
|
|
let mut diag = lint.build(
|
|
|
|
"`doc(primitive)` should never have been stable",
|
|
|
|
);
|
|
|
|
diag.emit();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 21:13:27 +00:00
|
|
|
_ => {
|
2021-03-02 14:42:32 +00:00
|
|
|
self.tcx.struct_span_lint_hir(
|
2021-03-05 13:44:31 +00:00
|
|
|
INVALID_DOC_ATTRIBUTES,
|
2021-03-02 14:42:32 +00:00
|
|
|
hir_id,
|
|
|
|
i_meta.span,
|
|
|
|
|lint| {
|
2021-03-09 03:35:53 +00:00
|
|
|
let mut diag = lint.build(&format!(
|
2021-03-01 16:12:39 +00:00
|
|
|
"unknown `doc` attribute `{}`",
|
2021-03-14 21:39:13 +00:00
|
|
|
rustc_ast_pretty::pprust::path_to_string(&i_meta.path),
|
2021-03-09 03:35:53 +00:00
|
|
|
));
|
|
|
|
if i_meta.has_name(sym::spotlight) {
|
|
|
|
diag.note(
|
|
|
|
"`doc(spotlight)` was renamed to `doc(notable_trait)`",
|
|
|
|
);
|
|
|
|
diag.span_suggestion_short(
|
|
|
|
i_meta.span,
|
|
|
|
"use `notable_trait` instead",
|
|
|
|
String::from("notable_trait"),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
diag.note("`doc(spotlight)` is now a no-op");
|
|
|
|
}
|
2021-05-19 01:46:41 +00:00
|
|
|
if i_meta.has_name(sym::include) {
|
|
|
|
if let Some(value) = i_meta.value_str() {
|
|
|
|
// if there are multiple attributes, the suggestion would suggest deleting all of them, which is incorrect
|
|
|
|
let applicability = if list.len() == 1 {
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
} else {
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
};
|
|
|
|
let inner = if attr.style == AttrStyle::Inner {
|
|
|
|
"!"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
|
|
|
diag.span_suggestion(
|
|
|
|
attr.meta().unwrap().span,
|
|
|
|
"use `doc = include_str!` instead",
|
|
|
|
format!(
|
|
|
|
"#{}[doc = include_str!(\"{}\")]",
|
|
|
|
inner, value
|
|
|
|
),
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 03:35:53 +00:00
|
|
|
diag.emit();
|
2021-03-02 14:42:32 +00:00
|
|
|
},
|
|
|
|
);
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false;
|
2021-03-01 16:12:39 +00:00
|
|
|
}
|
2020-07-08 12:48:31 +00:00
|
|
|
}
|
2021-03-13 21:25:27 +00:00
|
|
|
} else {
|
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
meta.span(),
|
|
|
|
|lint| {
|
2021-07-21 20:43:19 +00:00
|
|
|
lint.build(&"invalid `doc` attribute").emit();
|
2021-03-13 21:25:27 +00:00
|
|
|
},
|
|
|
|
);
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false;
|
2020-07-08 12:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 23:41:51 +00:00
|
|
|
|
|
|
|
is_valid
|
2020-07-08 12:48:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 04:47:42 +00:00
|
|
|
/// Checks if `#[cold]` is applied to a non-function. Returns `true` if valid.
|
2020-07-11 14:29:35 +00:00
|
|
|
fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
|
2020-06-14 04:47:42 +00:00
|
|
|
match target {
|
2020-08-12 16:43:14 +00:00
|
|
|
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[cold]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "cold");
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
_ => {
|
2020-07-11 14:29:35 +00:00
|
|
|
// FIXME: #[cold] was previously allowed on non-functions and some crates used
|
|
|
|
// this, so only emit a warning.
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("attribute should be applied to a function")
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
2020-08-12 16:43:14 +00:00
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
2020-07-11 14:29:35 +00:00
|
|
|
)
|
|
|
|
.span_label(*span, "not a function")
|
|
|
|
.emit();
|
|
|
|
});
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 14:29:35 +00:00
|
|
|
/// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
|
|
|
|
fn check_link_name(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
|
|
|
|
match target {
|
|
|
|
Target::ForeignFn | Target::ForeignStatic => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[link_name]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "link_name");
|
|
|
|
}
|
2020-07-11 14:29:35 +00:00
|
|
|
_ => {
|
|
|
|
// FIXME: #[cold] was previously allowed on non-functions/statics and some crates
|
|
|
|
// used this, so only emit a warning.
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
let mut diag =
|
|
|
|
lint.build("attribute should be applied to a foreign function or static");
|
|
|
|
diag.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
2020-08-12 16:43:14 +00:00
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
2020-06-14 04:47:42 +00:00
|
|
|
);
|
|
|
|
|
2020-07-11 14:29:35 +00:00
|
|
|
// See issue #47725
|
|
|
|
if let Target::ForeignMod = target {
|
|
|
|
if let Some(value) = attr.value_str() {
|
|
|
|
diag.span_help(
|
|
|
|
attr.span,
|
|
|
|
&format!(r#"try `#[link(name = "{}")]` instead"#, value),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
diag.span_help(attr.span, r#"try `#[link(name = "...")]` instead"#);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diag.span_label(*span, "not a foreign function or static");
|
|
|
|
diag.emit();
|
|
|
|
});
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if `#[no_link]` is applied to an `extern crate`. Returns `true` if valid.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_no_link(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::ExternCrate => true,
|
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[no_link]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "no_link");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"attribute should be applied to an `extern crate` item",
|
|
|
|
)
|
|
|
|
.span_label(*span, "not an `extern crate` item")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 18:41:57 +00:00
|
|
|
fn is_impl_item(&self, hir_id: HirId) -> bool {
|
|
|
|
matches!(self.tcx.hir().get(hir_id), hir::Node::ImplItem(..))
|
|
|
|
}
|
|
|
|
|
2020-06-14 04:47:42 +00:00
|
|
|
/// Checks if `#[export_name]` is applied to a function or static. Returns `true` if valid.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_export_name(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2020-06-14 04:47:42 +00:00
|
|
|
match target {
|
2021-08-10 18:41:57 +00:00
|
|
|
Target::Static | Target::Fn => true,
|
|
|
|
Target::Method(..) if self.is_impl_item(hir_id) => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[export_name]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "export_name");
|
|
|
|
true
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
2021-08-10 18:41:57 +00:00
|
|
|
"attribute should be applied to a free function, impl method or static",
|
2020-06-14 04:47:42 +00:00
|
|
|
)
|
2021-08-10 18:41:57 +00:00
|
|
|
.span_label(*span, "not a free function, impl method or static")
|
2020-06-14 04:47:42 +00:00
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 00:00:00 +00:00
|
|
|
fn check_rustc_layout_scalar_valid_range(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
if target != Target::Struct {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "attribute should be applied to a struct")
|
|
|
|
.span_label(*span, "not a struct")
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = match attr.meta_item_list() {
|
|
|
|
None => return false,
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
if matches!(&list[..], &[NestedMetaItem::Literal(Lit { kind: LitKind::Int(..), .. })]) {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "expected exactly one integer literal argument")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:12:28 +00:00
|
|
|
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
|
|
|
|
fn check_rustc_legacy_const_generics(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
item: Option<ItemLike<'_>>,
|
|
|
|
) -> bool {
|
2021-02-25 09:04:43 +00:00
|
|
|
let is_function = matches!(target, Target::Fn | Target::Method(..));
|
2021-02-23 15:12:28 +00:00
|
|
|
if !is_function {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "attribute should be applied to a function")
|
|
|
|
.span_label(*span, "not a function")
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let list = match attr.meta_item_list() {
|
|
|
|
// The attribute form is validated on AST.
|
|
|
|
None => return false,
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
2021-02-25 09:04:43 +00:00
|
|
|
let (decl, generics) = match item {
|
|
|
|
Some(ItemLike::Item(Item {
|
|
|
|
kind: ItemKind::Fn(FnSig { decl, .. }, generics, _),
|
|
|
|
..
|
|
|
|
})) => (decl, generics),
|
|
|
|
_ => bug!("should be a function item"),
|
|
|
|
};
|
|
|
|
|
|
|
|
for param in generics.params {
|
|
|
|
match param.kind {
|
|
|
|
hir::GenericParamKind::Const { .. } => {}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"#[rustc_legacy_const_generics] functions must \
|
|
|
|
only have const generics",
|
|
|
|
)
|
|
|
|
.span_label(param.span, "non-const generic parameter")
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if list.len() != generics.params.len() {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"#[rustc_legacy_const_generics] must have one index for each generic parameter",
|
|
|
|
)
|
|
|
|
.span_label(generics.span, "generic parameters")
|
|
|
|
.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg_count = decl.inputs.len() as u128 + generics.params.len() as u128;
|
2021-02-23 15:12:28 +00:00
|
|
|
let mut invalid_args = vec![];
|
|
|
|
for meta in list {
|
|
|
|
if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) {
|
2021-02-25 09:04:43 +00:00
|
|
|
if *val >= arg_count {
|
|
|
|
let span = meta.span();
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(span, "index exceeds number of arguments")
|
|
|
|
.span_label(
|
|
|
|
span,
|
|
|
|
format!(
|
|
|
|
"there {} only {} argument{}",
|
|
|
|
if arg_count != 1 { "are" } else { "is" },
|
|
|
|
arg_count,
|
|
|
|
pluralize!(arg_count)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
return false;
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-06 00:00:00 +00:00
|
|
|
invalid_args.push(meta.span());
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
2020-12-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !invalid_args.is_empty() {
|
2020-09-29 16:44:32 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
2020-12-06 00:00:00 +00:00
|
|
|
.struct_span_err(invalid_args, "arguments should be non-negative integers")
|
2020-09-29 16:44:32 +00:00
|
|
|
.emit();
|
|
|
|
false
|
2020-12-06 00:00:00 +00:00
|
|
|
} else {
|
|
|
|
true
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 11:59:53 +00:00
|
|
|
/// Checks that the dep-graph debugging attributes are only present when the query-dep-graph
|
|
|
|
/// option is passed to the compiler.
|
2021-03-22 18:21:55 +00:00
|
|
|
fn check_rustc_dirty_clean(&self, attr: &Attribute) -> bool {
|
|
|
|
if self.tcx.sess.opts.debugging_opts.query_dep_graph {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "attribute requires -Z query-dep-graph to be enabled")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 14:29:35 +00:00
|
|
|
/// Checks if `#[link_section]` is applied to a function or static.
|
|
|
|
fn check_link_section(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
|
2020-06-14 04:47:42 +00:00
|
|
|
match target {
|
2020-07-11 14:29:35 +00:00
|
|
|
Target::Static | Target::Fn | Target::Method(..) => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[link_section]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "link_section");
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
_ => {
|
2020-07-11 14:29:35 +00:00
|
|
|
// FIXME: #[link_section] was previously allowed on non-functions/statics and some
|
|
|
|
// crates used this, so only emit a warning.
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("attribute should be applied to a function or static")
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
2020-08-12 16:43:14 +00:00
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
2020-07-11 14:29:35 +00:00
|
|
|
)
|
|
|
|
.span_label(*span, "not a function or static")
|
|
|
|
.emit();
|
|
|
|
});
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 02:12:24 +00:00
|
|
|
/// Checks if `#[no_mangle]` is applied to a function or static.
|
|
|
|
fn check_no_mangle(&self, hir_id: HirId, attr: &Attribute, span: &Span, target: Target) {
|
|
|
|
match target {
|
2021-08-10 18:41:57 +00:00
|
|
|
Target::Static | Target::Fn => {}
|
|
|
|
Target::Method(..) if self.is_impl_item(hir_id) => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[no_mangle]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "no_mangle");
|
|
|
|
}
|
2020-09-06 02:12:24 +00:00
|
|
|
_ => {
|
|
|
|
// FIXME: #[no_mangle] was previously allowed on non-functions/statics and some
|
|
|
|
// crates used this, so only emit a warning.
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
2021-08-10 18:41:57 +00:00
|
|
|
lint.build(
|
|
|
|
"attribute should be applied to a free function, impl method or static",
|
|
|
|
)
|
|
|
|
.warn(
|
|
|
|
"this was previously accepted by the compiler but is \
|
|
|
|
being phased out; it will become a hard error in \
|
|
|
|
a future release!",
|
|
|
|
)
|
|
|
|
.span_label(*span, "not a free function, impl method or static")
|
|
|
|
.emit();
|
2020-09-06 02:12:24 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks if the `#[repr]` attributes on `item` are valid.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_repr(
|
|
|
|
&self,
|
2019-11-28 22:50:47 +00:00
|
|
|
attrs: &'hir [Attribute],
|
2019-10-11 01:33:16 +00:00
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
2020-09-29 16:44:32 +00:00
|
|
|
item: Option<ItemLike<'_>>,
|
2020-01-27 23:27:57 +00:00
|
|
|
hir_id: HirId,
|
2019-10-11 01:33:16 +00:00
|
|
|
) {
|
2018-01-01 20:42:12 +00:00
|
|
|
// Extract the names of all repr hints, e.g., [foo, bar, align] for:
|
|
|
|
// ```
|
|
|
|
// #[repr(foo)]
|
|
|
|
// #[repr(bar, align(8))]
|
|
|
|
// ```
|
2019-10-11 01:33:16 +00:00
|
|
|
let hints: Vec<_> = attrs
|
2018-01-01 20:42:12 +00:00
|
|
|
.iter()
|
2021-04-11 00:00:00 +00:00
|
|
|
.filter(|attr| attr.has_name(sym::repr))
|
2018-01-01 20:42:12 +00:00
|
|
|
.filter_map(|attr| attr.meta_item_list())
|
2018-09-12 10:31:11 +00:00
|
|
|
.flatten()
|
2018-01-01 20:42:12 +00:00
|
|
|
.collect();
|
2016-08-20 01:58:14 +00:00
|
|
|
|
2017-11-20 17:26:54 +00:00
|
|
|
let mut int_reprs = 0;
|
|
|
|
let mut is_c = false;
|
|
|
|
let mut is_simd = false;
|
2018-01-03 16:43:30 +00:00
|
|
|
let mut is_transparent = false;
|
2017-01-14 22:49:29 +00:00
|
|
|
|
2018-01-01 20:42:12 +00:00
|
|
|
for hint in &hints {
|
2021-01-21 02:49:04 +00:00
|
|
|
if !hint.is_meta_item() {
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
hint.span(),
|
|
|
|
E0565,
|
|
|
|
"meta item in `repr` must be an identifier"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-08 04:33:06 +00:00
|
|
|
let (article, allowed_targets) = match hint.name_or_empty() {
|
2021-01-21 02:49:04 +00:00
|
|
|
sym::C => {
|
|
|
|
is_c = true;
|
2019-05-22 14:31:09 +00:00
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Union | Target::Enum => continue,
|
|
|
|
_ => ("a", "struct, enum, or union"),
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 02:49:04 +00:00
|
|
|
sym::align => {
|
|
|
|
if let (Target::Fn, true) = (target, !self.tcx.features().fn_align) {
|
|
|
|
feature_err(
|
|
|
|
&self.tcx.sess.parse_sess,
|
|
|
|
sym::fn_align,
|
|
|
|
hint.span(),
|
|
|
|
"`repr(align)` attributes on functions are unstable",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Union | Target::Enum | Target::Fn => continue,
|
|
|
|
_ => ("a", "struct, enum, function, or union"),
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::packed => {
|
2019-12-22 22:42:04 +00:00
|
|
|
if target != Target::Struct && target != Target::Union {
|
|
|
|
("a", "struct or union")
|
2016-07-03 03:24:27 +00:00
|
|
|
} else {
|
2019-12-22 22:42:04 +00:00
|
|
|
continue;
|
2016-07-03 03:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::simd => {
|
2017-11-20 17:26:54 +00:00
|
|
|
is_simd = true;
|
2020-07-11 14:29:35 +00:00
|
|
|
if target != Target::Struct {
|
|
|
|
("a", "struct")
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::transparent => {
|
2018-01-03 16:43:30 +00:00
|
|
|
is_transparent = true;
|
2019-05-22 14:31:09 +00:00
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Union | Target::Enum => continue,
|
|
|
|
_ => ("a", "struct, enum, or union"),
|
2018-01-03 16:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-22 23:54:04 +00:00
|
|
|
sym::no_niche => {
|
|
|
|
if !self.tcx.features().enabled(sym::no_niche) {
|
|
|
|
feature_err(
|
|
|
|
&self.tcx.sess.parse_sess,
|
|
|
|
sym::no_niche,
|
|
|
|
hint.span(),
|
|
|
|
"the attribute `repr(no_niche)` is currently unstable",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Enum => continue,
|
|
|
|
_ => ("a", "struct or enum"),
|
|
|
|
}
|
|
|
|
}
|
2019-12-22 22:42:04 +00:00
|
|
|
sym::i8
|
|
|
|
| sym::u8
|
|
|
|
| sym::i16
|
|
|
|
| sym::u16
|
|
|
|
| sym::i32
|
|
|
|
| sym::u32
|
|
|
|
| sym::i64
|
|
|
|
| sym::u64
|
2020-07-06 19:42:19 +00:00
|
|
|
| sym::i128
|
|
|
|
| sym::u128
|
2019-12-22 22:42:04 +00:00
|
|
|
| sym::isize
|
|
|
|
| sym::usize => {
|
2017-11-20 17:26:54 +00:00
|
|
|
int_reprs += 1;
|
2020-07-11 14:29:35 +00:00
|
|
|
if target != Target::Enum {
|
|
|
|
("an", "enum")
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2021-01-21 02:49:04 +00:00
|
|
|
_ => {
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
hint.span(),
|
|
|
|
E0552,
|
|
|
|
"unrecognized representation hint"
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-09 16:43:55 +00:00
|
|
|
};
|
2020-09-10 18:54:17 +00:00
|
|
|
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
2019-03-03 17:56:24 +00:00
|
|
|
hint.span(),
|
2020-09-10 18:54:17 +00:00
|
|
|
E0517,
|
|
|
|
"{}",
|
|
|
|
&format!("attribute should be applied to {} {}", article, allowed_targets)
|
2018-03-22 15:57:26 +00:00
|
|
|
)
|
2020-09-10 18:54:17 +00:00
|
|
|
.span_label(*span, &format!("not {} {}", article, allowed_targets))
|
|
|
|
.emit();
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2017-11-20 17:26:54 +00:00
|
|
|
|
2018-01-03 16:43:30 +00:00
|
|
|
// Just point at all repr hints if there are any incompatibilities.
|
|
|
|
// This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
|
2019-03-03 17:56:24 +00:00
|
|
|
let hint_spans = hints.iter().map(|hint| hint.span());
|
2018-01-03 16:43:30 +00:00
|
|
|
|
2020-01-22 23:54:04 +00:00
|
|
|
// Error on repr(transparent, <anything else apart from no_niche>).
|
|
|
|
let non_no_niche = |hint: &&NestedMetaItem| hint.name_or_empty() != sym::no_niche;
|
|
|
|
let non_no_niche_count = hints.iter().filter(non_no_niche).count();
|
|
|
|
if is_transparent && non_no_niche_count > 1 {
|
2018-01-03 16:43:30 +00:00
|
|
|
let hint_spans: Vec<_> = hint_spans.clone().collect();
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
2019-12-22 22:42:04 +00:00
|
|
|
self.tcx.sess,
|
|
|
|
hint_spans,
|
|
|
|
E0692,
|
|
|
|
"transparent {} cannot have other repr hints",
|
|
|
|
target
|
2019-12-31 20:25:16 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2018-01-03 16:43:30 +00:00
|
|
|
}
|
2017-11-20 17:26:54 +00:00
|
|
|
// Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
|
|
|
|
if (int_reprs > 1)
|
2019-12-22 22:42:04 +00:00
|
|
|
|| (is_simd && is_c)
|
2020-09-29 16:44:32 +00:00
|
|
|
|| (int_reprs == 1
|
|
|
|
&& is_c
|
|
|
|
&& item.map_or(false, |item| {
|
|
|
|
if let ItemLike::Item(item) = item {
|
|
|
|
return is_c_like_enum(item);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}))
|
2019-12-22 22:42:04 +00:00
|
|
|
{
|
2020-02-01 23:47:58 +00:00
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
CONFLICTING_REPR_HINTS,
|
|
|
|
hir_id,
|
|
|
|
hint_spans.collect::<Vec<Span>>(),
|
|
|
|
|lint| {
|
|
|
|
lint.build("conflicting representation hints")
|
|
|
|
.code(rustc_errors::error_code!(E0566))
|
|
|
|
.emit();
|
|
|
|
},
|
|
|
|
);
|
2016-07-03 03:24:27 +00:00
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
|
2019-11-28 22:50:47 +00:00
|
|
|
fn check_used(&self, attrs: &'hir [Attribute], target: Target) {
|
2019-10-11 01:33:16 +00:00
|
|
|
for attr in attrs {
|
2021-04-11 00:00:00 +00:00
|
|
|
if attr.has_name(sym::used) && target != Target::Static {
|
2019-12-22 22:42:04 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
2018-04-30 05:43:22 +00:00
|
|
|
.span_err(attr.span, "attribute must be applied to a `static` variable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 20:49:08 +00:00
|
|
|
|
|
|
|
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
|
|
|
|
/// (Allows proc_macro functions)
|
|
|
|
fn check_allow_internal_unstable(
|
|
|
|
&self,
|
2021-02-01 14:35:53 +00:00
|
|
|
hir_id: HirId,
|
2020-10-21 20:49:08 +00:00
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
attrs: &[Attribute],
|
|
|
|
) -> bool {
|
|
|
|
debug!("Checking target: {:?}", target);
|
2021-02-01 14:35:53 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn => {
|
|
|
|
for attr in attrs {
|
|
|
|
if self.tcx.sess.is_proc_macro_attr(attr) {
|
|
|
|
debug!("Is proc macro attr");
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-21 20:49:08 +00:00
|
|
|
}
|
2021-02-01 14:35:53 +00:00
|
|
|
debug!("Is not proc macro attr");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
Target::MacroDef => true,
|
|
|
|
// FIXME(#80564): We permit struct fields and match arms to have an
|
|
|
|
// `#[allow_internal_unstable]` 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::Field | Target::Arm => {
|
|
|
|
self.inline_attr_str_error_without_macro_def(
|
|
|
|
hir_id,
|
|
|
|
attr,
|
|
|
|
"allow_internal_unstable",
|
|
|
|
);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "attribute should be applied to a macro")
|
|
|
|
.span_label(*span, "not a macro")
|
|
|
|
.emit();
|
|
|
|
false
|
2020-10-21 20:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 22:02:26 +00:00
|
|
|
|
|
|
|
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
|
|
|
|
/// (Allows proc_macro functions)
|
|
|
|
fn check_rustc_allow_const_fn_unstable(
|
|
|
|
&self,
|
2020-10-23 15:54:48 +00:00
|
|
|
hir_id: HirId,
|
2020-10-21 22:02:26 +00:00
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2021-02-01 14:35:53 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn | Target::Method(_)
|
|
|
|
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id)) =>
|
|
|
|
{
|
|
|
|
true
|
|
|
|
}
|
|
|
|
// FIXME(#80564): We permit struct fields and match arms to have an
|
|
|
|
// `#[allow_internal_unstable]` 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::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "allow_internal_unstable");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "attribute should be applied to `const fn`")
|
|
|
|
.span_label(*span, "not a `const fn`")
|
|
|
|
.emit();
|
|
|
|
false
|
2020-10-23 15:54:48 +00:00
|
|
|
}
|
2020-10-21 22:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-10 03:13:52 +00:00
|
|
|
|
|
|
|
/// default_method_body_is_const should only be applied to trait methods with default bodies.
|
|
|
|
fn check_default_method_body_is_const(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Method(MethodKind::Trait { body: true }) => true,
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
"attribute should be applied to a trait method with body",
|
|
|
|
)
|
|
|
|
.span_label(*span, "not a trait method or missing a body")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
|
|
|
|
fn check_stability_promotable(&self, attr: &Attribute, _span: &Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Expression => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(attr.span, "attribute cannot be applied to an expression")
|
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_deprecated(&self, hir_id: HirId, attr: &Attribute, _span: &Span, target: Target) {
|
|
|
|
match target {
|
|
|
|
Target::Closure | Target::Expression | Target::Statement | Target::Arm => {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("attribute is ignored here").emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_macro_use(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
|
|
|
let name = attr.name_or_empty();
|
|
|
|
match target {
|
|
|
|
Target::ExternCrate | Target::Mod => {}
|
|
|
|
_ => {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build(&format!(
|
|
|
|
"`#[{name}]` only has an effect on `extern crate` and modules"
|
|
|
|
))
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_macro_export(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
|
|
|
if target != Target::MacroDef {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build(&format!("`#[macro_export]` only has an effect on macro definitions"))
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_cfg_attr(&self, hir_id: HirId, attr: &Attribute) {
|
|
|
|
if let Some((_, attrs)) = rustc_parse::parse_cfg_attr(&attr, &self.tcx.sess.parse_sess) {
|
|
|
|
if attrs.is_empty() {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("`#[cfg_attr]` does not expand to any attributes").emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_plugin_registrar(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
|
|
|
if target != Target::Fn {
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build("`#[plugin_registrar]` only has an effect on functions").emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
|
2020-01-07 16:25:33 +00:00
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
2020-02-09 14:32:00 +00:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::OnlyBodies(self.tcx.hir())
|
2018-01-08 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 18:28:50 +00:00
|
|
|
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
|
2015-09-25 06:25:59 +00:00
|
|
|
let target = Target::from_item(item);
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(item.hir_id(), &item.span, target, Some(ItemLike::Item(item)));
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_item(self, item)
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:03:30 +00:00
|
|
|
fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) {
|
|
|
|
let target = Target::from_generic_param(generic_param);
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(generic_param.hir_id, &generic_param.span, target, None);
|
2020-11-15 13:03:30 +00:00
|
|
|
intravisit::walk_generic_param(self, generic_param)
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:47:10 +00:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
|
2019-10-11 01:35:22 +00:00
|
|
|
let target = Target::from_trait_item(trait_item);
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(trait_item.hir_id(), &trait_item.span, target, None);
|
2019-10-11 01:35:22 +00:00
|
|
|
intravisit::walk_trait_item(self, trait_item)
|
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn visit_field_def(&mut self, struct_field: &'tcx hir::FieldDef<'tcx>) {
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(struct_field.hir_id, &struct_field.span, Target::Field, None);
|
2021-03-15 21:36:07 +00:00
|
|
|
intravisit::walk_field_def(self, struct_field);
|
2021-02-01 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(arm.hir_id, &arm.span, Target::Arm, None);
|
2021-02-01 14:35:53 +00:00
|
|
|
intravisit::walk_arm(self, arm);
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:44:32 +00:00
|
|
|
fn visit_foreign_item(&mut self, f_item: &'tcx ForeignItem<'tcx>) {
|
2019-10-11 01:35:49 +00:00
|
|
|
let target = Target::from_foreign_item(f_item);
|
2020-09-29 16:44:32 +00:00
|
|
|
self.check_attributes(
|
2021-01-31 23:33:38 +00:00
|
|
|
f_item.hir_id(),
|
2020-09-29 16:44:32 +00:00
|
|
|
&f_item.span,
|
|
|
|
target,
|
|
|
|
Some(ItemLike::ForeignItem(f_item)),
|
|
|
|
);
|
2019-10-11 01:35:49 +00:00
|
|
|
intravisit::walk_foreign_item(self, f_item)
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:16:44 +00:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2020-01-27 18:37:36 +00:00
|
|
|
let target = target_from_impl_item(self.tcx, impl_item);
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(impl_item.hir_id(), &impl_item.span, target, None);
|
2019-10-13 15:14:59 +00:00
|
|
|
intravisit::walk_impl_item(self, impl_item)
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
|
2020-09-10 18:54:17 +00:00
|
|
|
// When checking statements ignore expressions, they will be checked later.
|
|
|
|
if let hir::StmtKind::Local(ref l) = stmt.kind {
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(l.hir_id, &stmt.span, Target::Statement, None);
|
2020-09-10 18:54:17 +00:00
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_stmt(self, stmt)
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
2020-09-10 18:54:17 +00:00
|
|
|
let target = match expr.kind {
|
|
|
|
hir::ExprKind::Closure(..) => Target::Closure,
|
|
|
|
_ => Target::Expression,
|
|
|
|
};
|
|
|
|
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(expr.hir_id, &expr.span, target, None);
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_expr(self, expr)
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2020-09-10 18:54:17 +00:00
|
|
|
|
|
|
|
fn visit_variant(
|
|
|
|
&mut self,
|
|
|
|
variant: &'tcx hir::Variant<'tcx>,
|
|
|
|
generics: &'tcx hir::Generics<'tcx>,
|
|
|
|
item_id: HirId,
|
|
|
|
) {
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(variant.id, &variant.span, Target::Variant, None);
|
2020-09-10 18:54:17 +00:00
|
|
|
intravisit::walk_variant(self, variant, generics, item_id)
|
|
|
|
}
|
2021-01-08 09:00:39 +00:00
|
|
|
|
|
|
|
fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef<'tcx>) {
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(macro_def.hir_id(), ¯o_def.span, Target::MacroDef, None);
|
2021-01-08 09:00:39 +00:00
|
|
|
intravisit::walk_macro_def(self, macro_def);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
2020-11-25 22:27:23 +00:00
|
|
|
self.check_attributes(param.hir_id, ¶m.span, Target::Param, None);
|
2021-01-08 09:00:39 +00:00
|
|
|
|
|
|
|
intravisit::walk_param(self, param);
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 18:28:50 +00:00
|
|
|
fn is_c_like_enum(item: &Item<'_>) -> bool {
|
2019-10-11 01:33:16 +00:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
2019-11-29 08:26:18 +00:00
|
|
|
for variant in def.variants {
|
2019-08-14 00:40:21 +00:00
|
|
|
match variant.data {
|
2019-02-02 14:40:08 +00:00
|
|
|
hir::VariantData::Unit(..) => { /* continue */ }
|
2019-10-11 01:33:16 +00:00
|
|
|
_ => return false,
|
2017-11-20 17:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 20:13:52 +00:00
|
|
|
|
2020-10-03 18:45:39 +00:00
|
|
|
fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
|
|
|
|
const ATTRS_TO_CHECK: &[Symbol] = &[
|
|
|
|
sym::macro_export,
|
|
|
|
sym::repr,
|
|
|
|
sym::path,
|
|
|
|
sym::automatically_derived,
|
|
|
|
sym::start,
|
2021-04-08 13:37:38 +00:00
|
|
|
sym::rustc_main,
|
2020-10-03 18:45:39 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for attr in attrs {
|
|
|
|
for attr_to_check in ATTRS_TO_CHECK {
|
2021-07-29 17:00:41 +00:00
|
|
|
if attr.has_name(*attr_to_check) {
|
2020-10-03 18:45:39 +00:00
|
|
|
tcx.sess
|
|
|
|
.struct_span_err(
|
|
|
|
attr.span,
|
|
|
|
&format!(
|
|
|
|
"`{}` attribute cannot be used at crate level",
|
|
|
|
attr_to_check.to_ident_string()
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_invalid_macro_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
|
|
|
|
for attr in attrs {
|
2021-07-29 17:00:41 +00:00
|
|
|
if attr.has_name(sym::inline) {
|
2021-02-01 14:35:53 +00:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
attr.span,
|
|
|
|
E0518,
|
|
|
|
"attribute should be applied to function or closure",
|
|
|
|
)
|
|
|
|
.span_label(attr.span, "not a function or closure")
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 11:09:54 +00:00
|
|
|
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
2021-02-01 14:35:53 +00:00
|
|
|
let check_attr_visitor = &mut CheckAttrVisitor { tcx };
|
|
|
|
tcx.hir().visit_item_likes_in_module(module_def_id, &mut check_attr_visitor.as_deep_visitor());
|
2020-09-04 15:12:53 +00:00
|
|
|
if module_def_id.is_top_level_module() {
|
2020-11-25 22:27:23 +00:00
|
|
|
check_attr_visitor.check_attributes(CRATE_HIR_ID, &DUMMY_SP, Target::Mod, None);
|
2020-10-03 18:45:39 +00:00
|
|
|
check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs());
|
2021-07-24 17:30:46 +00:00
|
|
|
tcx.hir().visit_exported_macros_in_krate(check_attr_visitor);
|
|
|
|
check_invalid_macro_level_attr(tcx, tcx.hir().krate().non_exported_macro_attrs);
|
2020-09-04 15:12:53 +00:00
|
|
|
}
|
2018-06-06 20:13:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2019-12-22 22:42:04 +00:00
|
|
|
*providers = Providers { check_mod_attrs, ..*providers };
|
2018-06-06 20:13:52 +00:00
|
|
|
}
|