Turn block into a closure in test macro#27
Merged
bartlomieju merged 1 commit intodenoland:mainfrom Sep 14, 2020
Merged
Conversation
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.
Member
|
@Aaron1011 nice catch and thanks for the patch! CC @SyrupThinker PTAL |
|
Looks good to me |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
doc_test!macro defines two local variables (entriesanddoc),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:
In this case, the
doc_test!macro was incorrectly allowed to compiledue to the presence of the
#[tokio::test]macro on the enclosingfunction. 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
entriesanddocvariables, allowing itto compile with both old and new compilers.
If you have any questions about this change, feel free to ask me.