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

Document compile_fail for doc tests #42288

Closed
clarfonthey opened this issue May 28, 2017 · 9 comments
Closed

Document compile_fail for doc tests #42288

clarfonthey opened this issue May 28, 2017 · 9 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented May 28, 2017

You can mark doctests as compile_fail but this isn't actually documented anywhere in the Rust book, even though it should be.

@Mark-Simulacrum Mark-Simulacrum added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label May 29, 2017
@frewsxcv
Copy link
Member

frewsxcv commented Jun 8, 2017

should_panic is documented, compile_fail is not

https://doc.rust-lang.org/book/documentation.html#running-documentation-tests

@clarfonthey clarfonthey changed the title Document should_panic, compile_fail for doc tests Document compile_fail for doc tests Jun 8, 2017
@clarfonthey
Copy link
Contributor Author

Updated the OP.

@frewsxcv frewsxcv added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Jun 8, 2017
@steveklabnik
Copy link
Member

probably part of #42322

@kennytm
Copy link
Member

kennytm commented Jun 20, 2017

There is also ```test_harness, currently only used in The Book second edition.

@clarfonthey
Copy link
Contributor Author

Is there actually a way for people to make compile_fail tests outside of documentation for non-rustc crates?

@Mark-Simulacrum
Copy link
Member

The compiletest crate will allow writing them.

@clarfonthey
Copy link
Contributor Author

@Mark-Simulacrum makes sense! To me it seems reasonable that you'd be able to create a test using an attribute like this:

#[compile_fail]
fn bad() {
    fn my_clone<T>(v: Vec<T>) -> Vec<T> {
        v.clone()
    }
}

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.

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 27, 2017
@frewsxcv
Copy link
Member

frewsxcv commented Sep 1, 2017

There is a PR open for this: #43949.

@frewsxcv frewsxcv removed the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label Sep 1, 2017
@steveklabnik
Copy link
Member

#43949 fixed this!

carols10cents added a commit to rust-lang/book that referenced this issue Dec 9, 2020
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)
carols10cents added a commit to rust-lang/book that referenced this issue Dec 9, 2020
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants