Compare commits

..

1 commit

Author SHA1 Message Date
Jacob Lifshay b7ec623bfa
WIP implementing handling #[cfg] and #[cfg_attr] in proc macro inputs
Some checks failed
/ deps (pull_request) Successful in 18s
/ test (pull_request) Failing after 1m21s
2024-12-27 16:05:34 -08:00

View file

@ -271,27 +271,60 @@ impl<T: Process<P>, P: Phase> Process<P> for Box<T> {
}
}
trait ProcessVecElement {
const REMOVE_ELEMENTS: bool;
}
impl ProcessVecElement for syn::Stmt {
const REMOVE_ELEMENTS: bool = true;
}
impl ProcessVecElement for syn::ForeignItem {
const REMOVE_ELEMENTS: bool = true;
}
impl ProcessVecElement for syn::ImplItem {
const REMOVE_ELEMENTS: bool = true;
}
impl ProcessVecElement for syn::Item {
const REMOVE_ELEMENTS: bool = true;
}
impl ProcessVecElement for syn::TraitItem {
const REMOVE_ELEMENTS: bool = true;
}
impl<T: Process<P> + ProcessVecElement, P: Phase> Process<P> for Vec<T> {
fn process(self, state: &mut State<P>) -> Option<Output<Self, P>> {
let mut output = Output::new(Vec::new());
for value in self {
if let Some(value) = value.process(state) {
output = (output, value).call(|(mut output, value)| {
output.push(value);
output
});
} else if !T::REMOVE_ELEMENTS {
return None;
}
}
Some(output)
}
}
trait ProcessOption {
/// if a configured-off value causes this value to be `None` instead of propagating the configuring-off
const REMOVE_VALUE: bool;
}
impl ProcessOption for Token![<] {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for Token![>] {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for Token![=] {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for syn::WhereClause {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for syn::ImplRestriction {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for (Token![=], syn::Expr) {
const REMOVE_VALUE: bool = true;
}
@ -300,34 +333,22 @@ impl ProcessOption for (Token![as], Ident) {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for Token![unsafe] {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for Token![default] {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for Token![const] {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for Token![async] {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for (Option<Token![!]>, syn::Path, Token![for]) {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for syn::Abi {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for syn::Variadic {
const REMOVE_VALUE: bool = true;
}
impl ProcessOption for (Box<syn::Pat>, Token![:]) {
const REMOVE_VALUE: bool = false;
}
impl ProcessOption for (syn::token::Brace, Vec<syn::Item>) {
const REMOVE_VALUE: bool = false;
}
impl<T: Process<P> + ProcessOption, P: Phase> Process<P> for Option<T> {
fn process(self, state: &mut State<P>) -> Option<Output<Self, P>> {
if let Some(this) = self {
@ -403,19 +424,8 @@ impl ProcessPunctuatedElement for syn::FnArg {
const REMOVE_ELEMENTS: bool = true;
}
impl<P: Phase> Process<P> for Vec<syn::Stmt> {
fn process(self, state: &mut State<P>) -> Option<Output<Self, P>> {
let mut output = Output::new(Vec::new());
for value in self {
if let Some(value) = value.process(state) {
output = (output, value).call(|(mut output, value)| {
output.push(value);
output
});
}
}
Some(output)
}
impl ProcessPunctuatedElement for syn::TypeParamBound {
const REMOVE_ELEMENTS: bool = true;
}
impl<T: Process<P>, U: Process<P>, P: Phase> Process<P> for (T, U) {
@ -444,12 +454,17 @@ macro_rules! process_no_op {
Some(Output::new(self))
}
}
impl ProcessOption for $ty {
const REMOVE_VALUE: bool = false;
}
};
}
process_no_op!(syn::Visibility);
process_no_op!(syn::Lifetime);
process_no_op!(syn::Abi);
process_no_op!(syn::StaticMutability);
process_no_op!(Token![as]);
process_no_op!(Token![async]);
process_no_op!(Token![auto]);
@ -495,14 +510,53 @@ process_no_op!(Token![use]);
process_no_op!(Token![where]);
process_no_op!(Token![while]);
process_no_op!(Token![yield]);
process_no_op!(Token![&]);
process_no_op!(Token![&&]);
process_no_op!(Token![&=]);
process_no_op!(Token![@]);
process_no_op!(Token![^]);
process_no_op!(Token![^=]);
process_no_op!(Token![:]);
process_no_op!(Token![::]);
process_no_op!(Token![;]);
process_no_op!(Token![=]);
process_no_op!(Token![?]);
process_no_op!(Token![,]);
process_no_op!(Token![$]);
process_no_op!(Token![.]);
process_no_op!(Token![..]);
process_no_op!(Token![<]);
process_no_op!(Token![...]);
process_no_op!(Token![..=]);
process_no_op!(Token![=]);
process_no_op!(Token![==]);
process_no_op!(Token![=>]);
process_no_op!(Token![>=]);
process_no_op!(Token![>]);
process_no_op!(Token![<-]);
process_no_op!(Token![<=]);
process_no_op!(Token![<]);
process_no_op!(Token![-]);
process_no_op!(Token![-=]);
process_no_op!(Token![!=]);
process_no_op!(Token![!]);
process_no_op!(Token![|]);
process_no_op!(Token![|=]);
process_no_op!(Token![||]);
process_no_op!(Token![::]);
process_no_op!(Token![%]);
process_no_op!(Token![%=]);
process_no_op!(Token![+]);
process_no_op!(Token![+=]);
process_no_op!(Token![#]);
process_no_op!(Token![?]);
process_no_op!(Token![->]);
process_no_op!(Token![;]);
process_no_op!(Token![<<]);
process_no_op!(Token![<<=]);
process_no_op!(Token![>>]);
process_no_op!(Token![>>=]);
process_no_op!(Token![/]);
process_no_op!(Token![/=]);
process_no_op!(Token![*]);
process_no_op!(Token![*=]);
process_no_op!(Token![~]);
process_no_op!(Token![_]);
process_no_op!(syn::token::Brace);
process_no_op!(syn::token::Bracket);
process_no_op!(syn::token::Paren);
@ -1489,6 +1543,71 @@ process_enum! {
}
}
process_enum! {
syn::Pat {
Const,
Ident,
Lit,
Macro,
Or,
Paren,
Path,
Range,
Reference,
Rest,
Slice,
Struct,
Tuple,
TupleStruct,
Type,
Wild,
#[no_op]
_,
}
}
process_enum! {
syn::ForeignItem {
Fn,
Static,
Type,
Macro,
#[no_op]
_,
}
}
process_enum! {
syn::ImplItem {
Const,
Fn,
Type,
Macro,
#[no_op]
_,
}
}
process_enum! {
syn::TypeParamBound {
Trait,
Lifetime,
#[no_op]
_,
}
}
process_enum! {
syn::TraitItem {
Const,
Fn,
Type,
Macro,
#[no_op]
_,
}
}
impl<P: Phase> Process<P> for syn::ReturnType {
fn process(self, state: &mut State<P>) -> Option<Output<Self, P>> {
match self {
@ -1554,6 +1673,12 @@ impl<P: Phase> Process<P> for TopItem {
}
}
impl<P: Phase> Process<P> for syn::ImplRestriction {
fn process(self, _state: &mut State<P>) -> Option<Output<Self, P>> {
todo!("ImplRestriction is an uninhabited type as of writing, blocked on syn implementing impl restrictions")
}
}
pub(crate) fn process_cfgs(item: syn::Item, cfgs: Cfgs<bool>) -> syn::Result<Option<syn::Item>> {
let mut state = State::<ProcessCfgsPhase> {
cfgs,