Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

select! cannot contain more than 64 macro calls #2097

Closed
dbrgn opened this issue Mar 10, 2020 · 5 comments · Fixed by #2243
Closed

select! cannot contain more than 64 macro calls #2097

dbrgn opened this issue Mar 10, 2020 · 5 comments · Fixed by #2243
Labels
A-macro Area: macro related

Comments

@dbrgn
Copy link
Contributor

dbrgn commented Mar 10, 2020

This is a bit strange, but based on experimentation, I hope that the issue title is right.

The following code fails to compile:

#![recursion_limit = "2048"]

use futures::{select, channel::mpsc};
use futures_util::sink::SinkExt;
use futures_util::stream::StreamExt;

async fn blah() {
    let (mut tx, mut rx) = mpsc::channel(8);
    tx.send(42u8).await;
    
    loop {
        let mut next_msg = rx.next();
        select! {
            _ = next_msg => {
                println!("1");
                ...
                println!("65"); 
            }
        }
    }
}

fn main() {
    let myfuture = blah();
}

Error message:

   Compiling playground v0.0.1 (/playground)
error: no rules expected the token `!`
  --> src/main.rs:13:9
   |
13 | /         select! {
14 | |             _ = next_msg => {
15 | |                 println!("1");
16 | |                 println!("2");
...  |
80 | |             }
81 | |         }
   | |_________^ no rules expected this token in macro call
   |
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

Removing one of the println calls makes it compile.

The problem is not specific to println!, it also happens with macros like debug!.

Reproducer: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=de8c4b5aa1ae3ac9c432952e9143fb59

This happened to me in a real-world project due to logging calls in a big select block 😕

@dbrgn dbrgn changed the title select! cannot contain 64+ macro calls select! cannot contain more than 64 macro calls Mar 10, 2020
@cramertj
Copy link
Member

Presumably this is a limitation of proc-macro-hack? cc @dtolnay

@dtolnay
Copy link
Member

dtolnay commented Mar 17, 2020

Yeah this limit comes from https://github.com/dtolnay/proc-macro-hack/blob/0.5.11/nested/build.rs#L29. I guess we could bump it. I don't know how much it would slow down rustc's macro matcher; the generated macro is quadratically large in this limit.

@dbrgn
Copy link
Contributor Author

dbrgn commented Mar 17, 2020

If possible, at least a proper error msg should be generated. I managed to reduce the issue a bit by converting a commonly used custom macro to a regular function.

@dtolnay
Copy link
Member

dtolnay commented Mar 17, 2020

I fixed the error message in proc-macro-nested 0.1.4.

error: this macro does not support >64 nested macro invocations
  --> src/main.rs:13:9
   |
13 | /         select! {
14 | |             _ = next_msg => {
15 | |                 println!("1");
16 | |                 println!("2");
...  |
80 | |             }
81 | |         }
   | |_________^

@taiki-e
Copy link
Member

taiki-e commented Oct 1, 2020

This will be fixed by dtolnay/proc-macro-hack#62. (Rust 1.45+)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro Area: macro related
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants