Skip to content

Macros can't expand to 'match' alternatives #12832

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
kmcallister opened this issue Mar 11, 2014 · 11 comments
Closed

Macros can't expand to 'match' alternatives #12832

kmcallister opened this issue Mar 11, 2014 · 11 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@kmcallister
Copy link
Contributor

macro_rules! foo (() => (
    2 => "two"
))

fn main() {
    match 2 {
        foo!(),
        _ => "not two",
    }
}
$ rustc -v
rustc 0.10-pre (6ae5de0 2014-02-19 12:51:48 -0800)
host: x86_64-unknown-linux-gnu
$ rustc foo.rs
foo.rs:7:12: 7:13 error: expected `=>` but found `!`
foo.rs:7         foo!(),
                    ^
@huonw
Copy link
Member

huonw commented Mar 12, 2014

Dupe of #6830.

@huonw huonw closed this as completed Mar 12, 2014
@kmcallister
Copy link
Contributor Author

Not quite a dupe, because 2 => "two" isn't a pattern. I'd like to be able to expand to patterns, or |s of patterns with a guard, or entire match arms. But I think that would be pretty messy to parse.

@huonw
Copy link
Member

huonw commented May 20, 2014

Oh, whoops.

@huonw huonw reopened this May 20, 2014
@kmcallister kmcallister added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Sep 27, 2014
@steveklabnik
Copy link
Member

Traige: updated code sample:

macro_rules! foo (() => (
    2 => "two"
));

fn main() {
    match 2 {
        foo!(),
        _ => "not two",
    }
}

updated error:

hello.rs:7:15: 7:16 error: expected one of `=>`, `if`, or `|`, found `,`
hello.rs:7         foo!(),
                         ^

@lambda-fairy
Copy link
Contributor

Seems related to #26330

@SamWhited
Copy link
Contributor

SamWhited commented Jun 12, 2017

I'm not sure if this is the same thing (but I assume it is since it would be inserting a partial AST node, if I understand what is happening correctly), but this also doesn't work and, should also be fixed as part of this issue (I think):

// Table tests for a function called "baz" on a result type that is being checked
// with a match
macro_rules! test_invalid_baz {
    ( $( $num:ident: [$arg:expr, $err:expr] ),+ ) => {
        $(
            #[test]
            fn $num() {
                //  with $arg as the input, expect Err($err) as the output
                match baz($arg) {
                    Err($err) => {},
                    _ => panic!("Errors did not match"),
                }
            }
        )*
    };
}

test_invalid_split!([test0, "foo", Error::Bar])

@Mark-Simulacrum
Copy link
Member

Macros cannot expand to incomplete AST nodes; this is not something that we can readily change/implement I think without an RFC. As such, I'm going to close.

@comex
Copy link
Contributor

comex commented Jul 30, 2018

I don't think there is any RFC that defines an AST node; the fact that match alternatives aren't AST nodes seems like essentially an implementation detail.

If there are any ambiguities in the desired semantics, that may require an RFC to make a choice, but I can't think of any.

Nor can I think of any downside to allowing this, other than making the compiler marginally more complex.

@Aaron1011
Copy link
Member

Could this issue be re-opened? I think this would be a very useful feature.

At the very least, I think this warrants further discussion.

@steveklabnik
Copy link
Member

That discussion should take place in an RFC, not here.

@lord-ne
Copy link

lord-ne commented Jun 7, 2021

Do procedural macros support expanding to match alternatives like this?

fasterthanlime pushed a commit to fasterthanlime/rust that referenced this issue Jul 26, 2022
…gen, r=Veykril

fix: don't replace default members' body

cc rust-lang#12779, rust-lang#12821
addresses rust-lang/rust-analyzer#12821 (comment)

`gen_trait_fn_body()` only attempts to implement required trait member functions, so we shouldn't call it for `Implement default members` assist.

This patch also documents the precondition of `gen_trait_fn_body()` and inserts `debug_assert!`, but I'm not entirely sure if the assertions are appropriate.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

9 participants