From cd1c306c5f951a10d4c01e5315cd7c5285c4514b Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Thu, 28 Jul 2022 20:54:09 +0200 Subject: [PATCH] Add experimental cfg for using the proc-macro-expand API https://github.com/rust-lang/rust/issues/90765 --- .github/workflows/nightly.yml | 15 +++++++++++++++ core/src/lib.rs | 23 +++++++++++++++++++++++ macros/src/lib.rs | 17 +++++++++++++++++ tests/tests.rs | 8 ++++++++ 4 files changed, 63 insertions(+) diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 183c7b8..bf80f4b 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -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 diff --git a/core/src/lib.rs b/core/src/lib.rs index 92d192f..03ff8ce 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -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)*)?) }; @@ -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)*)?) + }; +} diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 54d0e04..e91e609 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -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; @@ -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::()?; + 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; diff --git a/tests/tests.rs b/tests/tests.rs index 4df3148..4c90424 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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!()); + } }