Skip to content

Declarative macros don't work in patterns. #44127

Closed
@Lymia

Description

@Lymia

It looks like something relating to hygiene in declarative macros makes them fail when used in patterns. This code looks like it should compile:

#![feature(decl_macro)]

pub struct Foo {
    bar: u32,
}
pub macro pattern($a:pat) {
    Foo { bar: $a }
}

fn main() {
    match (Foo { bar: 3 }) {
        pattern!(3) => println!("Test OK"),
        _ => unreachable!(),
    }
}

But instead, it does this:

   Compiling playground v0.0.1 (file:///playground)
error[E0026]: struct `Foo` does not have a field named `bar`
  --> src/main.rs:7:11
   |
7  |       Foo { bar: $a }
   |  ___________^
8  | | }
9  | |
10 | | fn main() {
11 | |     match (Foo { bar: 3 }) {
12 | |         pattern!(3) => println!("Test OK"),
   | |         ---------^-
   | |_________|________|
   |           |        struct `Foo` does not have field `bar`
   |           in this macro invocation

error[E0027]: pattern does not mention field `bar`
  --> src/main.rs:7:5
   |
7  |     Foo { bar: $a }
   |     ^^^^^^^^^^^^^^^ missing field `bar`
...
12 |         pattern!(3) => println!("Test OK"),
   |         ----------- in this macro invocation

error: aborting due to 2 previous errors

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

Labels

A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions