From e23d8e27aa74e468a0c3703e3b8fdd896a15d7bf Mon Sep 17 00:00:00 2001 From: Haibo Huang Date: Sun, 26 Jul 2020 23:48:13 -0700 Subject: Upgrade rust/crates/proc-macro-hack to 0.5.18 Change-Id: I4e17bf83d28687a52026191a93e1768afc89f2ff --- .cargo_vcs_info.json | 2 +- .github/FUNDING.yml | 1 + .github/workflows/ci.yml | 4 +- Cargo.toml | 2 +- Cargo.toml.orig | 2 +- METADATA | 6 +- README.md | 16 +++-- src/lib.rs | 183 +++++++++++++++++++++++++++-------------------- 8 files changed, 127 insertions(+), 89 deletions(-) create mode 100644 .github/FUNDING.yml diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index e1a1090..24bc86c 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "35b1fa620a6486ed4b544d4fec6aa8cbdd017bd6" + "sha1": "3da1be8f9519c725b1f425a954b06a1a0a8b25ac" } } diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..7507077 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: dtolnay diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fea412f..ca24eac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,11 +12,13 @@ jobs: strategy: fail-fast: false matrix: - rust: [nightly, beta, stable, 1.31.0] + rust: [nightly, beta, stable, 1.42.0, 1.31.0] steps: - uses: actions/checkout@v2 - uses: dtolnay/rust-toolchain@master with: toolchain: ${{matrix.rust}} + - if: matrix.rust == '1.31.0' + run: echo 'extern crate proc_macro;' >> demo-hack-impl/src/lib.rs - run: cargo run --manifest-path example/Cargo.toml - run: cargo test --all diff --git a/Cargo.toml b/Cargo.toml index 102757c..67fadfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ [package] edition = "2018" name = "proc-macro-hack" -version = "0.5.16" +version = "0.5.18" authors = ["David Tolnay "] description = "Procedural macros in expression position" readme = "README.md" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 1f75add..153d880 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "proc-macro-hack" -version = "0.5.16" +version = "0.5.18" authors = ["David Tolnay "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/METADATA b/METADATA index 932bd30..0fd8eb1 100644 --- a/METADATA +++ b/METADATA @@ -9,11 +9,11 @@ third_party { type: GIT value: "https://github.com/dtolnay/proc-macro-hack" } - version: "0.5.16" + version: "0.5.18" license_type: NOTICE last_upgrade_date { year: 2020 - month: 5 - day: 25 + month: 7 + day: 26 } } diff --git a/README.md b/README.md index d6a6463..df7a2ec 100644 --- a/README.md +++ b/README.md @@ -6,14 +6,20 @@ Procedural macros in expression position [docs.rs](https://docs.rs/proc-macro-hack) [build status](https://github.com/dtolnay/proc-macro-hack/actions?query=branch%3Amaster) -As of Rust 1.30, the language supports user-defined function-like procedural -macros. However these can only be invoked in item position, not in -statements or expressions. +

+Note: As of Rust 1.45 this crate is superseded by native support for +#[proc_macro] in expression position. Only consider using this crate if you care +about supporting compilers between 1.31 and 1.45. +
+ +Since Rust 1.30, the language supports user-defined function-like procedural +macros. However these can only be invoked in item position, not in statements or +expressions. This crate implements an alternative type of procedural macro that can be invoked in statement or expression position. -This approach works with any stable or nightly Rust version 1.31+. +This approach works with any Rust version 1.31+. ## Defining procedural macros @@ -33,8 +39,6 @@ The function signature is the same as for ordinary function-like procedural macros. ```rust -extern crate proc_macro; - use proc_macro::TokenStream; use proc_macro_hack::proc_macro_hack; use quote::quote; diff --git a/src/lib.rs b/src/lib.rs index 16d7823..901a719 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,13 @@ //! //!
//! -//! As of Rust 1.30, the language supports user-defined function-like procedural +//!

+//! Note: As of Rust 1.45 this crate is superseded by native support +//! for #[proc_macro] in expression position. Only consider using this crate if +//! you care about supporting compilers between 1.31 and 1.45. +//!
+//! +//! Since Rust 1.30, the language supports user-defined function-like procedural //! macros. However these can only be invoked in item position, not in //! statements or expressions. //! @@ -31,8 +37,8 @@ //! macros. //! //! ``` -//! extern crate proc_macro; -//! +//! # extern crate proc_macro; +//! # //! use proc_macro::TokenStream; //! use proc_macro_hack::proc_macro_hack; //! use quote::quote; @@ -262,86 +268,111 @@ fn expand_export(export: Export, args: ExportArgs) -> TokenStream { }; let from = export.from; - let rules = export - .macros - .into_iter() - .map(|Macro { name, export_as }| { - let actual_name = actual_proc_macro_name(&name); - let dispatch = dispatch_macro_name(&name); - let call_site = call_site_macro_name(&name); - - let export_dispatch = if args.support_nested { - quote! { - #[doc(hidden)] - #vis use proc_macro_nested::dispatch as #dispatch; - } - } else { - quote!() - }; + let mut actual_names = TokenStream::new(); + let mut export_dispatch = TokenStream::new(); + let mut export_call_site = TokenStream::new(); + let mut macro_rules = TokenStream::new(); + for Macro { name, export_as } in &export.macros { + let actual_name = actual_proc_macro_name(&name); + let dispatch = dispatch_macro_name(&name); + let call_site = call_site_macro_name(&name); + + if !actual_names.is_empty() { + actual_names.extend(quote!(,)); + } + actual_names.extend(quote!(#actual_name)); - let proc_macro_call = if args.support_nested { - let extra_bangs = (0..args.internal_macro_calls) - .map(|_| TokenTree::Punct(Punct::new('!', Spacing::Alone))) - .collect::(); - quote! { - #crate_prefix #dispatch! { ($($proc_macro)*) #extra_bangs } - } - } else { - quote! { - proc_macro_call!() - } - }; + if !export_dispatch.is_empty() { + export_dispatch.extend(quote!(,)); + } + export_dispatch.extend(quote!(dispatch as #dispatch)); - let export_call_site = if args.fake_call_site { - quote! { - #[doc(hidden)] - #vis use proc_macro_hack::fake_call_site as #call_site; - } - } else { - quote!() - }; + if !export_call_site.is_empty() { + export_call_site.extend(quote!(,)); + } + export_call_site.extend(quote!(fake_call_site as #call_site)); - let do_derive = if !args.fake_call_site { - quote! { - #[derive(#crate_prefix #actual_name)] - } - } else if crate_prefix.is_some() { - quote! { - use #crate_prefix #actual_name; - #[#crate_prefix #call_site ($($proc_macro)*)] - #[derive(#actual_name)] - } - } else { - quote! { - #[#call_site ($($proc_macro)*)] - #[derive(#actual_name)] - } - }; + let do_derive = if !args.fake_call_site { + quote! { + #[derive(#crate_prefix #actual_name)] + } + } else if crate_prefix.is_some() { + quote! { + use #crate_prefix #actual_name; + #[#crate_prefix #call_site ($($proc_macro)*)] + #[derive(#actual_name)] + } + } else { + quote! { + #[#call_site ($($proc_macro)*)] + #[derive(#actual_name)] + } + }; + let proc_macro_call = if args.support_nested { + let extra_bangs = (0..args.internal_macro_calls) + .map(|_| TokenTree::Punct(Punct::new('!', Spacing::Alone))) + .collect::(); quote! { - #[doc(hidden)] - #vis use #from::#actual_name; - - #export_dispatch - #export_call_site - - #attrs - #macro_export - macro_rules! #export_as { - ($($proc_macro:tt)*) => {{ - #do_derive - #[allow(dead_code)] - enum ProcMacroHack { - #enum_variant = (stringify! { $($proc_macro)* }, 0).1, - } - #proc_macro_call - }}; - } + #crate_prefix #dispatch! { ($($proc_macro)*) #extra_bangs } + } + } else { + quote! { + proc_macro_call!() } - }) - .collect(); + }; + + macro_rules.extend(quote! { + #attrs + #macro_export + macro_rules! #export_as { + ($($proc_macro:tt)*) => {{ + #do_derive + #[allow(dead_code)] + enum ProcMacroHack { + #enum_variant = (stringify! { $($proc_macro)* }, 0).1, + } + #proc_macro_call + }}; + } + }); + } + + if export.macros.len() != 1 { + export_dispatch = quote!({#export_dispatch}); + export_call_site = quote!({#export_call_site}); + actual_names = quote!({#actual_names}); + } + + let export_dispatch = if args.support_nested { + quote! { + #[doc(hidden)] + #vis use proc_macro_nested::#export_dispatch; + } + } else { + quote!() + }; + + let export_call_site = if args.fake_call_site { + quote! { + #[doc(hidden)] + #vis use proc_macro_hack::#export_call_site; + } + } else { + quote!() + }; + + let expanded = quote! { + #[doc(hidden)] + #vis use #from::#actual_names; + + #export_dispatch + #export_call_site + + #macro_rules + }; - wrap_in_enum_hack(dummy, rules) + wrap_in_enum_hack(dummy, expanded) } fn expand_define(define: Define) -> TokenStream { -- cgit v1.2.3