1 //! Implementation of the `#[cfg_accessible(path)]` attribute macro.
4 use rustc_expand
::base
::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier}
;
5 use rustc_feature
::AttributeTemplate
;
6 use rustc_parse
::validate_attr
;
7 use rustc_span
::symbol
::sym
;
10 pub(crate) struct Expander
;
12 fn validate_input
<'a
>(ecx
: &mut ExtCtxt
<'_
>, mi
: &'a ast
::MetaItem
) -> Option
<&'a ast
::Path
> {
13 match mi
.meta_item_list() {
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"),
21 ecx
.span_err(mi
.span
, "`cfg_accessible` path cannot accept arguments");
23 return Some(&mi
.path
);
30 impl MultiItemModifier
for Expander
{
33 ecx
: &mut ExtCtxt
<'_
>,
35 meta_item
: &ast
::MetaItem
,
37 ) -> ExpandResult
<Vec
<Annotatable
>, Annotatable
> {
38 let template
= AttributeTemplate { list: Some("path"), ..Default::default() }
;
39 let attr
= &ecx
.attribute(meta_item
.clone());
40 validate_attr
::check_builtin_attribute(
47 let Some(path
) = validate_input(ecx
, meta_item
) else {
48 return ExpandResult
::Ready(Vec
::new());
51 match ecx
.resolver
.cfg_accessible(ecx
.current_expansion
.id
, path
) {
52 Ok(true) => ExpandResult
::Ready(vec
![item
]),
53 Ok(false) => ExpandResult
::Ready(Vec
::new()),
54 Err(Indeterminate
) if ecx
.force_mode
=> {
55 ecx
.span_err(span
, "cannot determine whether the path is accessible or not");
56 ExpandResult
::Ready(vec
![item
])
58 Err(Indeterminate
) => ExpandResult
::Retry(item
),