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 that assert! format arguments are evaluated lazily #82169

Merged
merged 2 commits into from
Feb 17, 2021
Merged

Document that assert! format arguments are evaluated lazily #82169

merged 2 commits into from
Feb 17, 2021

Conversation

not-an-aardvark
Copy link
Contributor

It can be useful to do some computation in assert! format arguments, in order to get better error messages. For example:

assert!(
    some_condition,
    "The state is invalid. Details: {}",
    expensive_call_to_get_debugging_info(),
);

It seems like assert! only evaluates the format arguments if the assertion fails, which is useful but doesn't appear to be documented anywhere. This PR documents the behavior and adds some tests.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 16, 2021
@sfackler
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 16, 2021

📌 Commit 21293582fe02785cb2978d080915d7886e15acf4 has been approved by sfackler

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 16, 2021
@rust-log-analyzer

This comment has been minimized.

@jyn514
Copy link
Member

jyn514 commented Feb 16, 2021

@bors r-

CI is failing.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Feb 16, 2021
It can be useful to do some computation in `assert!` format arguments, in order to get better error messages. For example:

```rust
assert!(
    some_condition,
    "The state is invalid. Details: {}",
    expensive_call_to_get_debugging_info(),
);
```

It seems like `assert!` only evaluates the format arguments if the assertion fails, which is useful but doesn't appear to be documented anywhere. This PR documents the behavior and adds some tests.
@not-an-aardvark
Copy link
Contributor Author

CI is hopefully fixed now.

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 16, 2021
#[allow(unreachable_code)]
fn main() {
assert!(true, "Failed: {:?}", panic!("assert! evaluated format expressions"));
debug_assert!(true, "Failed: {:?}", panic!("debug_assert! evaluated format expressions"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are debug assertions disabled for tests? If so, this doesn't test what is intended.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it does right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From trying it out, it looks like the debug assertions were indeed disabled for this test, so I updated it to ensure they're enabled. (I verified that the test now fails when debug_assert!(true, ...) is changed to debug_assert!(false, ...).

@sfackler
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 17, 2021

📌 Commit 15197cb has been approved by sfackler

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 17, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2021
…laumeGomez

Rollup of 11 pull requests

Successful merges:

 - rust-lang#79981 (Add 'consider using' message to overflowing_literals)
 - rust-lang#82094 (To digit simplification)
 - rust-lang#82105 (Don't fail to remove files if they are missing)
 - rust-lang#82136 (Fix ICE: Use delay_span_bug for mismatched subst/hir arg)
 - rust-lang#82169 (Document that `assert!` format arguments are evaluated lazily)
 - rust-lang#82174 (Replace File::create and write_all with fs::write)
 - rust-lang#82196 (Add caveat to Path::display() about lossiness)
 - rust-lang#82198 (Use internal iteration in Iterator::is_sorted_by)
 - rust-lang#82204 (Update books)
 - rust-lang#82207 (rustdoc: treat edition 2021 as unstable)
 - rust-lang#82231 (Add long explanation for E0543)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 16481a2 into rust-lang:master Feb 17, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants