-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
remove static_assert #1096
remove static_assert #1096
Conversation
[issue]: https://github.com/rust-lang/rust/issues/6676 | ||
|
||
So why not improve `static_assert`? With compile time function evaluation, the | ||
idea of a ‘static assertion’ doesn’t need to have language semantics. Either |
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 don't see how CTFE obviates the desire for static_assert
. If anything, I think CTFE makes static_assert
much more powerful. CTFE allows us to calculate things at compile-time, and static_assert
then lets you abort compilation if a given CTFE expression doesn't have the expected value.
That said, with CTFE, I'd expect static_assert
to take the form of a macro static_assert!(condition[, message])
.
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 was under the assumption that in a macro, you could just panic, or use something like https://github.com/huonw/compile_msg 's compile_error
, which would outright replace the current static_assert
.
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 don't know what the current thinking is on CTFE, but I'm a little skeptical that panicking is something that would be compatible with CTFE. Similarly, I don't know whether CTFE would be able to use syntax extensions like compile_error
(as opposed to a syntax extension evaluating a CTFE, which is what my proposed static_assert!()
would be).
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 suppose with CTFE, this static_assert!()
macro could be provided by a crate, although it seems like a reasonable candidate to bake into the language (e.g. because the standard libraries might want to use it).
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.
Yeah, I don't see how CTFE obviates it.
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.
Ideally, assuming a CTFE where begin_unwind[_fmt]
(and whatever other functions are needed for panic!
) are const fn
s (throwing compile errors at runtime) then we could just use the regular assert!
macro:
static _assert: () = assert!(FOO);
// Or, in a future Rust where statements are allowed at module-level:
assert!(FOO);
The current form of |
+1 |
As someone who uses |
|
||
Throughout its life, `static_assert` has been... weird. Graydon suggested it | ||
[in May of 2013][suggest], and it was | ||
[implemented][https://github.com/rust-lang/rust/pull/6670] shortly after. |
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.
Typo: Should be [text](url)
, not [text][url]
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.
Thanks, I'll fix this if it's about to get merged.
alternative: provide a macro |
The “want” to remove static_assert is intense here. It infects compiler all the way to trans. If we’re keeping it, I’d rather see this implemented within bounds of CTFE somehow. |
@nrc given that I'm the author, I probably shouldn't shepard? |
@pythonesque what's the use case? When I implemented this I planned on usnig it to assert that sizes of things were the same but constant expressions aren't powerful enough. As the original designer and implementor, 👍. This is a weird feature that is extremely limited. |
@cmr I use it to assert that |
I'm in favour of removing this and just using |
|
No tests running at all seems like an orthogonal problem that should be solved anyway. :) In any case, replacing the |
Wait... the |
I distinctly remember writing a patch that explains this, but I cannot find
it in the testing guide now :(
Yes, rustdoc is only able to test libraries, not binaries.
|
rust-lang/rust#24508 <- hmmmmmm
|
oh! it's in http://doc.rust-lang.org/nightly/book/documentation.html#running-documentation-tests
but not in the testing guide. that's an oversight. I'll fix that now.
|
wooo reading github first thing in the morning. I should drink more coffee.
It's true that _rustdoc_ cannot test binary crates, but #test doesn't
work either? I'm not aware of that.
|
@ssokolow hm, a fn main() {
println!("Hello, world!");
}
#[test]
fn foo() {} (If you're still having problems I'm happy to help, but we should take it to stackoverflow or users rather than here.) |
Note: I have created a replacement lint-plugin: https://crates.io/crates/static_assert So now there's zero reason for this to stay in the compiler. Usage example: #![feature(plugin, custom_attribute)]
#![plugin(static_assert_)]
fn main() {
#[static_assert_]
const TEST: bool = false;
} I haven't figured out yet how to silence the "unused" warnings, but I'm working on it. Also the next step is to create a macro that automatically creates a temporary constant + adds the attribute (maybe even the Note2: once Edit: I just noticed the original was on static fields not on constants... so it's not a drop-in replacement. sorry about that. |
It’s probably going away: rust-lang/rfcs#1096
It’s probably going away: rust-lang/rfcs#1096
FWIW, I’ve replaced one usage of |
Hear ye, hear ye. This RFC is now in final comment period until June 2nd. |
🎊 |
@SimonSapin Unless I missed an announcement that compiler plugins will work in stable channel, I'd hardly call it zero reason. |
@ssokolow NB. |
Ahh. That makes sense. It can get a bit difficult to keep track of which features I'm not currently using are stable. |
I think I agree with @huonw that, in practice, adding a |
I'm fine with removing |
Based on the comment thread, we've decided to accept this RFC to remove --- Language Subteam |
I will rebase my implementation PR later today or tomorrow.
|
rendered