-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Constify assert_eq!
and assert_ne!
#103639
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const _: () = assert_eq!(1, 1); | ||
//~^ ERROR `core::panicking::assert_failed` is not yet stable as a const fn | ||
//~| HELP add `#![feature(const_assert_eq)]` to the crate attributes to enable | ||
|
||
fn main() {} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: `core::panicking::assert_failed` is not yet stable as a const fn | ||
--> $DIR/assert_eq_gate.rs:1:15 | ||
| | ||
LL | const _: () = assert_eq!(1, 1); | ||
| ^^^^^^^^^^^^^^^^ | ||
| | ||
= help: add `#![feature(const_assert_eq)]` to the crate attributes to enable | ||
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to previous error | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#![feature(const_assert_eq)] | ||
|
||
const _BAD1: () = { | ||
assert_eq!(1, 2) | ||
}; //~^ ERROR: evaluation of constant value failed | ||
|
||
const _BAD2: () = { | ||
assert_ne!(1, 1) | ||
}; //~^ ERROR: evaluation of constant value failed | ||
|
||
const _BAD3: () = { | ||
assert!(false) | ||
}; //~^ ERROR: evaluation of constant value failed | ||
|
||
fn main() {} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/assertions.rs:4:5 | ||
| | ||
LL | assert_eq!(1, 2) | ||
| ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: `1 == 2`', $DIR/assertions.rs:4:5 | ||
| | ||
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/assertions.rs:8:5 | ||
| | ||
LL | assert_ne!(1, 1) | ||
| ^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: `1 != 1`', $DIR/assertions.rs:8:5 | ||
| | ||
= note: this error originates in the macro `assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error[E0080]: evaluation of constant value failed | ||
--> $DIR/assertions.rs:12:5 | ||
| | ||
LL | assert!(false) | ||
| ^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: false', $DIR/assertions.rs:12:5 | ||
| | ||
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
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 means that at compile-time, assert_eq/ne will print the stringify'd form of the values, rather than Debug format them, right?
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.
Yes, because we can't format the actual values in compile time unless there is a way to make
Display
const.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.
But this PR requires us to always support the stringify fallback, no? That is, we won't be able to add a
~const Display
requirement to the arguments in the future, if this PR is merged.We might still support printing via specialization though, if
~const Display
is available. Maybe, to ensure we've not painted ourselves into a corner, the stringify fallback could also be enabled via specialization on non-const contexts?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.
What do you mean? This isn't and shouldn't be insta-stable, which means we can change the implementation should future developments allow const display.
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.
Oh right sorry I've missed the
#[rustc_const_unstable(feature = "const_assert_eq", issue = "none")]
annotation. Should also answer the question of @Mark-Simulacrum .