-
Notifications
You must be signed in to change notification settings - Fork 13.4k
rustdoc: Experiment: Inject doctests into the host crate #141083
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
Comments
Thank you for writing this up! To add notes from the Cargo perspective. The assumption is that This also assumes that all doctest binaries, standalone or merged, can be run like any other libtest test. I had thought that some are run in a special way but was assured in the All Hands meeting that this wasn't the case. This would allow Cargo to treat this like any other compilation step, including caching, and we can run the binaries like any other test. The main downside to this is if only one doctest binary needs to be rebuilt, then all will be rebuilt. However, changing the Edition is rare and the goal is to migrate everyone over to |
As for the rustdoc side of this, the question is what to do about doctests against public APIs. We could make The main downside is that public API doctests could make references to private items and this wouldn't be caught. Personally, I'm fine with that. Its not restricting user behavior but allowing them to do more. It could lead to some poor docs due to sloppiness but that is a trade off. This could actually make doctesting easier because you could have shared helper functions to initialize logic with the call hidden by |
There was also the question of how to handle unit tests as those will also be present in the code being compiled and run. This would cause unit tests to run in the lib's test target as well as each doctest binary. There may be ways to detect this and avoid them but we need to make sure that happens. |
note that this requires type checking function bodies, which rustdoc is currently not allowed to do. |
I have always told people that one of the advantages of writing doctests is that they help you make sure your public API is actually usable — that there isn't some little mistake in not making a function, type, or field |
Well, there is no downside. You will have control over whether this a "normal" doctest or unit-test-like one. |
I do not see anywhere that it is said that this would be under user control, so I assumed it would be the one new way of running doctests. (I do like the idea of doctests being less magic, and built and run more like other kinds of tests.) |
The end goal is to have all doctests be injected like this. But we cannot do it if it means losing the current behaviour of the doctests without a way to keep it. |
One question I just had: how will this handle lint attributes? If the test item is being injected approximately at the location of the item it is testing, then will its parent lint attributes apply to it? Something else I think I mentioned in some discussion but didn't write down: I don't see any (nice) way to extend this to supporting clippy (#56232), since it's all integrated in one process I assume we'd have to have a "rustdoc-clippy" binary that does this transformation then runs with clippy's lints too. |
at the all hands, I was discussing with @rust-lang/clippy and @Mark-Simulacrum that clippy could instead be a library, not its own driver, which would allow rustc to emit clippy lints at the same time as generating unit tests. that would work here too, rustdoc could link against clippy and run its lints. |
I'm concerned about the clippy-library route. One of my goals is to de-specialize doctests. I overlooked that this proposal does not do that in it does not respect the configured driver. We could have |
another possible transformation is for rustdoc to do something like this:
|
Uh oh!
There was an error while loading. Please reload this page.
From: https://hackmd.io/@fmease/inject-doctests-into-host-crate:
Motivation
unused_crate_dependencies
, ...)Implementation
#[test] fn doctest_1234 { ... }
with the doctest body inside///
in the original code but not the new code given to rustc_parse? would it just work or does it need more invasive changes?Implementation History
Empty.
The text was updated successfully, but these errors were encountered: