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
|
|
|
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:306:28
|
306 | fn err_duplicate_option(p: &mut Parser<'_>, symbol: Symbol, span: Span) {
| ^^^^^^^^^^^^^^^ help: consider changing to: `&Parser<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:318:8
|
318 | p: &mut Parser<'a>,
| ^^^^^^^^^^^^^^^ help: consider changing to: `&Parser<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\assert.rs:114:25
|
114 | fn parse_assert<'a>(cx: &mut ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult<'a, Assert> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:32:10
|
32 | ecx: &mut ExtCtxt<'a>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\test.rs:99:9
|
99 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\source_util.rs:237:9
|
237 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:809:10
|
809 | ecx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:737:10
|
737 | ecx: &mut ExtCtxt<'a>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:68:24
|
68 | fn parse_args<'a>(ecx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:607:10
|
607 | ecx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\edition_panic.rs:43:9
|
43 | cx: &'cx mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\concat_bytes.rs:11:9
|
11 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\cfg.rs:38:22
|
38 | fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\cfg_accessible.rs:13:28
|
13 | fn validate_input<'a>(ecx: &mut ExtCtxt<'_>, mi: &'a ast::MetaItem) -> Option<&'a ast::Path> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 09:04:00 +00:00
|
|
|
fn validate_input<'a>(ecx: &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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|