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

Turn block into a closure in test macro #27

Merged
merged 1 commit into from
Sep 14, 2020

Conversation

Aaron1011
Copy link
Contributor

The doc_test! macro defines two local variables (entries and doc),
which are then accessed from the provided block.

However, this only compiled due to a bug in rustc: substituted
metavariables cannot refer to names defined within the macro body. For
example, the following ode does not compile:

macro_rules! foo {
    ($block:expr) => {
        let mut bar = false;
        $block
    }
}

fn main() {
    foo!({bar = true});
}

In this case, the doc_test! macro was incorrectly allowed to compile
due to the presence of the #[tokio::test] macro on the enclosing
function. When the underlying compiler bug is fixed in
rust-lang/rust#75800,
this macro will stop compiling.

I've adjusted the macro to take in a closure instead of a block. This
closure is invoked with the entries and doc variables, allowing it
to compile with both old and new compilers.

If you have any questions about this change, feel free to ask me.

The `doc_test!` macro defines two local variables (`entries` and `doc`),
which are then accessed from the provided `block`.

However, this only compiled due to a bug in rustc: substituted
metavariables cannot refer to names defined within the macro body. For
example, the following ode does not compile:

```rust
macro_rules! foo {
    ($block:expr) => {
        let mut bar = false;
        $block
    }
}

fn main() {
    foo!({bar = true});
}
```

In this case, the `doc_test!` macro was incorrectly allowed to compile
due to the presence of the `#[tokio::test]` macro on the enclosing
function. When the underlying compiler bug is fixed in
rust-lang/rust#75800,
this macro will stop compiling.

I've adjusted the macro to take in a closure instead of a block. This
closure is invoked with the `entries` and `doc` variables, allowing it
to compile with both old and new compilers.

If you have any questions about this change, feel free to ask me.
@CLAassistant
Copy link

CLAassistant commented Sep 14, 2020

CLA assistant check
All committers have signed the CLA.

@bartlomieju
Copy link
Member

@Aaron1011 nice catch and thanks for the patch!

CC @SyrupThinker PTAL

@SyrupThinker
Copy link

Looks good to me

Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@bartlomieju bartlomieju merged commit 7eefbc8 into denoland:main Sep 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants