Skip to content

Commit

Permalink
Add experimental cfg for using the proc-macro-expand API
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo157 committed Jul 28, 2022
1 parent 33f8a1c commit cd1c306
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ jobs:
command: test
args: --workspace --locked --all-features

name: cargo +nightly test --cfg stylish_proc_macro_expand
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: hecrj/setup-rust-action@v1
with:
rust-version: nightly
- uses: actions-rs/cargo@v1
env:
RUSTFLAGS: --cfg stylish_proc_macro_expand
with:
toolchain: nightly
command: test
args: --workspace --locked --all-features

fmt:
name: cargo +nightly fmt --check
runs-on: ubuntu-latest
Expand Down
23 changes: 23 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ pub mod 𓀄 {
/// ```
#[macro_export]
macro_rules! format_args {
($fmt:literal $(, $($arg:tt)*)?) => {
$crate::_format_args!($fmt $(, $($arg)*)?)
};
($fmt:expr $(, $($arg:tt)*)?) => {
$crate::_format_args!($fmt $(, $($arg)*)?)
};
}

#[cfg(feature = "macros")]
#[cfg(not(stylish_proc_macro_expand))]
#[doc(hidden)]
#[macro_export]
macro_rules! _format_args {
($fmt:literal $(, $($arg:tt)*)?) => {
$crate::𓀄::format_args!(crate=$crate, $fmt $(, $($arg)*)?)
};
Expand All @@ -105,3 +118,13 @@ macro_rules! format_args {
})
};
}

#[cfg(feature = "macros")]
#[cfg(stylish_proc_macro_expand)]
#[doc(hidden)]
#[macro_export]
macro_rules! _format_args {
($fmt:expr $(, $($arg:tt)*)?) => {
$crate::𓀄::format_args!(crate=$crate, $fmt $(, $($arg)*)?)
};
}
17 changes: 17 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![warn(unused_extern_crates)]
#![warn(unused_import_braces)]
#![warn(variant_size_differences)]
#![cfg_attr(stylish_proc_macro_expand, feature(proc_macro_expand))]

use std::collections::HashMap;

Expand Down Expand Up @@ -48,7 +49,23 @@ impl syn::parse::Parse for ArgsInput {
} else {
None
};
#[cfg(not(stylish_proc_macro_expand))]
let format = input.parse()?;
#[cfg(stylish_proc_macro_expand)]
let format = {
use syn::spanned::Spanned;
let expr = input.parse::<Expr>()?;
let span = expr.span();
let tokens = proc_macro::TokenStream::from(expr.into_token_stream());
let expanded = tokens.expand_expr().map_err(|e| {
syn::parse::Error::new(span, format!("failed to expand format string: {e}"))
})?;
#[cfg(stylish_proc_macro_expand_debug)]
if expanded.to_string() != tokens.to_string() {
eprintln!("{tokens} => {expanded}");
}
syn::parse(expanded)?
};
let mut positional_args = Vec::new();
let mut named_args = Vec::new();
let mut onto_named = false;
Expand Down
8 changes: 8 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,12 @@ mod tests {
r#""a"b"#
);
}

#[cfg(stylish_proc_macro_expand)]
#[test]
fn extended_builtin_macros() {
// `file!()` is not supported by `with_builtin_macros`, but it works with
// `proc_macro_expand`
assert_eq!(stylish::plain::format!(file!()), file!());
}
}

0 comments on commit cd1c306

Please sign in to comment.