-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Split libsyntax apart #65324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Split libsyntax apart #65324
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| //! Meta-syntax validation logic of attributes for post-expansion. | ||
|
|
||
| use crate::ast::{self, Attribute, AttrKind, Ident, MetaItem}; | ||
| use crate::attr::{AttributeTemplate, mk_name_value_item_str}; | ||
| use crate::sess::ParseSess; | ||
| use crate::feature_gate::BUILTIN_ATTRIBUTE_MAP; | ||
| use crate::early_buffered_lints::BufferedEarlyLintId; | ||
| use crate::token; | ||
| use crate::tokenstream::TokenTree; | ||
|
|
||
| use errors::{PResult, Applicability}; | ||
| use syntax_pos::{Symbol, sym}; | ||
|
|
||
| pub fn check_meta(sess: &ParseSess, attr: &Attribute) { | ||
| let attr_info = | ||
| attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a); | ||
|
|
||
| // Check input tokens for built-in and key-value attributes. | ||
| match attr_info { | ||
| // `rustc_dummy` doesn't have any restrictions specific to built-in attributes. | ||
| Some((name, _, template, _)) if name != sym::rustc_dummy => | ||
| check_builtin_attribute(sess, attr, name, template), | ||
| _ => if let Some(TokenTree::Token(token)) = attr.get_normal_item().tokens.trees().next() { | ||
| if token == token::Eq { | ||
| // All key-value attributes are restricted to meta-item syntax. | ||
| parse_meta(sess, attr).map_err(|mut err| err.emit()).ok(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> { | ||
| Ok(match attr.kind { | ||
| AttrKind::Normal(ref item) => MetaItem { | ||
| path: item.path.clone(), | ||
| kind: super::parse_in_attr(sess, attr, |p| p.parse_meta_item_kind())?, | ||
| span: attr.span, | ||
| }, | ||
| AttrKind::DocComment(comment) => { | ||
| mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| pub fn check_builtin_attribute( | ||
| sess: &ParseSess, | ||
| attr: &Attribute, | ||
| name: Symbol, | ||
| template: AttributeTemplate, | ||
| ) { | ||
| // Some special attributes like `cfg` must be checked | ||
| // before the generic check, so we skip them here. | ||
| let should_skip = |name| name == sym::cfg; | ||
| // Some of previously accepted forms were used in practice, | ||
| // report them as warnings for now. | ||
| let should_warn = |name| name == sym::doc || name == sym::ignore || | ||
| name == sym::inline || name == sym::link || | ||
| name == sym::test || name == sym::bench; | ||
|
|
||
| match parse_meta(sess, attr) { | ||
| Ok(meta) => if !should_skip(name) && !template.compatible(&meta.kind) { | ||
| let error_msg = format!("malformed `{}` attribute input", name); | ||
| let mut msg = "attribute must be of the form ".to_owned(); | ||
| let mut suggestions = vec![]; | ||
| let mut first = true; | ||
| if template.word { | ||
| first = false; | ||
| let code = format!("#[{}]", name); | ||
| msg.push_str(&format!("`{}`", &code)); | ||
| suggestions.push(code); | ||
| } | ||
| if let Some(descr) = template.list { | ||
| if !first { | ||
| msg.push_str(" or "); | ||
| } | ||
| first = false; | ||
| let code = format!("#[{}({})]", name, descr); | ||
| msg.push_str(&format!("`{}`", &code)); | ||
| suggestions.push(code); | ||
| } | ||
| if let Some(descr) = template.name_value_str { | ||
| if !first { | ||
| msg.push_str(" or "); | ||
| } | ||
| let code = format!("#[{} = \"{}\"]", name, descr); | ||
| msg.push_str(&format!("`{}`", &code)); | ||
| suggestions.push(code); | ||
| } | ||
| if should_warn(name) { | ||
| sess.buffer_lint( | ||
| BufferedEarlyLintId::IllFormedAttributeInput, | ||
| meta.span, | ||
| ast::CRATE_NODE_ID, | ||
| &msg, | ||
| ); | ||
| } else { | ||
| sess.span_diagnostic.struct_span_err(meta.span, &error_msg) | ||
| .span_suggestions( | ||
| meta.span, | ||
| if suggestions.len() == 1 { | ||
| "must be of the form" | ||
| } else { | ||
| "the following are the possible correct uses" | ||
| }, | ||
| suggestions.into_iter(), | ||
| Applicability::HasPlaceholders, | ||
| ).emit(); | ||
| } | ||
| } | ||
| Err(mut err) => err.emit(), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| use syntax_pos::Symbol; | ||
| use syntax::ast::MetaItem; | ||
| use syntax::attr::{check_builtin_attribute, AttributeTemplate}; | ||
| use syntax::attr::AttributeTemplate; | ||
| use syntax::parse::validate_attr; | ||
| use syntax_expand::base::ExtCtxt; | ||
|
|
||
| pub fn check_builtin_macro_attribute(ecx: &ExtCtxt<'_>, meta_item: &MetaItem, name: Symbol) { | ||
| // All the built-in macro attributes are "words" at the moment. | ||
| let template = AttributeTemplate::only_word(); | ||
| let attr = ecx.attribute(meta_item.clone()); | ||
| check_builtin_attribute(ecx.parse_sess, &attr, name, template); | ||
| validate_attr::check_builtin_attribute(ecx.parse_sess, &attr, name, template); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.