rustc: Refactor attribute checking to operate on HIR
This'll enable running queries that could be cached and overall be more amenable to the query infastructure.
This commit is contained in:
parent
5f006cebfc
commit
0ecaa67e90
5 changed files with 59 additions and 47 deletions
|
@ -14,11 +14,10 @@
|
|||
//! conflicts between multiple such attributes attached to the same
|
||||
//! item.
|
||||
|
||||
use session::Session;
|
||||
use ty::TyCtxt;
|
||||
|
||||
use syntax::ast;
|
||||
use syntax::visit;
|
||||
use syntax::visit::Visitor;
|
||||
use hir;
|
||||
use hir::intravisit::{self, Visitor, NestedVisitorMap};
|
||||
|
||||
#[derive(Copy, Clone, PartialEq)]
|
||||
enum Target {
|
||||
|
@ -30,24 +29,26 @@ enum Target {
|
|||
}
|
||||
|
||||
impl Target {
|
||||
fn from_item(item: &ast::Item) -> Target {
|
||||
fn from_item(item: &hir::Item) -> Target {
|
||||
match item.node {
|
||||
ast::ItemKind::Fn(..) => Target::Fn,
|
||||
ast::ItemKind::Struct(..) => Target::Struct,
|
||||
ast::ItemKind::Union(..) => Target::Union,
|
||||
ast::ItemKind::Enum(..) => Target::Enum,
|
||||
hir::ItemFn(..) => Target::Fn,
|
||||
hir::ItemStruct(..) => Target::Struct,
|
||||
hir::ItemUnion(..) => Target::Union,
|
||||
hir::ItemEnum(..) => Target::Enum,
|
||||
_ => Target::Other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CheckAttrVisitor<'a> {
|
||||
sess: &'a Session,
|
||||
struct CheckAttrVisitor<'a, 'tcx: 'a> {
|
||||
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||
}
|
||||
|
||||
impl<'a> CheckAttrVisitor<'a> {
|
||||
impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
|
||||
/// Check any attribute.
|
||||
fn check_attributes(&self, item: &ast::Item, target: Target) {
|
||||
fn check_attributes(&self, item: &hir::Item, target: Target) {
|
||||
self.tcx.target_features_enabled(self.tcx.hir.local_def_id(item.id));
|
||||
|
||||
for attr in &item.attrs {
|
||||
if let Some(name) = attr.name() {
|
||||
if name == "inline" {
|
||||
|
@ -55,20 +56,24 @@ impl<'a> CheckAttrVisitor<'a> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.check_repr(item, target);
|
||||
}
|
||||
|
||||
/// Check if an `#[inline]` is applied to a function.
|
||||
fn check_inline(&self, attr: &ast::Attribute, item: &ast::Item, target: Target) {
|
||||
fn check_inline(&self, attr: &hir::Attribute, item: &hir::Item, target: Target) {
|
||||
if target != Target::Fn {
|
||||
struct_span_err!(self.sess, attr.span, E0518, "attribute should be applied to function")
|
||||
struct_span_err!(self.tcx.sess,
|
||||
attr.span,
|
||||
E0518,
|
||||
"attribute should be applied to function")
|
||||
.span_label(item.span, "not a function")
|
||||
.emit();
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the `#[repr]` attributes on `item` are valid.
|
||||
fn check_repr(&self, item: &ast::Item, target: Target) {
|
||||
fn check_repr(&self, item: &hir::Item, target: Target) {
|
||||
// Extract the names of all repr hints, e.g., [foo, bar, align] for:
|
||||
// ```
|
||||
// #[repr(foo)]
|
||||
|
@ -144,7 +149,7 @@ impl<'a> CheckAttrVisitor<'a> {
|
|||
}
|
||||
_ => continue,
|
||||
};
|
||||
struct_span_err!(self.sess, hint.span, E0517,
|
||||
struct_span_err!(self.tcx.sess, hint.span, E0517,
|
||||
"attribute should be applied to {}", allowed_targets)
|
||||
.span_label(item.span, format!("not {} {}", article, allowed_targets))
|
||||
.emit();
|
||||
|
@ -154,32 +159,37 @@ impl<'a> CheckAttrVisitor<'a> {
|
|||
if (int_reprs > 1)
|
||||
|| (is_simd && is_c)
|
||||
|| (int_reprs == 1 && is_c && is_c_like_enum(item)) {
|
||||
// Just point at all repr hints. This is not ideal, but tracking precisely which ones
|
||||
// are at fault is a huge hassle.
|
||||
// Just point at all repr hints. This is not ideal, but tracking
|
||||
// precisely which ones are at fault is a huge hassle.
|
||||
let spans: Vec<_> = hints.iter().map(|hint| hint.span).collect();
|
||||
span_warn!(self.sess, spans, E0566,
|
||||
span_warn!(self.tcx.sess, spans, E0566,
|
||||
"conflicting representation hints");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Visitor<'a> for CheckAttrVisitor<'a> {
|
||||
fn visit_item(&mut self, item: &'a ast::Item) {
|
||||
impl<'a, 'tcx> Visitor<'tcx> for CheckAttrVisitor<'a, 'tcx> {
|
||||
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
||||
NestedVisitorMap::None
|
||||
}
|
||||
|
||||
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
||||
let target = Target::from_item(item);
|
||||
self.check_attributes(item, target);
|
||||
visit::walk_item(self, item);
|
||||
intravisit::walk_item(self, item);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_crate(sess: &Session, krate: &ast::Crate) {
|
||||
visit::walk_crate(&mut CheckAttrVisitor { sess: sess }, krate);
|
||||
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
||||
let mut checker = CheckAttrVisitor { tcx };
|
||||
tcx.hir.krate().visit_all_item_likes(&mut checker.as_deep_visitor());
|
||||
}
|
||||
|
||||
fn is_c_like_enum(item: &ast::Item) -> bool {
|
||||
if let ast::ItemKind::Enum(ref def, _) = item.node {
|
||||
fn is_c_like_enum(item: &hir::Item) -> bool {
|
||||
if let hir::ItemEnum(ref def, _) = item.node {
|
||||
for variant in &def.variants {
|
||||
match variant.node.data {
|
||||
ast::VariantData::Unit(_) => { /* continue */ }
|
||||
hir::VariantData::Unit(_) => { /* continue */ }
|
||||
_ => { return false; }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue