-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Compile fail stable #43949
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
Merged
Merged
Compile fail stable #43949
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ that your tests are up to date and working. | |
|
||
The basic idea is this: | ||
|
||
```rust,ignore | ||
```ignore | ||
/// # Examples | ||
/// | ||
/// ``` | ||
|
@@ -16,6 +16,19 @@ The basic idea is this: | |
The triple backticks start and end code blocks. If this were in a file named `foo.rs`, | ||
running `rustdoc --test foo.rs` will extract this example, and then run it as a test. | ||
|
||
Please note that by default, if no language is set for the block code, `rustdoc` | ||
assumes it is `Rust` code. So the following: | ||
|
||
```rust | ||
let x = 5; | ||
``` | ||
|
||
is strictly equivalent to: | ||
|
||
``` | ||
let x = 5; | ||
``` | ||
|
||
There's some subtlety though! Read on for more details. | ||
|
||
## Pre-processing examples | ||
|
@@ -106,23 +119,23 @@ our source code: | |
```text | ||
First, we set `x` to five: | ||
|
||
```rust | ||
``` | ||
let x = 5; | ||
# let y = 6; | ||
# println!("{}", x + y); | ||
``` | ||
|
||
Next, we set `y` to six: | ||
|
||
```rust | ||
``` | ||
# let x = 5; | ||
let y = 6; | ||
# println!("{}", x + y); | ||
``` | ||
|
||
Finally, we print the sum of `x` and `y`: | ||
|
||
```rust | ||
``` | ||
# let x = 5; | ||
# let y = 6; | ||
println!("{}", x + y); | ||
|
@@ -136,7 +149,7 @@ explanation. | |
Another case where the use of `#` is handy is when you want to ignore | ||
error handling. Lets say you want the following, | ||
|
||
```rust,ignore | ||
```ignore | ||
/// use std::io; | ||
/// let mut input = String::new(); | ||
/// io::stdin().read_line(&mut input)?; | ||
|
@@ -145,7 +158,7 @@ error handling. Lets say you want the following, | |
The problem is that `?` returns a `Result<T, E>` and test functions | ||
don't return anything so this will give a mismatched types error. | ||
|
||
```rust,ignore | ||
```ignore | ||
/// A doc test using ? | ||
/// | ||
/// ``` | ||
|
@@ -179,7 +192,7 @@ Here’s an example of documenting a macro: | |
/// # } | ||
/// ``` | ||
/// | ||
/// ```rust,should_panic | ||
/// ```should_panic | ||
/// # #[macro_use] extern crate foo; | ||
/// # fn main() { | ||
/// panic_unless!(true == false, “I’m broken.”); | ||
|
@@ -224,7 +237,7 @@ only shows the part you care about. | |
`should_panic` tells `rustdoc` that the code should compile correctly, but | ||
not actually pass as a test. | ||
|
||
```rust | ||
```text | ||
/// ```no_run | ||
/// loop { | ||
/// println!("Hello, world"); | ||
|
@@ -233,6 +246,18 @@ not actually pass as a test. | |
# fn foo() {} | ||
``` | ||
|
||
`compile_fail` tells `rustdoc` that the compilation should fail. If it | ||
compiles, then the test will fail. However please note that code failing | ||
with the current Rust release may work in a future release, as new features | ||
are added. | ||
|
||
```text | ||
/// ```compile_fail | ||
/// let x = 5; | ||
/// x += 2; // shouldn't compile! | ||
/// ``` | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Documentation comment that doesn't document anything" Probably good to stick |
||
|
||
The `no_run` attribute will compile your code, but not run it. This is | ||
important for examples such as "Here's how to retrieve a web page," | ||
which you would want to ensure compiles, but might be run in a test | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block doesn't seem related to the rest of this PR? Like, it's worth noting, but it's not part of "making
compile_fail
stable".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to put this change in another commit?