From 6d8c00b8f2cae1fc9d4afcb8b31f58ac64ec760b Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 21 May 2014 13:09:29 -0700 Subject: [PATCH 1/3] RFC: pattern expansion in macros --- active/0000-pattern-macros.md | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 active/0000-pattern-macros.md diff --git a/active/0000-pattern-macros.md b/active/0000-pattern-macros.md new file mode 100644 index 00000000000..b0ab6409e86 --- /dev/null +++ b/active/0000-pattern-macros.md @@ -0,0 +1,54 @@ +- Start Date: 2014-05-21 +- RFC PR #: +- Rust Issue #: + +# Summary + +Allow macro expansion in patterns, i.e. + +~~~ .rs +match x { + my_macro!() => 1, + _ => 2, +} +~~~ + +# Motivation + +This is consistent with allowing macros in expressions etc. It's also a year-old [open issue](https://github.com/mozilla/rust/issues/6830). + +I have [implemented](https://github.com/mozilla/rust/pull/14298) this feature already (with some bugs) and I'm [using it](https://github.com/kmcallister/html5/blob/937684f107090741c8e87135efc6e5476489857b/src/tree_builder/mod.rs#L111-L117) to [condense](https://github.com/kmcallister/html5/blob/937684f107090741c8e87135efc6e5476489857b/src/tree_builder/mod.rs#L261-L269) some ubiquitous patterns in the [HTML parser](https://github.com/kmcallister/html5) I'm writing. This makes the code more concise and easier to cross-reference with the spec. + +# Drawbacks / alternatives + +A macro invocation in this position: + +~~~ .rs +match x { + my_macro!() +~~~ + +could potentially expand to any of three different syntactic elements: + +* A pattern, i.e. `Foo(x)` +* The left side of a `match` arm, i.e. `Foo(x) | Bar(x) if x > 5` +* An entire `match` arm, i.e. `Foo(x) | Bar(x) if x > 5 => 1` + +This RFC proposes only the first of these, but the others would be more useful in some cases. Supporting multiple of the above would be significantly more complex. + +Another alternative is to use a macro for the entire `match` expression, e.g. + +~~~ .rs +my_match!(x { + my_new_syntax => 1, + _ => 2, +}) +~~~ + +This doesn't involve any language changes, but requires writing a complicated procedural macro. (My sustained attempts to do things like this with MBE macros have all failed.) Perhaps I could alleviate some of the pain with a library for writing `match`-like macros, or better use of the existing parser in `libsyntax`. + +The `my_match!` approach is also not very composable. + +# Unresolved questions + +I need to fix the ICE described in [the pull request](https://github.com/mozilla/rust/pull/14298). I don't expect this to have any design impact, but I can't be sure. From 374d6909b96977085121ff9e34bc85a1a467d016 Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 21 May 2014 15:12:52 -0700 Subject: [PATCH 2/3] Fixed an implementation bug --- active/0000-pattern-macros.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/active/0000-pattern-macros.md b/active/0000-pattern-macros.md index b0ab6409e86..88210095c0d 100644 --- a/active/0000-pattern-macros.md +++ b/active/0000-pattern-macros.md @@ -17,7 +17,7 @@ match x { This is consistent with allowing macros in expressions etc. It's also a year-old [open issue](https://github.com/mozilla/rust/issues/6830). -I have [implemented](https://github.com/mozilla/rust/pull/14298) this feature already (with some bugs) and I'm [using it](https://github.com/kmcallister/html5/blob/937684f107090741c8e87135efc6e5476489857b/src/tree_builder/mod.rs#L111-L117) to [condense](https://github.com/kmcallister/html5/blob/937684f107090741c8e87135efc6e5476489857b/src/tree_builder/mod.rs#L261-L269) some ubiquitous patterns in the [HTML parser](https://github.com/kmcallister/html5) I'm writing. This makes the code more concise and easier to cross-reference with the spec. +I have [implemented](https://github.com/mozilla/rust/pull/14298) this feature already and I'm [using it](https://github.com/kmcallister/html5/blob/937684f107090741c8e87135efc6e5476489857b/src/tree_builder/mod.rs#L111-L117) to [condense](https://github.com/kmcallister/html5/blob/937684f107090741c8e87135efc6e5476489857b/src/tree_builder/mod.rs#L261-L269) some ubiquitous patterns in the [HTML parser](https://github.com/kmcallister/html5) I'm writing. This makes the code more concise and easier to cross-reference with the spec. # Drawbacks / alternatives @@ -51,4 +51,4 @@ The `my_match!` approach is also not very composable. # Unresolved questions -I need to fix the ICE described in [the pull request](https://github.com/mozilla/rust/pull/14298). I don't expect this to have any design impact, but I can't be sure. +None, as far as I know. From 59bdf1bd683e2872305a895d60406417c61f182c Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Wed, 21 May 2014 15:21:22 -0700 Subject: [PATCH 3/3] Note rustdoc limitation --- active/0000-pattern-macros.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/active/0000-pattern-macros.md b/active/0000-pattern-macros.md index 88210095c0d..a501a44cc9b 100644 --- a/active/0000-pattern-macros.md +++ b/active/0000-pattern-macros.md @@ -49,6 +49,8 @@ This doesn't involve any language changes, but requires writing a complicated pr The `my_match!` approach is also not very composable. +Another small drawback: `rustdoc` [can't document](https://github.com/kmcallister/rust/blob/af65e3e9824087a472de3fea3c7cb1efcec4550b/src/librustdoc/clean.rs#L1287-L1291) the name of a function argument which is produced by a pattern macro. + # Unresolved questions None, as far as I know.