-
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
Add compile-fail test in rustdoc #30726
Conversation
This feature will be real awesome. |
f67cc66
to
61a1146
Compare
I personally don't think that this ends up being too useful of a feature as-is in practice. Knowing that an example doesn't compile provides no guarantee about preventing something from accidentally regressing in the future because it's far more likely to break for other reasons such as API change. Along those lines the only "really useful" form of compile-fail tests in my opinion are the ones we have which check error messages. Those are quite flaky, however, as the order, number, and placement of errors changes over time in the compiler. Overall I don't think that flagging an example as purely "this should not compile" is a useful feature in the long run, and the more useful "this should not compile with this message" is not something we want to provide because it is not stable. |
driver::compile_input(sess, &cstore, cfg, &input, &out, &None, None, control); | ||
}).join() { | ||
Ok(_) if compile_fail => panic!("couldn't compile the test"), | ||
Err(_) if compile_fail == false => panic!("test compiled while it wasn't supposed to"), |
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.
Doesn't Ok here mean that the code does compile (insofar as compilation doesn't panic)? The match here seems to indicate the opposite.
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.
@rphmeier: Indeed, I switched error messages. Thanks for notifying me.
@alexcrichton: The point here isn't to prevent regression but to check if examples are still up to date. For the rest, I don't think I'm the best to answer. |
This was intended to be the first step. Testing that examples don't compile is better than not testing them, and we can improve later with more precise checking of errors. Compile-fail tests are an important type of test, and none of our documentation examples that don't compile are tested. We are planning on using this to test the compiler error index, which is filled with such examples. Given that compile-fail doc tests are important, it is clear to me that the tool we should use to test them is rustdoc, the tool we do all our other documentation testing with. Do you have other suggestions about how we might test documentation examples that don't compile? |
For the basic case of testing that examples fail to compile, without checking the specific errors, this patch lgtm, barring @alexcrichton's concerns. |
I guess what I was getting at here was two-fold:
I agree, though, that testing the error index is something we should definitely do, so I'd be happy with ensuring that this is feature gated. We can always stomach changing our own error messages, but it's not necessarily true that everyone else will. I would personally prefer to see some more extensive changes before landing the initial bits, but that doesn't matter too much. |
c80fc19
to
943d205
Compare
@nikomatsakis is also thinking about how to expose compile-fail tests to users. We might want to come up with a single compile-fail scheme that works for both doc tests and unit tests. |
b1918e7
to
a65e549
Compare
The first step is done, I added the
|
☔ The latest upstream changes (presumably #30978) made this pull request unmergeable. Please resolve the merge conflicts. |
778fa6a
to
05bd4c1
Compare
@alexcrichton Again I believe it is important for Rust users to have compile-fail tests, and want to have some path forward for adding that functionality, but you sound like you are against it completely.
There is code somewhere for generating the html index from the error codes. I thought this was going through markdown intermediately, but apparently it is not. To run through rustdoc it should be generating markdown.
Yes. I think what needs to happen to make this work is:
|
@brson sorry, but to clarify, when I said this:
I meant that I'm fine moving forward with this so long as it is feature gated or somehow requires the nightly compiler (e.g. isn't present on stable Rust) |
OK, let's move forward on this but make the feature unstable. @alexcrichton is a command-line flag sufficient, like @GuillaumeGomez do the rest of the steps for testing the index make sense? |
It may end up being an equivalent amount of code to detect something like |
@brson: I didn't test yet, very busy with gtk-rs. I'll have time this week-end. |
☔ The latest upstream changes (presumably #31089) made this pull request unmergeable. Please resolve the merge conflicts. |
Feature flag is ok by me. |
219003d
to
19d5420
Compare
91e535a
to
194f1ca
Compare
@bors r+ Let's see how this goes... |
📌 Commit 03dd31c has been approved by |
@@ -438,6 +440,18 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { | |||
} | |||
} | |||
|
|||
fn get_unstable_features_setting() -> bool { |
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.
Shouldn't this just call the function that's already in the compiler?
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 one is a bit shorter but it could (should?).
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.
I have a feeling it will be much shorter if you call that one instead! It's also best to not reproduce logic wherever possible.
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.
Can you r- please?
03dd31c
to
abd5ffb
Compare
@alexcrichton: Updated, you were absolutely right, |
@@ -462,6 +467,11 @@ impl LangString { | |||
let mut seen_rust_tags = false; | |||
let mut seen_other_tags = false; | |||
let mut data = LangString::all_false(); | |||
let allow_compile_fail = if let UnstableFeatures::Cheat = get_unstable_features_setting() { |
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.
@brson: This seems to not work. For what I understood, if it returned UnstableFeatures::Cheat
, it means that unstable features were allowed. Did I understood wrong?
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.
env::var("RUSTC_BOOTSTRAP_KEY").ok()
always returns None
.
abd5ffb
to
eb7664b
Compare
Test is good! \o/ |
@bors r+ |
📌 Commit eb7664b has been approved by |
r? @brson cc @alexcrichton I still need to add error code explanation test with this, but I can't figure out a way to generate the `.md` files in order to test example source codes. Will fix #27328.
our linux-cross-opt builder disappeared, but otherwise all test passed, so merging manually. |
Well done @GuillaumeGomez, this looks great! |
@michaelsproul: Thanks! :) |
…hton Compile fail stable Since #30726, we never made the `compile_fail` flag nor the error code check stable. I think it's time to change this fact. r? @alexcrichton
r? @brson
cc @alexcrichton
I still need to add error code explanation test with this, but I can't figure out a way to generate the
.md
files in order to test example source codes.Will fix #27328.