-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Do not panic when a test function returns Result::Err. #100451
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
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
ed1fcdc
to
ea2e724
Compare
"Yes", but it's generally fine to change unstable APIs, so this is fine as-is. Based on not needing to update compiletest it'll affect a very small fraction of users most likely, so I'm not worried.
Hm, I'm not sure. I would have suggested -Cpanic=abort and --test would work, but it looks like rustc currently hard errors on that (without -Zpanic_abort_tests, which IIRC spawns separate processes). It's probably OK to go without a test here.
Not that I'm aware of.
Can you say more about this? Are you expecting a future patch to differentiate between Result returning tests and those that don't, and only spawn processes for the latter? It seems to me that we'd be unable to make that delta unless we're willing to accept panics in result-returning tests aborting the test harness entirely (maybe yes, but not an obvious tradeoff to me). |
@rustbot author |
Thanks for the quick review! (Also sorry for the late reply; I was on vacation last week.)
In our systems it is most likely that (nearly) all tests will return So no, we won't have any need to differentiate between |
1aadd16
to
67f857b
Compare
@rustbot ready Putting this back in my review queue. |
Please squash in the fixup commits (e.g., formatting, review comments). Looking over this again I think part of me wants to suggest we go all the way here and make it the responsibility of the test harness (or similar) to catch the panics very close to the test itself, and then have the rest of the code work purely with a Result. The code here feels pretty spread out to me though and I'm not sure there's a great place for that to happen. It doesn't feel like this patch is really improving that situation but it also doesn't feel like it makes it worse, just kind of neutral. I think I'm inclined to move ahead -- perhaps with a possible test ala #100451 (comment) added around that comment. |
67f857b
to
7c4fee2
Compare
Done.
I agree.
Sounds good, thanks! As mentioned in the comment above, it seems the test to which you linked does in fact catch that problem -- for static benches, anyway -- but was somehow not triggered by CI. I'd be happy to write a test to cover the dynamic benchmark case if you can point me to how to write dynamic benches. |
7c4fee2
to
6522223
Compare
Rust's test library allows test functions to return a Result, so that the test is deemed to have failed if the function returns a Result::Err variant. Currently, this works by having Result implement the Termination trait and asserting in assert_test_result that Termination::report() indicates successful completion. This turns a Result::Err into a panic, which is caught and unwound in the test library. This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by: * Compiling all code with --panic=abort to avoid having to generate unwinding tables, and * Running most tests in-process to avoid the overhead of spawning new processes. This change removes the intermediate panic step and passes a Result::Err directly through to the test runner. To do this, it modifies assert_test_result to return a Result<(), String> where the Err variant holds what was previously the panic message. It changes the types in the TestFn enum to return Result<(), String>. This tries to minimise the changes to benchmark tests, so it calls unwrap() on the Result returned by assert_test_result, effectively keeping the same behaviour as before.
6522223
to
e19a98c
Compare
@bors r+ |
…test, r=Mark-Simulacrum Do not panic when a test function returns Result::Err. Rust's test library allows test functions to return a `Result`, so that the test is deemed to have failed if the function returns a `Result::Err` variant. Currently, this works by having `Result` implement the `Termination` trait and asserting in assert_test_result that `Termination::report()` indicates successful completion. This turns a `Result::Err` into a panic, which is caught and unwound in the test library. This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by: * Compiling all code with `--panic=abort` to avoid having to generate unwinding tables, and * Running most tests in-process to avoid the overhead of spawning new processes. This change removes the intermediate panic step and passes a `Result::Err` directly through to the test runner. To do this, it modifies `assert_test_result` to return a `Result<(), String>` where the `Err` variant holds what was previously the panic message. It changes the types in the `TestFn` enum to return `Result<(), String>`. This tries to minimise the changes to benchmark tests, so it calls `unwrap()` on the `Result` returned by `assert_test_result`, effectively keeping the same behaviour as before. Some questions for reviewers: * Does the change to the return types in the enum `TestFn` constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable. * Is there a way to test this change, i.e., to test that no panic occurs if a test returns `Result::Err`? * Is there a shorter, more idiomatic way to fold `Result<Result<T,E>,E>` into a `Result<T,E>` than the `fold_err` function I added?
…test, r=Mark-Simulacrum Do not panic when a test function returns Result::Err. Rust's test library allows test functions to return a `Result`, so that the test is deemed to have failed if the function returns a `Result::Err` variant. Currently, this works by having `Result` implement the `Termination` trait and asserting in assert_test_result that `Termination::report()` indicates successful completion. This turns a `Result::Err` into a panic, which is caught and unwound in the test library. This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by: * Compiling all code with `--panic=abort` to avoid having to generate unwinding tables, and * Running most tests in-process to avoid the overhead of spawning new processes. This change removes the intermediate panic step and passes a `Result::Err` directly through to the test runner. To do this, it modifies `assert_test_result` to return a `Result<(), String>` where the `Err` variant holds what was previously the panic message. It changes the types in the `TestFn` enum to return `Result<(), String>`. This tries to minimise the changes to benchmark tests, so it calls `unwrap()` on the `Result` returned by `assert_test_result`, effectively keeping the same behaviour as before. Some questions for reviewers: * Does the change to the return types in the enum `TestFn` constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable. * Is there a way to test this change, i.e., to test that no panic occurs if a test returns `Result::Err`? * Is there a shorter, more idiomatic way to fold `Result<Result<T,E>,E>` into a `Result<T,E>` than the `fold_err` function I added?
Rollup of 5 pull requests Successful merges: - rust-lang#100451 (Do not panic when a test function returns Result::Err.) - rust-lang#102098 (Use fetch_update in sync::Weak::upgrade) - rust-lang#102538 (Give `def_span` the same SyntaxContext as `span_with_body`.) - rust-lang#102556 (Make `feature(const_btree_len)` implied by `feature(const_btree_new)`) - rust-lang#102566 (Add a known-bug test for rust-lang#102498) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 5 pull requests Successful merges: - rust-lang#100451 (Do not panic when a test function returns Result::Err.) - rust-lang#102098 (Use fetch_update in sync::Weak::upgrade) - rust-lang#102538 (Give `def_span` the same SyntaxContext as `span_with_body`.) - rust-lang#102556 (Make `feature(const_btree_len)` implied by `feature(const_btree_new)`) - rust-lang#102566 (Add a known-bug test for rust-lang#102498) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Rust's test library allows test functions to return a
Result
, so that the test is deemed to have failed if the function returns aResult::Err
variant. Currently, this works by havingResult
implement theTermination
trait and asserting in assert_test_result thatTermination::report()
indicates successful completion. This turns aResult::Err
into a panic, which is caught and unwound in the test library.This approach is problematic in certain environments where one wishes to save on both binary size and compute resources when running tests by:
--panic=abort
to avoid having to generate unwinding tables, andThis change removes the intermediate panic step and passes a
Result::Err
directly through to the test runner.To do this, it modifies
assert_test_result
to return aResult<(), String>
where theErr
variant holds what was previously the panic message. It changes the types in theTestFn
enum to returnResult<(), String>
.This tries to minimise the changes to benchmark tests, so it calls
unwrap()
on theResult
returned byassert_test_result
, effectively keeping the same behaviour as before.Some questions for reviewers:
TestFn
constitute a breaking change for the library API? Namely, the enum definition is public but the test library indicates that "Currently, not much of this is meant for users" and most of the library API appears to be marked unstable.Result::Err
?Result<Result<T,E>,E>
into aResult<T,E>
than thefold_err
function I added?