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.
|
|
|
|
|
2019-11-28 22:50:47 +00:00
|
|
|
use crate::hir::{self, HirId, Attribute, Item, ItemKind, TraitItem, TraitItemKind};
|
2019-10-11 01:33:16 +00:00
|
|
|
use crate::hir::DUMMY_HIR_ID;
|
2019-02-05 17:20:45 +00:00
|
|
|
use crate::hir::def_id::DefId;
|
|
|
|
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2019-10-11 01:35:22 +00:00
|
|
|
use crate::lint::builtin::UNUSED_ATTRIBUTES;
|
2019-09-06 02:57:44 +00:00
|
|
|
use crate::ty::TyCtxt;
|
|
|
|
use crate::ty::query::Providers;
|
|
|
|
|
2018-10-11 17:36:51 +00:00
|
|
|
use std::fmt::{self, Display};
|
2019-10-03 01:24:10 +00:00
|
|
|
use syntax::{attr, symbol::sym};
|
2018-10-11 17:36:51 +00:00
|
|
|
use syntax_pos::Span;
|
2015-09-25 06:25:59 +00:00
|
|
|
|
2019-11-11 21:46:56 +00:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2019-10-24 21:21:30 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
pub(crate) enum MethodKind {
|
|
|
|
Trait { body: bool },
|
|
|
|
Inherent,
|
|
|
|
}
|
|
|
|
|
2015-09-25 06:25:59 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
2018-10-11 17:36:51 +00:00
|
|
|
pub(crate) enum Target {
|
|
|
|
ExternCrate,
|
|
|
|
Use,
|
|
|
|
Static,
|
|
|
|
Const,
|
2015-09-25 06:25:59 +00:00
|
|
|
Fn,
|
2018-10-11 17:36:51 +00:00
|
|
|
Closure,
|
|
|
|
Mod,
|
|
|
|
ForeignMod,
|
|
|
|
GlobalAsm,
|
2019-08-02 20:01:00 +00:00
|
|
|
TyAlias,
|
2019-07-31 23:41:54 +00:00
|
|
|
OpaqueTy,
|
2018-10-11 17:36:51 +00:00
|
|
|
Enum,
|
2015-09-25 06:25:59 +00:00
|
|
|
Struct,
|
2016-08-10 18:00:17 +00:00
|
|
|
Union,
|
2018-10-11 17:36:51 +00:00
|
|
|
Trait,
|
|
|
|
TraitAlias,
|
|
|
|
Impl,
|
2018-03-22 15:57:26 +00:00
|
|
|
Expression,
|
|
|
|
Statement,
|
2019-10-11 01:35:22 +00:00
|
|
|
AssocConst,
|
2019-10-24 21:21:30 +00:00
|
|
|
Method(MethodKind),
|
2019-10-11 01:35:22 +00:00
|
|
|
AssocTy,
|
2019-10-11 01:35:49 +00:00
|
|
|
ForeignFn,
|
|
|
|
ForeignStatic,
|
|
|
|
ForeignTy,
|
2018-10-11 17:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Target {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{}", match *self {
|
|
|
|
Target::ExternCrate => "extern crate",
|
|
|
|
Target::Use => "use",
|
|
|
|
Target::Static => "static item",
|
|
|
|
Target::Const => "constant item",
|
|
|
|
Target::Fn => "function",
|
|
|
|
Target::Closure => "closure",
|
|
|
|
Target::Mod => "module",
|
|
|
|
Target::ForeignMod => "foreign module",
|
|
|
|
Target::GlobalAsm => "global asm",
|
2019-08-02 20:01:00 +00:00
|
|
|
Target::TyAlias => "type alias",
|
2019-07-31 23:41:54 +00:00
|
|
|
Target::OpaqueTy => "opaque type",
|
2018-10-11 17:36:51 +00:00
|
|
|
Target::Enum => "enum",
|
|
|
|
Target::Struct => "struct",
|
|
|
|
Target::Union => "union",
|
|
|
|
Target::Trait => "trait",
|
|
|
|
Target::TraitAlias => "trait alias",
|
|
|
|
Target::Impl => "item",
|
|
|
|
Target::Expression => "expression",
|
|
|
|
Target::Statement => "statement",
|
2019-10-11 01:35:22 +00:00
|
|
|
Target::AssocConst => "associated const",
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Method(_) => "method",
|
2019-10-11 01:35:22 +00:00
|
|
|
Target::AssocTy => "associated type",
|
2019-10-11 01:35:49 +00:00
|
|
|
Target::ForeignFn => "foreign function",
|
|
|
|
Target::ForeignStatic => "foreign static item",
|
|
|
|
Target::ForeignTy => "foreign type",
|
2018-10-11 17:36:51 +00:00
|
|
|
})
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Target {
|
2019-11-28 18:28:50 +00:00
|
|
|
pub(crate) fn from_item(item: &Item<'_>) -> Target {
|
2019-09-26 16:51:36 +00:00
|
|
|
match item.kind {
|
2019-10-11 01:33:16 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 01:35:22 +00:00
|
|
|
|
|
|
|
fn from_trait_item(trait_item: &TraitItem) -> Target {
|
|
|
|
match trait_item.kind {
|
|
|
|
TraitItemKind::Const(..) => Target::AssocConst,
|
|
|
|
TraitItemKind::Method(_, hir::TraitMethod::Required(_)) => {
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Method(MethodKind::Trait { body: false })
|
2019-10-11 01:35:22 +00:00
|
|
|
}
|
|
|
|
TraitItemKind::Method(_, hir::TraitMethod::Provided(_)) => {
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Method(MethodKind::Trait { body: true })
|
2019-10-11 01:35:22 +00:00
|
|
|
}
|
|
|
|
TraitItemKind::Type(..) => Target::AssocTy,
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 01:35:49 +00:00
|
|
|
|
2019-11-28 19:18:29 +00:00
|
|
|
fn from_foreign_item(foreign_item: &hir::ForeignItem<'_>) -> Target {
|
2019-10-11 01:35:49 +00:00
|
|
|
match foreign_item.kind {
|
|
|
|
hir::ForeignItemKind::Fn(..) => Target::ForeignFn,
|
|
|
|
hir::ForeignItemKind::Static(..) => Target::ForeignStatic,
|
|
|
|
hir::ForeignItemKind::Type => Target::ForeignTy,
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-13 15:14:59 +00:00
|
|
|
|
2019-10-24 21:21:30 +00:00
|
|
|
fn from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem) -> Target {
|
2019-10-13 15:14:59 +00:00
|
|
|
match impl_item.kind {
|
2019-10-25 23:46:07 +00:00
|
|
|
hir::ImplItemKind::Const(..) => Target::AssocConst,
|
2019-10-24 21:21:30 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 23:46:07 +00:00
|
|
|
hir::ImplItemKind::TyAlias(..) | hir::ImplItemKind::OpaqueTy(..) => Target::AssocTy,
|
2019-10-13 15:14:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2019-11-28 22:50:47 +00:00
|
|
|
attrs: &'hir [Attribute],
|
2019-10-11 01:33:16 +00:00
|
|
|
span: &Span,
|
|
|
|
target: Target,
|
2019-11-28 18:28:50 +00:00
|
|
|
item: Option<&Item<'_>>,
|
2019-10-11 01:33:16 +00:00
|
|
|
) {
|
2019-09-26 10:00:53 +00:00
|
|
|
let mut is_valid = true;
|
2019-10-11 01:33:16 +00:00
|
|
|
for attr in attrs {
|
2019-09-26 10:00:53 +00:00
|
|
|
is_valid &= if attr.check_name(sym::inline) {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_inline(hir_id, attr, span, target)
|
2019-05-08 03:21:18 +00:00
|
|
|
} else if attr.check_name(sym::non_exhaustive) {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_non_exhaustive(attr, span, target)
|
2019-05-08 03:21:18 +00:00
|
|
|
} else if attr.check_name(sym::marker) {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_marker(attr, span, target)
|
2019-09-26 10:00:53 +00:00
|
|
|
} else if attr.check_name(sym::target_feature) {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_target_feature(attr, span, target)
|
2019-07-20 00:55:58 +00:00
|
|
|
} else if attr.check_name(sym::track_caller) {
|
2019-10-11 01:42:23 +00:00
|
|
|
self.check_track_caller(&attr.span, attrs, span, target)
|
2019-09-26 10:00:53 +00:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if !is_valid {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-26 16:03:29 +00:00
|
|
|
if target == Target::Fn {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.tcx.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
|
|
|
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_repr(attrs, span, target, item);
|
|
|
|
self.check_used(attrs, target);
|
2017-06-03 16:14:29 +00:00
|
|
|
}
|
|
|
|
|
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-10-24 21:21:30 +00:00
|
|
|
Target::Fn | Target::Closure | Target::Method(MethodKind::Trait { body: true })
|
|
|
|
| Target::Method(MethodKind::Inherent) => true,
|
|
|
|
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
|
2019-10-11 01:35:22 +00:00
|
|
|
self.tcx.struct_span_lint_hir(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
"`#[inline]` is ignored on function prototypes",
|
|
|
|
).emit();
|
|
|
|
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 => {
|
|
|
|
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
|
|
|
|
}
|
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",
|
|
|
|
).span_label(*span, "not a function or closure")
|
2019-10-11 01:33:16 +00:00
|
|
|
.emit();
|
|
|
|
false
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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 {
|
|
|
|
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
|
|
|
|
}
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Fn | Target::Method(MethodKind::Inherent) => true,
|
|
|
|
Target::Method(_) => {
|
2019-10-14 14:07:16 +00:00
|
|
|
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
|
|
|
|
}
|
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.
|
|
|
|
fn check_non_exhaustive(
|
|
|
|
&self,
|
2019-10-11 01:33:16 +00:00
|
|
|
attr: &Attribute,
|
|
|
|
span: &Span,
|
2019-09-26 10:00:53 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2018-03-24 18:07:18 +00:00
|
|
|
match target {
|
2019-09-26 10:00:53 +00:00
|
|
|
Target::Struct | Target::Enum => true,
|
2018-03-24 18:07:18 +00:00
|
|
|
_ => {
|
|
|
|
struct_span_err!(self.tcx.sess,
|
|
|
|
attr.span,
|
2018-06-10 12:04:48 +00:00
|
|
|
E0701,
|
2018-03-24 18:07:18 +00:00
|
|
|
"attribute can only be applied to a struct or enum")
|
2019-10-11 01:33:16 +00:00
|
|
|
.span_label(*span, "not a struct or enum")
|
2018-03-24 18:07:18 +00:00
|
|
|
.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.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_marker(&self, 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,
|
2018-08-25 07:54:41 +00:00
|
|
|
_ => {
|
2018-09-03 09:17:20 +00:00
|
|
|
self.tcx.sess
|
|
|
|
.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.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_target_feature(&self, attr: &Attribute, span: &Span, target: Target) -> bool {
|
2019-09-26 10:00:53 +00:00
|
|
|
match target {
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Fn | Target::Method(MethodKind::Trait { body: true })
|
|
|
|
| Target::Method(MethodKind::Inherent) => true,
|
2019-09-26 10:00:53 +00:00
|
|
|
_ => {
|
|
|
|
self.tcx.sess
|
|
|
|
.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-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,
|
2019-11-28 18:28:50 +00:00
|
|
|
item: Option<&Item<'_>>,
|
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()
|
2019-05-08 03:21:18 +00:00
|
|
|
.filter(|attr| attr.check_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 {
|
2019-05-08 04:33:06 +00:00
|
|
|
let (article, allowed_targets) = match hint.name_or_empty() {
|
|
|
|
name @ sym::C | name @ sym::align => {
|
|
|
|
is_c |= name == sym::C;
|
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
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::packed => {
|
2016-08-10 18:00:17 +00:00
|
|
|
if target != Target::Struct &&
|
|
|
|
target != Target::Union {
|
2018-01-01 20:42:12 +00:00
|
|
|
("a", "struct or union")
|
2016-07-03 03:24:27 +00:00
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::simd => {
|
2017-11-20 17:26:54 +00:00
|
|
|
is_simd = true;
|
2015-09-25 06:25:59 +00:00
|
|
|
if target != Target::Struct {
|
2018-01-01 20:42:12 +00:00
|
|
|
("a", "struct")
|
2015-11-09 16:43:55 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::i8 | sym::u8 | sym::i16 | sym::u16 |
|
|
|
|
sym::i32 | sym::u32 | sym::i64 | sym::u64 |
|
|
|
|
sym::isize | sym::usize => {
|
2017-11-20 17:26:54 +00:00
|
|
|
int_reprs += 1;
|
2015-09-25 06:25:59 +00:00
|
|
|
if target != Target::Enum {
|
2018-01-01 20:42:12 +00:00
|
|
|
("an", "enum")
|
2015-11-09 16:43:55 +00:00
|
|
|
} else {
|
|
|
|
continue
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-09 16:43:55 +00:00
|
|
|
_ => continue,
|
|
|
|
};
|
2018-03-22 15:57:26 +00:00
|
|
|
self.emit_repr_error(
|
2019-03-03 17:56:24 +00:00
|
|
|
hint.span(),
|
2019-10-11 01:33:16 +00:00
|
|
|
*span,
|
2018-03-22 15:57:26 +00:00
|
|
|
&format!("attribute should be applied to {}", allowed_targets),
|
|
|
|
&format!("not {} {}", article, allowed_targets),
|
|
|
|
)
|
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
|
|
|
|
|
|
|
// Error on repr(transparent, <anything else>).
|
|
|
|
if is_transparent && hints.len() > 1 {
|
|
|
|
let hint_spans: Vec<_> = hint_spans.clone().collect();
|
|
|
|
span_err!(self.tcx.sess, hint_spans, E0692,
|
2019-05-22 14:31:09 +00:00
|
|
|
"transparent {} cannot have other repr hints", target);
|
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)
|
|
|
|
|| (is_simd && is_c)
|
2019-10-13 15:14:59 +00:00
|
|
|
|| (int_reprs == 1 && is_c && item.map_or(false, |item| is_c_like_enum(item))) {
|
2018-01-03 16:43:30 +00:00
|
|
|
let hint_spans: Vec<_> = hint_spans.collect();
|
|
|
|
span_warn!(self.tcx.sess, hint_spans, E0566,
|
2016-07-03 03:24:27 +00:00
|
|
|
"conflicting representation hints");
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
|
|
|
|
fn emit_repr_error(
|
|
|
|
&self,
|
|
|
|
hint_span: Span,
|
|
|
|
label_span: Span,
|
|
|
|
hint_message: &str,
|
|
|
|
label_message: &str,
|
|
|
|
) {
|
|
|
|
struct_span_err!(self.tcx.sess, hint_span, E0517, "{}", hint_message)
|
|
|
|
.span_label(label_span, label_message)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt_attributes(&self, stmt: &hir::Stmt) {
|
|
|
|
// When checking statements ignore expressions, they will be checked later
|
2019-09-26 16:34:50 +00:00
|
|
|
if let hir::StmtKind::Local(ref l) = stmt.kind {
|
2019-01-16 23:39:24 +00:00
|
|
|
for attr in l.attrs.iter() {
|
2019-05-08 03:21:18 +00:00
|
|
|
if attr.check_name(sym::inline) {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_inline(DUMMY_HIR_ID, attr, &stmt.span, Target::Statement);
|
2018-03-22 15:57:26 +00:00
|
|
|
}
|
2019-05-08 03:21:18 +00:00
|
|
|
if attr.check_name(sym::repr) {
|
2018-03-22 15:57:26 +00:00
|
|
|
self.emit_repr_error(
|
|
|
|
attr.span,
|
|
|
|
stmt.span,
|
2018-07-28 12:40:32 +00:00
|
|
|
"attribute should not be applied to a statement",
|
2019-05-22 14:31:09 +00:00
|
|
|
"not a struct, enum, or union",
|
2018-03-22 15:57:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr_attributes(&self, expr: &hir::Expr) {
|
2019-09-26 13:39:48 +00:00
|
|
|
let target = match expr.kind {
|
2018-07-11 12:05:29 +00:00
|
|
|
hir::ExprKind::Closure(..) => Target::Closure,
|
2018-04-27 05:20:46 +00:00
|
|
|
_ => Target::Expression,
|
|
|
|
};
|
2018-03-22 15:57:26 +00:00
|
|
|
for attr in expr.attrs.iter() {
|
2019-05-08 03:21:18 +00:00
|
|
|
if attr.check_name(sym::inline) {
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_inline(DUMMY_HIR_ID, attr, &expr.span, target);
|
2018-03-22 15:57:26 +00:00
|
|
|
}
|
2019-05-08 03:21:18 +00:00
|
|
|
if attr.check_name(sym::repr) {
|
2018-03-22 15:57:26 +00:00
|
|
|
self.emit_repr_error(
|
|
|
|
attr.span,
|
|
|
|
expr.span,
|
2018-07-28 12:40:32 +00:00
|
|
|
"attribute should not be applied to an expression",
|
2019-05-22 14:31:09 +00:00
|
|
|
"not defining a struct, enum, or union",
|
2018-03-22 15:57:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 05:43:22 +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 {
|
2019-05-08 03:21:18 +00:00
|
|
|
if attr.check_name(sym::used) && target != Target::Static {
|
2018-04-30 05:43:22 +00:00
|
|
|
self.tcx.sess
|
|
|
|
.span_err(attr.span, "attribute must be applied to a `static` variable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
|
2018-01-08 21:43:42 +00:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 12:45:36 +00:00
|
|
|
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);
|
2019-11-28 22:50:47 +00:00
|
|
|
self.check_attributes(item.hir_id, item.attrs, &item.span, target, Some(item));
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_item(self, item)
|
|
|
|
}
|
|
|
|
|
2019-10-11 01:35:22 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
|
2019-11-28 19:18:29 +00:00
|
|
|
fn visit_foreign_item(&mut self, f_item: &'tcx hir::ForeignItem<'tcx>) {
|
2019-10-11 01:35:49 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-10-13 15:14:59 +00:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
|
2019-10-24 21:21:30 +00:00
|
|
|
let target = Target::from_impl_item(self.tcx, impl_item);
|
2019-10-13 15:14:59 +00:00
|
|
|
self.check_attributes(impl_item.hir_id, &impl_item.attrs, &impl_item.span, target, None);
|
|
|
|
intravisit::walk_impl_item(self, impl_item)
|
|
|
|
}
|
|
|
|
|
2018-03-22 15:57:26 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt) {
|
|
|
|
self.check_stmt_attributes(stmt);
|
|
|
|
intravisit::walk_stmt(self, stmt)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
|
|
|
|
self.check_expr_attributes(expr);
|
|
|
|
intravisit::walk_expr(self, expr)
|
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 {
|
2017-11-20 17:26:54 +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
|
|
|
|
2019-06-21 18:05:14 +00:00
|
|
|
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: DefId) {
|
2019-01-11 03:58:46 +00:00
|
|
|
tcx.hir().visit_item_likes_in_module(
|
2018-06-06 20:13:52 +00:00
|
|
|
module_def_id,
|
|
|
|
&mut CheckAttrVisitor { tcx }.as_deep_visitor()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn provide(providers: &mut Providers<'_>) {
|
|
|
|
*providers = Providers {
|
|
|
|
check_mod_attrs,
|
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|