-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Document compile_fail for doc tests #42288
Comments
https://doc.rust-lang.org/book/documentation.html#running-documentation-tests |
Updated the OP. |
probably part of #42322 |
There is also |
Is there actually a way for people to make |
The compiletest crate will allow writing them. |
@Mark-Simulacrum makes sense! To me it seems reasonable that you'd be able to create a test using an attribute like this:
Which would cover most use cases. To me it seems like this inconsistency in doc / other tests should be either addressed or more properly documented. |
There is a PR open for this: #43949. |
#43949 fixed this! |
The reasons we needed empty `fn main() {}`s were twofold: - Avoid confusing people when they click the "expand" button on the code listing and see the auto-main wrapping - Avoid failing doctests when running `mdbook test` that don't work when rustdoc wraps a code listing in main I think I have a solution that mostly solves these cases. I don't know why this didn't occur to me before. Here's my current thinking in case these assumptions turn out to be wrong: There are a [few things that tell mdbook to disable the main-wrapping][mdbook], and I hadn't noticed one of them until now: if you annotate a code block with `noplayground`, it won't add a `main` around it (and it also won't have the "play" button in the upper right that runs the block and inserts the result into the page). So instead of putting an empty `fn main() {}` at the bottom of src/lib.rs files that doesn't make sense, annotate those listings with `noplayground`. I don't think anyone will miss the play button anyway because: - The play button doesn't run tests, so there wasn't any output for these examples anyway - If an example doesn't compile, we have it marked `ignore` so that it doesn't make the tests fail, and `ignore` also disables the play button, so there isn't a way to see compiler errors either In most of these cases, `mdbook test` that runs these as doctests will still wrap these in main, but the tests still pass. There are some cases, mostly around modules and using `crate::` that won't pass as doctests when wrapped in main. For those, I've annotated them with the [undocumented][] [`test_harness`][] attribute that apparently I was using at some point and then [stopped using][] and now I've decided to use again, but maybe send in a PR to rust-lang/rust to change the name to `no_main` and document it or something. In any case, that shouldn't affect readers at all. [mdbook]: https://github.com/rust-lang/mdBook/blob/d0deee90b04068ed949f524bb682a47fa26f2218/src/renderer/html_handlebars/hbs_renderer.rs#L805-L808 [undocumented]: rust-lang/rust#42288 (comment) [`test_harness`]: https://github.com/rust-lang/rust/blob/220352781c2585f0efb07ab0e758b136514de5b8/src/librustdoc/doctest.rs#L252 [stopped using]: #1233 (comment)
The reasons we needed empty `fn main() {}`s were twofold: - Avoid confusing people when they click the "expand" button on the code listing and see the auto-main wrapping - Avoid failing doctests when running `mdbook test` that don't work when rustdoc wraps a code listing in main I think I have a solution that mostly solves these cases. I don't know why this didn't occur to me before. Here's my current thinking in case these assumptions turn out to be wrong: There are a [few things that tell mdbook to disable the main-wrapping][mdbook], and I hadn't noticed one of them until now: if you annotate a code block with `noplayground`, it won't add a `main` around it (and it also won't have the "play" button in the upper right that runs the block and inserts the result into the page). So instead of putting an empty `fn main() {}` at the bottom of src/lib.rs files that doesn't make sense, annotate those listings with `noplayground`. I don't think anyone will miss the play button anyway because: - The play button doesn't run tests, so there wasn't any output for these examples anyway - If an example doesn't compile, we have it marked `ignore` so that it doesn't make the tests fail, and `ignore` also disables the play button, so there isn't a way to see compiler errors either In most of these cases, `mdbook test` that runs these as doctests will still wrap these in main, but the tests still pass. There are some cases, mostly around modules and using `crate::` that won't pass as doctests when wrapped in main. For those, I've annotated them with the [undocumented][] [`test_harness`][] attribute that apparently I was using at some point and then [stopped using][] and now I've decided to use again, but maybe send in a PR to rust-lang/rust to change the name to `no_main` and document it or something. In any case, that shouldn't affect readers at all. [mdbook]: https://github.com/rust-lang/mdBook/blob/d0deee90b04068ed949f524bb682a47fa26f2218/src/renderer/html_handlebars/hbs_renderer.rs#L805-L808 [undocumented]: rust-lang/rust#42288 (comment) [`test_harness`]: https://github.com/rust-lang/rust/blob/220352781c2585f0efb07ab0e758b136514de5b8/src/librustdoc/doctest.rs#L252 [stopped using]: #1233 (comment)
You can mark doctests as
compile_fail
but this isn't actually documented anywhere in the Rust book, even though it should be.The text was updated successfully, but these errors were encountered: