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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,8 @@ pub(crate) mod builtin {
///
/// This macro has a second form, where a custom panic message can
/// be provided with or without arguments for formatting. See [`std::fmt`]
/// for syntax for this form.
/// for syntax for this form. Expressions used as format arguments will only
/// be evaluated if the assertion fails.
///
/// [`std::fmt`]: ../std/fmt/index.html
///
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/macros/assert-format-lazy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-pass

#[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, ...).

assert_eq!(1, 1, "Failed: {:?}", panic!("assert_eq! evaluated format expressions"));
debug_assert_eq!(1, 1, "Failed: {:?}", panic!("debug_assert_eq! evaluated format expressions"));
assert_ne!(1, 2, "Failed: {:?}", panic!("assert_ne! evaluated format expressions"));
debug_assert_ne!(1, 2, "Failed: {:?}", panic!("debug_assert_ne! evaluated format expressions"));
}