]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_builtin_macros/src/cfg_accessible.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / compiler / rustc_builtin_macros / src / cfg_accessible.rs
CommitLineData
ba9703b0
XL
1//! Implementation of the `#[cfg_accessible(path)]` attribute macro.
2
3dfed10e 3use rustc_ast as ast;
fc512014 4use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
ba9703b0
XL
5use rustc_feature::AttributeTemplate;
6use rustc_parse::validate_attr;
7use rustc_span::symbol::sym;
8use rustc_span::Span;
9
923072b8 10pub(crate) struct Expander;
ba9703b0
XL
11
12fn validate_input<'a>(ecx: &mut ExtCtxt<'_>, mi: &'a ast::MetaItem) -> Option<&'a ast::Path> {
13 match mi.meta_item_list() {
14 None => {}
15 Some([]) => ecx.span_err(mi.span, "`cfg_accessible` path is not specified"),
16 Some([_, .., l]) => ecx.span_err(l.span(), "multiple `cfg_accessible` paths are specified"),
17 Some([nmi]) => match nmi.meta_item() {
18 None => ecx.span_err(nmi.span(), "`cfg_accessible` path cannot be a literal"),
19 Some(mi) => {
20 if !mi.is_word() {
21 ecx.span_err(mi.span, "`cfg_accessible` path cannot accept arguments");
22 }
23 return Some(&mi.path);
24 }
25 },
26 }
27 None
28}
29
30impl MultiItemModifier for Expander {
31 fn expand(
32 &self,
33 ecx: &mut ExtCtxt<'_>,
fc512014 34 span: Span,
ba9703b0
XL
35 meta_item: &ast::MetaItem,
36 item: Annotatable,
487cf647 37 _is_derive_const: bool,
ba9703b0
XL
38 ) -> ExpandResult<Vec<Annotatable>, Annotatable> {
39 let template = AttributeTemplate { list: Some("path"), ..Default::default() };
487cf647 40 validate_attr::check_builtin_meta_item(
3dfed10e 41 &ecx.sess.parse_sess,
487cf647
FG
42 &meta_item,
43 ast::AttrStyle::Outer,
3dfed10e
XL
44 sym::cfg_accessible,
45 template,
46 );
ba9703b0 47
5e7ed085
FG
48 let Some(path) = validate_input(ecx, meta_item) else {
49 return ExpandResult::Ready(Vec::new());
ba9703b0
XL
50 };
51
ba9703b0
XL
52 match ecx.resolver.cfg_accessible(ecx.current_expansion.id, path) {
53 Ok(true) => ExpandResult::Ready(vec![item]),
54 Ok(false) => ExpandResult::Ready(Vec::new()),
fc512014
XL
55 Err(Indeterminate) if ecx.force_mode => {
56 ecx.span_err(span, "cannot determine whether the path is accessible or not");
57 ExpandResult::Ready(vec![item])
58 }
59 Err(Indeterminate) => ExpandResult::Retry(item),
ba9703b0
XL
60 }
61 }
62}