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