2020-03-10 00:56:20 +03:00
|
|
|
//! Implementation of the `#[cfg_accessible(path)]` attribute macro.
|
|
|
|
|
|
2023-04-08 20:37:41 +01:00
|
|
|
use crate::errors;
|
2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-11-14 14:47:14 +03:00
|
|
|
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
|
2020-03-10 00:56:20 +03:00
|
|
|
use rustc_feature::AttributeTemplate;
|
|
|
|
|
use rustc_parse::validate_attr;
|
|
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) struct Expander;
|
2020-03-10 00:56:20 +03:00
|
|
|
|
|
|
|
|
fn validate_input<'a>(ecx: &mut ExtCtxt<'_>, mi: &'a ast::MetaItem) -> Option<&'a ast::Path> {
|
2023-04-08 20:37:41 +01:00
|
|
|
use errors::CfgAccessibleInvalid::*;
|
2020-03-10 00:56:20 +03:00
|
|
|
match mi.meta_item_list() {
|
|
|
|
|
None => {}
|
2023-04-08 20:37:41 +01:00
|
|
|
Some([]) => {
|
2023-12-18 20:54:03 +11:00
|
|
|
ecx.dcx().emit_err(UnspecifiedPath(mi.span));
|
2023-04-08 20:37:41 +01:00
|
|
|
}
|
|
|
|
|
Some([_, .., l]) => {
|
2023-12-18 20:54:03 +11:00
|
|
|
ecx.dcx().emit_err(MultiplePaths(l.span()));
|
2023-04-08 20:37:41 +01:00
|
|
|
}
|
2020-03-10 00:56:20 +03:00
|
|
|
Some([nmi]) => match nmi.meta_item() {
|
2023-04-08 20:37:41 +01:00
|
|
|
None => {
|
2023-12-18 20:54:03 +11:00
|
|
|
ecx.dcx().emit_err(LiteralPath(nmi.span()));
|
2023-04-08 20:37:41 +01:00
|
|
|
}
|
2020-03-10 00:56:20 +03:00
|
|
|
Some(mi) => {
|
|
|
|
|
if !mi.is_word() {
|
2023-12-18 20:54:03 +11:00
|
|
|
ecx.dcx().emit_err(HasArguments(mi.span));
|
2020-03-10 00:56:20 +03:00
|
|
|
}
|
|
|
|
|
return Some(&mi.path);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MultiItemModifier for Expander {
|
|
|
|
|
fn expand(
|
|
|
|
|
&self,
|
|
|
|
|
ecx: &mut ExtCtxt<'_>,
|
2020-11-14 14:47:14 +03:00
|
|
|
span: Span,
|
2020-03-10 00:56:20 +03:00
|
|
|
meta_item: &ast::MetaItem,
|
|
|
|
|
item: Annotatable,
|
2022-09-20 11:55:07 +00:00
|
|
|
_is_derive_const: bool,
|
2020-03-10 00:56:20 +03:00
|
|
|
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
|
|
|
|
|
let template = AttributeTemplate { list: Some("path"), ..Default::default() };
|
2022-11-24 16:00:57 +11:00
|
|
|
validate_attr::check_builtin_meta_item(
|
2020-07-30 11:27:50 +10:00
|
|
|
&ecx.sess.parse_sess,
|
2023-11-21 20:07:32 +01:00
|
|
|
meta_item,
|
2022-11-24 16:00:57 +11:00
|
|
|
ast::AttrStyle::Outer,
|
2020-07-30 11:27:50 +10:00
|
|
|
sym::cfg_accessible,
|
|
|
|
|
template,
|
|
|
|
|
);
|
2020-03-10 00:56:20 +03:00
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(path) = validate_input(ecx, meta_item) else {
|
|
|
|
|
return ExpandResult::Ready(Vec::new());
|
2020-03-10 00:56:20 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match ecx.resolver.cfg_accessible(ecx.current_expansion.id, path) {
|
|
|
|
|
Ok(true) => ExpandResult::Ready(vec![item]),
|
|
|
|
|
Ok(false) => ExpandResult::Ready(Vec::new()),
|
2020-11-14 14:47:14 +03:00
|
|
|
Err(Indeterminate) if ecx.force_mode => {
|
2023-12-18 20:54:03 +11:00
|
|
|
ecx.dcx().emit_err(errors::CfgAccessibleIndeterminate { span });
|
2020-11-14 14:47:14 +03:00
|
|
|
ExpandResult::Ready(vec![item])
|
|
|
|
|
}
|
|
|
|
|
Err(Indeterminate) => ExpandResult::Retry(item),
|
2020-03-10 00:56:20 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|