aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaibo Huang <hhb@google.com>2020-07-27 20:19:38 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2020-07-27 20:19:38 +0000
commitc4babc7587bcee6529ab8317ee7706aa19dd9348 (patch)
treedd36ed2ad7763fe9ee9537a2c243e99513ae2aea
parent55547b25794314831cad6e418e2bd7da281d296d (diff)
parent11521d5d5d0299385c967443aa49760987b806ac (diff)
downloadproc-macro-hack-c4babc7587bcee6529ab8317ee7706aa19dd9348.tar.gz
Upgrade rust/crates/proc-macro-hack to 0.5.18 am: e23d8e27aa am: 11521d5d5d
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/proc-macro-hack/+/1372957 Change-Id: I2225ad0858e9b8005577c49541b2d4425ac47cb0
-rw-r--r--.cargo_vcs_info.json2
-rw-r--r--.github/FUNDING.yml1
-rw-r--r--.github/workflows/ci.yml4
-rw-r--r--Cargo.toml2
-rw-r--r--Cargo.toml.orig2
-rw-r--r--METADATA6
-rw-r--r--README.md16
-rw-r--r--src/lib.rs183
8 files changed, 127 insertions, 89 deletions
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 <dtolnay@gmail.com>"]
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 <dtolnay@gmail.com>"]
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
[<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-proc--macro--hack-66c2a5?style=for-the-badge&labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/proc-macro-hack)
[<img alt="build status" src="https://img.shields.io/github/workflow/status/dtolnay/proc-macro-hack/CI/master?style=for-the-badge" height="20">](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.
+<table><tr><td><hr>
+<b>Note:</b> <i>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.</i>
+<hr></td></tr></table>
+
+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 @@
//!
//! <br>
//!
-//! As of Rust 1.30, the language supports user-defined function-like procedural
+//! <table><tr><td><hr>
+//! <b>Note:</b> <i>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.</i>
+//! <hr></td></tr></table>
+//!
+//! 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::<TokenStream>();
- 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::<TokenStream>();
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 {