rust-lang / rust-lang/rust

[DESIGN BUG] declarative macros lack of neat way to simulate lookahead within rust grammer syntax `const X: Y`

Open
#130,928 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-discussion T-lang
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

macro match and echo non const X: Y pattern is fine.

    macro_rules! echo1 {
        (pub type $ident:ident<$($gi:ident),*> = $($tt:tt)*) => {
            pub type $ident<$($gi),*> = $($tt)*;
        };
    }

    echo1!(pub type Foo1<T, N> = (T, N));

But within current rust desgin, to let declarative macro match lookahead within rust grammer syntax const X: Y

  • or input rust code, and do lookahead simulation

  • or input lookahead free dsl, and output lookahead code

    macro_rules! echo2 {
        (@derive_foo pub type $ident:ident<$($gi:ident $(lookahead_qualifier=$gq:tt)? $(: $gt:ty)?),*> = $($tt:tt)*) => {
            pub type $ident<$($($gq)? $gi $(: $gt)?),*> = $($tt)*;
        };

        (@derive_bar pub type $ident:ident<$($gi:ident $(lookahead_qualifier=$gq:tt)? $(: $gt:ty)?),*> = $($tt:tt)*) => {
            pub type $ident<$($($gq)? $gi $(: $gt)?),*> = $($tt)*;
        };

        (@lookahead_workaround pub type $ident:ident<$($gi:ident $(lookahead_qualifier=$gq:tt)? $(: $gt:ty)?),*> = $($tt:tt)*) => {
            pub type $ident<$($($gq)? $gi $(: $gt)?),*> = $($tt)*;
        };

        (pub type $ident:ident<$($(const)? $gi:ident $(: $gt:ty)?),*> = $($tt:tt)*) => {
            pub type $ident<$($(const)? $gi $(: $gt)?),*> = $($tt)*;
        };
    }

    // // TODO: https://github.com/rust-lang/rust/issues/130928
    // echo2!(pub type Foo2<T, const N: usize> = [T; N]);

    echo2!(@lookahead_workaround pub type Foo2<T, N lookahead_qualifier=const: usize> = [T; N]);

I expected to see this happen:

  • Neat way to match lookahead within rust grammer syntax const X: Y

Instead, this happened:

  • Need to simulate lookahead within rust grammer syntax const X: Y

  • Leads to ugly simulation code.

  • Thus I think it's a design bug.

Meta

rustc --version --verbose:

rustc 1.83.0-nightly (9b72238eb 2024-09-14)
binary: rustc
commit-hash: 9b72238eb813e9d06e9e9d270168512fbffd7ee7
commit-date: 2024-09-14
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
Backtrace

<backtrace>

related

Also found similar issue back to 2021 in pin-project-lite.

So, I guess there is no way yet.

https://github.com/taiki-e/pin-project-lite/issues/62

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the macro_rules! examples in the issue and investigate how declarative macro matching handles generic parameters such as const N: usize. Read the related pin-project-lite issue for prior context. Done would require a clearly scoped, accepted design for matching or simulating this syntax, along with corresponding Rust compiler coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.