Skip to content

Commit 8ad4d15

Browse files
committed
move AttributeTemplate to builtin_attrs
1 parent 048201f commit 8ad4d15

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

src/librustc_lint/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ use rustc_feature::Stability;
3939
use syntax::tokenstream::{TokenTree, TokenStream};
4040
use syntax::ast::{self, Expr};
4141
use syntax::ptr::P;
42-
use syntax::attr::{self, HasAttrs, AttributeTemplate};
42+
use syntax::attr::{self, HasAttrs};
4343
use syntax::source_map::Spanned;
4444
use syntax::edition::Edition;
45-
use syntax::feature_gate::{AttributeGate, AttributeType};
45+
use syntax::feature_gate::{AttributeGate, AttributeTemplate, AttributeType};
4646
use syntax::feature_gate::deprecated_attributes;
4747
use syntax_pos::{BytePos, Span};
4848
use syntax::symbol::{Symbol, kw, sym};

src/librustc_parse/validate_attr.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Meta-syntax validation logic of attributes for post-expansion.
22
33
use errors::{PResult, Applicability};
4-
use syntax::ast::{self, Attribute, AttrKind, Ident, MetaItem};
5-
use syntax::attr::{AttributeTemplate, mk_name_value_item_str};
4+
use syntax::feature_gate::AttributeTemplate;
5+
use syntax::ast::{self, Attribute, AttrKind, Ident, MetaItem, MetaItemKind};
6+
use syntax::attr::mk_name_value_item_str;
67
use syntax::early_buffered_lints::BufferedEarlyLintId;
78
use syntax::feature_gate::BUILTIN_ATTRIBUTE_MAP;
89
use syntax::token;
@@ -41,6 +42,16 @@ pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, Meta
4142
})
4243
}
4344

45+
/// Checks that the given meta-item is compatible with this `AttributeTemplate`.
46+
fn is_attr_template_compatible(template: &AttributeTemplate, meta: &ast::MetaItemKind) -> bool {
47+
match meta {
48+
MetaItemKind::Word => template.word,
49+
MetaItemKind::List(..) => template.list.is_some(),
50+
MetaItemKind::NameValue(lit) if lit.kind.is_str() => template.name_value_str.is_some(),
51+
MetaItemKind::NameValue(..) => false,
52+
}
53+
}
54+
4455
pub fn check_builtin_attribute(
4556
sess: &ParseSess,
4657
attr: &Attribute,
@@ -57,7 +68,7 @@ pub fn check_builtin_attribute(
5768
name == sym::test || name == sym::bench;
5869

5970
match parse_meta(sess, attr) {
60-
Ok(meta) => if !should_skip(name) && !template.compatible(&meta.kind) {
71+
Ok(meta) => if !should_skip(name) && !is_attr_template_compatible(&template, &meta.kind) {
6172
let error_msg = format!("malformed `{}` attribute input", name);
6273
let mut msg = "attribute must be of the form ".to_owned();
6374
let mut suggestions = vec![];

src/libsyntax/attr/builtin.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,6 @@ enum AttrError {
2525
UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
2626
}
2727

28-
/// A template that the attribute input must match.
29-
/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
30-
#[derive(Clone, Copy)]
31-
pub struct AttributeTemplate {
32-
pub word: bool,
33-
pub list: Option<&'static str>,
34-
pub name_value_str: Option<&'static str>,
35-
}
36-
37-
impl AttributeTemplate {
38-
pub fn only_word() -> Self {
39-
Self { word: true, list: None, name_value_str: None }
40-
}
41-
42-
/// Checks that the given meta-item is compatible with this template.
43-
pub fn compatible(&self, meta_item_kind: &ast::MetaItemKind) -> bool {
44-
match meta_item_kind {
45-
ast::MetaItemKind::Word => self.word,
46-
ast::MetaItemKind::List(..) => self.list.is_some(),
47-
ast::MetaItemKind::NameValue(lit) if lit.kind.is_str() => self.name_value_str.is_some(),
48-
ast::MetaItemKind::NameValue(..) => false,
49-
}
50-
}
51-
}
52-
5328
fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
5429
let diag = &sess.span_diagnostic;
5530
match error {

src/libsyntax/feature_gate/builtin_attrs.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use super::check::{EXPLAIN_ALLOW_INTERNAL_UNSAFE, EXPLAIN_ALLOW_INTERNAL_UNSTABL
88
use rustc_feature::{Features, Stability};
99

1010
use crate::ast;
11-
use crate::attr::AttributeTemplate;
1211
use crate::sess::ParseSess;
1312

1413
use syntax_pos::symbol::{Symbol, sym};
1514
use syntax_pos::Span;
1615
use rustc_data_structures::fx::FxHashMap;
1716
use lazy_static::lazy_static;
1817

18+
1919
type GateFn = fn(&Features) -> bool;
2020

2121
macro_rules! cfg_fn {
@@ -108,6 +108,21 @@ impl AttributeGate {
108108
}
109109
}
110110

111+
/// A template that the attribute input must match.
112+
/// Only top-level shape (`#[attr]` vs `#[attr(...)]` vs `#[attr = ...]`) is considered now.
113+
#[derive(Clone, Copy)]
114+
pub struct AttributeTemplate {
115+
pub word: bool,
116+
pub list: Option<&'static str>,
117+
pub name_value_str: Option<&'static str>,
118+
}
119+
120+
impl AttributeTemplate {
121+
pub fn only_word() -> Self {
122+
Self { word: true, list: None, name_value_str: None }
123+
}
124+
}
125+
111126
/// A convenience macro for constructing attribute templates.
112127
/// E.g., `template!(Word, List: "description")` means that the attribute
113128
/// supports forms `#[attr]` and `#[attr(description)]`.

src/libsyntax/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub mod feature_gate {
101101
};
102102
mod builtin_attrs;
103103
pub use builtin_attrs::{
104-
AttributeGate, AttributeType, GatedCfg,
104+
AttributeGate, AttributeTemplate, AttributeType, GatedCfg,
105105
BuiltinAttribute, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
106106
deprecated_attributes, is_builtin_attr, is_builtin_attr_name,
107107
};

src/libsyntax_ext/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_parse::validate_attr;
22
use syntax_pos::Symbol;
33
use syntax::ast::MetaItem;
4-
use syntax::attr::AttributeTemplate;
4+
use syntax::feature_gate::AttributeTemplate;
55
use syntax_expand::base::ExtCtxt;
66

77
pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) {

0 commit comments

Comments
 (0)