forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#69870 - petrochenkov:cfgacc, r=matthewjasper
expand: Implement something similar to `#[cfg(accessible(path))]` cc rust-lang#64797 The feature is implemented as a `#[cfg_accessible(path)]` attribute macro rather than as `#[cfg(accessible(path))]` because it needs to wait until `path` becomes resolvable, and `cfg` cannot wait, but macros can wait. Later we can think about desugaring or not desugaring `#[cfg(accessible(path))]` into `#[cfg_accessible(path)]`. This implementation is also incomplete in the sense that it never returns "false" from `cfg_accessible(path)`, it requires some tweaks to resolve, which is not quite ready to answer queries like this during early resolution. However, the most important part of this PR is not `cfg_accessible` itself, but expansion infrastructure for retrying expansions. Before this PR we could say "we cannot resolve this macro path, let's try it later", with this PR we can say "we cannot expand this macro, let's try it later" as well. This is a pre-requisite for - turning `#[derive(...)]` into a regular attribute macro, - properly supporting eager expansion for macros that cannot yet be resolved like ``` fn main() { println!(not_available_yet!()); } macro_rules! make_available { () => { #[macro_export] macro_rules! not_available_yet { () => { "Hello world!" } }} } make_available!(); ```
- Loading branch information
Showing
22 changed files
with
439 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 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 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,54 @@ | ||
//! Implementation of the `#[cfg_accessible(path)]` attribute macro. | ||
|
||
use rustc_ast::ast; | ||
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier}; | ||
use rustc_feature::AttributeTemplate; | ||
use rustc_parse::validate_attr; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
|
||
crate struct Expander; | ||
|
||
fn validate_input<'a>(ecx: &mut ExtCtxt<'_>, mi: &'a ast::MetaItem) -> Option<&'a ast::Path> { | ||
match mi.meta_item_list() { | ||
None => {} | ||
Some([]) => ecx.span_err(mi.span, "`cfg_accessible` path is not specified"), | ||
Some([_, .., l]) => ecx.span_err(l.span(), "multiple `cfg_accessible` paths are specified"), | ||
Some([nmi]) => match nmi.meta_item() { | ||
None => ecx.span_err(nmi.span(), "`cfg_accessible` path cannot be a literal"), | ||
Some(mi) => { | ||
if !mi.is_word() { | ||
ecx.span_err(mi.span, "`cfg_accessible` path cannot accept arguments"); | ||
} | ||
return Some(&mi.path); | ||
} | ||
}, | ||
} | ||
None | ||
} | ||
|
||
impl MultiItemModifier for Expander { | ||
fn expand( | ||
&self, | ||
ecx: &mut ExtCtxt<'_>, | ||
_span: Span, | ||
meta_item: &ast::MetaItem, | ||
item: Annotatable, | ||
) -> ExpandResult<Vec<Annotatable>, Annotatable> { | ||
let template = AttributeTemplate { list: Some("path"), ..Default::default() }; | ||
let attr = &ecx.attribute(meta_item.clone()); | ||
validate_attr::check_builtin_attribute(ecx.parse_sess, attr, sym::cfg_accessible, template); | ||
|
||
let path = match validate_input(ecx, meta_item) { | ||
Some(path) => path, | ||
None => return ExpandResult::Ready(Vec::new()), | ||
}; | ||
|
||
let failure_msg = "cannot determine whether the path is accessible or not"; | ||
match ecx.resolver.cfg_accessible(ecx.current_expansion.id, path) { | ||
Ok(true) => ExpandResult::Ready(vec![item]), | ||
Ok(false) => ExpandResult::Ready(Vec::new()), | ||
Err(_) => ExpandResult::Retry(item, failure_msg.into()), | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.