-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builtin_macros: Add attribute macro
#[cfg_accessible(path)]
- Loading branch information
1 parent
552a887
commit 2e65289
Showing
17 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Implementation of the `#[cfg_accessible(path)]` attribute macro. | ||
use rustc_ast::ast; | ||
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier}; | ||
use rustc_feature::AttributeTemplate; | ||
use rustc_parse::validate_attr; | ||
use rustc_span::symbol::sym; | ||
use rustc_span::Span; | ||
|
||
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.span_err(mi.span, "`cfg_accessible` path is not specified"), | ||
Some([_, .., l]) => ecx.span_err(l.span(), "multiple `cfg_accessible` paths are specified"), | ||
Some([nmi]) => match nmi.meta_item() { | ||
None => ecx.span_err(nmi.span(), "`cfg_accessible` path cannot be a literal"), | ||
Some(mi) => { | ||
if !mi.is_word() { | ||
ecx.span_err(mi.span, "`cfg_accessible` path cannot accept arguments"); | ||
} | ||
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.parse_sess, attr, sym::cfg_accessible, template); | ||
|
||
let path = match validate_input(ecx, meta_item) { | ||
Some(path) => path, | ||
None => return ExpandResult::Ready(Vec::new()), | ||
}; | ||
|
||
let failure_msg = "cannot determine whether the path is accessible or not"; | ||
match ecx.resolver.cfg_accessible(ecx.current_expansion.id, path) { | ||
Ok(true) => ExpandResult::Ready(vec![item]), | ||
Ok(false) => ExpandResult::Ready(Vec::new()), | ||
Err(_) => ExpandResult::Retry(item, failure_msg.into()), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -181,6 +181,7 @@ symbols! { | |
caller_location, | ||
cdylib, | ||
cfg, | ||
cfg_accessible, | ||
cfg_attr, | ||
cfg_attr_multi, | ||
cfg_doctest, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/test/ui/conditional-compilation/cfg_accessible-input-validation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#![feature(cfg_accessible)] | ||
|
||
#[cfg_accessible] //~ ERROR malformed `cfg_accessible` attribute input | ||
struct S1; | ||
|
||
#[cfg_accessible = "value"] //~ ERROR malformed `cfg_accessible` attribute input | ||
struct S2; | ||
|
||
#[cfg_accessible()] //~ ERROR `cfg_accessible` path is not specified | ||
struct S3; | ||
|
||
#[cfg_accessible(std, core)] //~ ERROR multiple `cfg_accessible` paths are specified | ||
struct S4; | ||
|
||
#[cfg_accessible("std")] //~ ERROR `cfg_accessible` path cannot be a literal | ||
struct S5; | ||
|
||
#[cfg_accessible(std = "value")] //~ ERROR `cfg_accessible` path cannot accept arguments | ||
struct S6; | ||
|
||
#[cfg_accessible(std(value))] //~ ERROR `cfg_accessible` path cannot accept arguments | ||
struct S7; | ||
|
||
fn main() {} |
44 changes: 44 additions & 0 deletions
44
src/test/ui/conditional-compilation/cfg_accessible-input-validation.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
error: malformed `cfg_accessible` attribute input | ||
--> $DIR/cfg_accessible-input-validation.rs:3:1 | ||
| | ||
LL | #[cfg_accessible] | ||
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[cfg_accessible(path)]` | ||
|
||
error: malformed `cfg_accessible` attribute input | ||
--> $DIR/cfg_accessible-input-validation.rs:6:1 | ||
| | ||
LL | #[cfg_accessible = "value"] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[cfg_accessible(path)]` | ||
|
||
error: `cfg_accessible` path is not specified | ||
--> $DIR/cfg_accessible-input-validation.rs:9:1 | ||
| | ||
LL | #[cfg_accessible()] | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: multiple `cfg_accessible` paths are specified | ||
--> $DIR/cfg_accessible-input-validation.rs:12:23 | ||
| | ||
LL | #[cfg_accessible(std, core)] | ||
| ^^^^ | ||
|
||
error: `cfg_accessible` path cannot be a literal | ||
--> $DIR/cfg_accessible-input-validation.rs:15:18 | ||
| | ||
LL | #[cfg_accessible("std")] | ||
| ^^^^^ | ||
|
||
error: `cfg_accessible` path cannot accept arguments | ||
--> $DIR/cfg_accessible-input-validation.rs:18:18 | ||
| | ||
LL | #[cfg_accessible(std = "value")] | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: `cfg_accessible` path cannot accept arguments | ||
--> $DIR/cfg_accessible-input-validation.rs:21:18 | ||
| | ||
LL | #[cfg_accessible(std(value))] | ||
| ^^^^^^^^^^ | ||
|
||
error: aborting due to 7 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![feature(cfg_accessible)] | ||
|
||
#[cfg_accessible(Z)] //~ ERROR cannot determine whether the path is accessible or not | ||
struct S; | ||
|
||
#[cfg_accessible(S)] //~ ERROR cannot determine whether the path is accessible or not | ||
struct Z; | ||
|
||
fn main() {} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/conditional-compilation/cfg_accessible-stuck.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error: cannot determine whether the path is accessible or not | ||
--> $DIR/cfg_accessible-stuck.rs:6:1 | ||
| | ||
LL | #[cfg_accessible(S)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: cannot determine whether the path is accessible or not | ||
--> $DIR/cfg_accessible-stuck.rs:3:1 | ||
| | ||
LL | #[cfg_accessible(Z)] | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
2 changes: 2 additions & 0 deletions
2
src/test/ui/conditional-compilation/cfg_accessible-unstable.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg_accessible(std)] //~ ERROR use of unstable library feature 'cfg_accessible' | ||
fn main() {} |
12 changes: 12 additions & 0 deletions
12
src/test/ui/conditional-compilation/cfg_accessible-unstable.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0658]: use of unstable library feature 'cfg_accessible': `cfg_accessible` is not fully implemented | ||
--> $DIR/cfg_accessible-unstable.rs:1:3 | ||
| | ||
LL | #[cfg_accessible(std)] | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= note: see issue #64797 <https://github.com/rust-lang/rust/issues/64797> for more information | ||
= help: add `#![feature(cfg_accessible)]` to the crate attributes to enable | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#![feature(cfg_accessible)] | ||
|
||
mod m { | ||
pub struct ExistingPublic; | ||
struct ExistingPrivate; | ||
} | ||
|
||
#[cfg_accessible(m::ExistingPublic)] | ||
struct ExistingPublic; | ||
|
||
// FIXME: Not implemented yet. | ||
#[cfg_accessible(m::ExistingPrivate)] //~ ERROR not sure whether the path is accessible or not | ||
struct ExistingPrivate; | ||
|
||
// FIXME: Not implemented yet. | ||
#[cfg_accessible(m::NonExistent)] //~ ERROR not sure whether the path is accessible or not | ||
struct ExistingPrivate; | ||
|
||
#[cfg_accessible(n::AccessibleExpanded)] // OK, `cfg_accessible` can wait and retry. | ||
struct AccessibleExpanded; | ||
|
||
macro_rules! generate_accessible_expanded { | ||
() => { | ||
mod n { | ||
pub struct AccessibleExpanded; | ||
} | ||
}; | ||
} | ||
|
||
generate_accessible_expanded!(); | ||
|
||
struct S { | ||
field: u8, | ||
} | ||
|
||
// FIXME: Not implemented yet. | ||
#[cfg_accessible(S::field)] //~ ERROR not sure whether the path is accessible or not | ||
struct Field; | ||
|
||
fn main() { | ||
ExistingPublic; | ||
AccessibleExpanded; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
error: not sure whether the path is accessible or not | ||
--> $DIR/cfg_accessible.rs:12:18 | ||
| | ||
LL | #[cfg_accessible(m::ExistingPrivate)] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: `cfg_accessible` is not fully implemented | ||
--> $DIR/cfg_accessible.rs:12:18 | ||
| | ||
LL | #[cfg_accessible(m::ExistingPrivate)] | ||
| ^^^^^^^^^^^^^^^^^^ | ||
|
||
error: not sure whether the path is accessible or not | ||
--> $DIR/cfg_accessible.rs:16:18 | ||
| | ||
LL | #[cfg_accessible(m::NonExistent)] | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
note: `cfg_accessible` is not fully implemented | ||
--> $DIR/cfg_accessible.rs:16:18 | ||
| | ||
LL | #[cfg_accessible(m::NonExistent)] | ||
| ^^^^^^^^^^^^^^ | ||
|
||
error: not sure whether the path is accessible or not | ||
--> $DIR/cfg_accessible.rs:37:18 | ||
| | ||
LL | #[cfg_accessible(S::field)] | ||
| ^^^^^^^^ | ||
| | ||
note: `cfg_accessible` is not fully implemented | ||
--> $DIR/cfg_accessible.rs:37:18 | ||
| | ||
LL | #[cfg_accessible(S::field)] | ||
| ^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|