forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfg_accessible.rs
More file actions
72 lines (65 loc) · 2.28 KB
/
Copy pathcfg_accessible.rs
File metadata and controls
72 lines (65 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Implementation of the `#[cfg_accessible(path)]` attribute macro.
use rustc_ast as ast;
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, Indeterminate, MultiItemModifier};
use rustc_feature::AttributeTemplate;
use rustc_parse::validate_attr;
use rustc_span::symbol::sym;
use rustc_span::Span;
use crate::errors::{
MultiplePathsSpecified, NondeterministicAccess, NotSpecified, UnacceptedArguments,
UnallowedLiteralPath,
};
pub(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.emit_err(NotSpecified { span: mi.span });
}
Some([_, .., l]) => {
ecx.emit_err(MultiplePathsSpecified { span: l.span() });
}
Some([nmi]) => match nmi.meta_item() {
None => {
ecx.emit_err(UnallowedLiteralPath { span: nmi.span() });
}
Some(mi) => {
if !mi.is_word() {
ecx.emit_err(UnacceptedArguments { span: mi.span });
}
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.sess.parse_sess,
attr,
sym::cfg_accessible,
template,
);
let Some(path) = validate_input(ecx, meta_item) else {
return ExpandResult::Ready(Vec::new());
};
match ecx.resolver.cfg_accessible(ecx.current_expansion.id, path) {
Ok(true) => ExpandResult::Ready(vec![item]),
Ok(false) => ExpandResult::Ready(Vec::new()),
Err(Indeterminate) if ecx.force_mode => {
ecx.emit_err(NondeterministicAccess { span });
ExpandResult::Ready(vec![item])
}
Err(Indeterminate) => ExpandResult::Retry(item),
}
}
}