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

perform macro-expansion on doctests before checking for fn main #57415

Open
QuietMisdreavus opened this issue Jan 7, 2019 · 1 comment
Open
Labels
A-doctests Area: Documentation tests, run by rustdoc C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@QuietMisdreavus
Copy link
Member

As discussed in #57019, i'd like to refactor the doctest parsing/execution code in rustdoc so that it checks for fn main and extern crate during compilation, not before. This allows us to take advantage of macro expansion and a proper parsing step to find multi-line attributes or extern crate statements with the #[macro_use] on a separate line.

I believe it's possible to break up the compilation steps in test::run_test like in core::run_core, except this time we'd stop after phase_2 and inspect the crate for our desired items. If the test doesn't have a fn main or the right extern crate statement, then we can modify the crate in-place (or attempt to splice in the right text in the original text and run compilation again) before continuing. I haven't tried it, but i'd like to sketch it out soon.

@QuietMisdreavus QuietMisdreavus added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. C-feature-request Category: A feature request, i.e: not implemented / a PR. A-doctests Area: Documentation tests, run by rustdoc labels Jan 7, 2019
@QuietMisdreavus
Copy link
Member Author

Oh, dear, i forgot, we can't just start compiling doctests as if they were a regular crate, because standard doctests are just going to contain a bunch of expressions in module scope; it wouldn't even parse. I'll have to go back to the drawing board...

Centril added a commit to Centril/rust that referenced this issue Jan 12, 2019
…-parsing, r=GuillaumeGomez

rustdoc: use text-based doctest parsing if a macro is wrapping main

This is a "forward-port" of rust-lang#57019, intended to get rust-lang#56898 on nightly, since it's now fixed on beta (and already worked on stable).

To recap:

* The libsyntax-based doctest parsing now checks to see whether there is a top-level macro invocation in the doctest while it's checking for `fn main` and an `extern crate` statement.
* If it finds a macro invocation and *didn't* find `fn main`, then it performs the older text-based scan to allow doctests like the ones in `allocator_api` to still compile.

A "proper" fix will involve changing how `make_test` works to call it later in the `run_test` function, after the initial steps of compilation have completed. I've filed [a separate issue](rust-lang#57415) for that, though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-doctests Area: Documentation tests, run by rustdoc C-feature-request Category: A feature request, i.e: not implemented / a PR. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
Status: No status
Development

No branches or pull requests

1 participant