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

remove static_assert #1096

Merged
merged 1 commit into from Jun 2, 2015

Conversation

Projects
None yet
@steveklabnik
Copy link
Member

steveklabnik commented Apr 28, 2015

[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

This comment has been minimized.

@lilyball

lilyball Apr 28, 2015

Contributor

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]).

This comment has been minimized.

@steveklabnik

steveklabnik Apr 28, 2015

Author Member

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.

This comment has been minimized.

@lilyball

lilyball Apr 28, 2015

Contributor

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).

This comment has been minimized.

@lilyball

lilyball Apr 28, 2015

Contributor

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).

This comment has been minimized.

@pythonesque

pythonesque Apr 28, 2015

Contributor

Yeah, I don't see how CTFE obviates it.

This comment has been minimized.

@P1start

P1start Apr 29, 2015

Contributor

Ideally, assuming a CTFE where begin_unwind[_fmt] (and whatever other functions are needed for panic!) are const fns (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);
@lilyball

This comment has been minimized.

Copy link
Contributor

lilyball commented Apr 28, 2015

The current form of static_assert is pretty weird. If we really aren't actually using it for anything, then 👍 on removing it. But with full-blown CTFE I'd want to have it reintroduced as a macro static_assert(cond[, message]) as I mentioned in my comment.

@Ericson2314

This comment has been minimized.

Copy link
Contributor

Ericson2314 commented Apr 28, 2015

+1

@pythonesque

This comment has been minimized.

Copy link
Contributor

pythonesque commented Apr 28, 2015

As someone who uses static_assert, I'd rather not see it removed without a more compelling reason (unless full CTFE can actually fully reintroduce it, but it's not clear to me that it would be able to; panic! diverges, after all).


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.

This comment has been minimized.

@msiemens

msiemens Apr 29, 2015

Typo: Should be [text](url), not [text][url]

This comment has been minimized.

@steveklabnik

steveklabnik May 6, 2015

Author Member

Thanks, I'll fix this if it's about to get merged.

@ben0x539

This comment has been minimized.

Copy link

ben0x539 commented Apr 29, 2015

alternative: provide a macro static_assert!(condition); that expands to #[static_assert] static new_identifier: bool = condition; and stabilize the macro but not the implementation :P

@nagisa

This comment has been minimized.

Copy link
Contributor

nagisa commented Apr 29, 2015

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.

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented Apr 30, 2015

@nrc given that I'm the author, I probably shouldn't shepard?

@cmr

This comment has been minimized.

Copy link
Member

cmr commented May 6, 2015

@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.

@pythonesque

This comment has been minimized.

Copy link
Contributor

pythonesque commented May 7, 2015

@cmr I use it to assert that u8::MAX >= usize::MAX_BITS, which is required for correctness for my union find algorithm. With more powerful constant expressions (which I think have been accepted now) I would be able to assert more interesting things (particularly around things like correct alignments / gaps in data structures, where I rely on two representations matching up for memory safety).

@huonw

This comment has been minimized.

Copy link
Member

huonw commented May 7, 2015

I'm in favour of removing this and just using #[test]s until we get a nicer solution.

@ssokolow

This comment has been minimized.

Copy link

ssokolow commented May 7, 2015

#[test]s are less reliable. In fact, at the moment, I've got an experimental project where, even if I create tests exactly as shown in the book, zero tests are found by cargo test. I haven't had time to figure out why yet.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented May 7, 2015

No tests running at all seems like an orthogonal problem that should be solved anyway. :)

In any case, replacing the #[static_assert] const NAME: bool = expression; with #[test] fn name() { assert!(expression); } in a library should "just work" (unless one has explicitly opted for test = false in the [lib] section for that library).

@ssokolow

This comment has been minimized.

Copy link

ssokolow commented May 7, 2015

Wait... the [lib] section? The project in question is currently a single-file binary project. Did the book neglect to mention #[test] only working in library projects?

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented May 7, 2015

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented May 7, 2015

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented May 7, 2015

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented May 7, 2015

@huonw

This comment has been minimized.

Copy link
Member

huonw commented May 7, 2015

@ssokolow hm, a src/main.rs like the following gets compiled and tested when I run cargo test.

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.)

@oli-obk

This comment has been minimized.

Copy link
Contributor

oli-obk commented May 11, 2015

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 #[allow(dead_code)] attribute) so it can be used just like the C++ static_assert on any constant expression

Note2: once static_assert isn't a builtin-attribute anymore, i can remove the trailing underscore

Edit: I just noticed the original was on static fields not on constants... so it's not a drop-in replacement. sorry about that.

SimonSapin added a commit to servo/string-cache that referenced this pull request May 11, 2015

Remove usage of #[static_assert]
It’s probably going away: rust-lang/rfcs#1096

SimonSapin added a commit to servo/string-cache that referenced this pull request May 11, 2015

Remove usage of #[static_assert]
It’s probably going away: rust-lang/rfcs#1096
@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented May 11, 2015

FWIW, I’ve replaced one usage of #[static_assert] by #[cfg(target_endian = "little")] so that the function would be missing on big-endian (and compilation would fail). The other usage in that library I’ve made a debug_assert!:

servo/string-cache#85

@nrc nrc added the T-lang label May 15, 2015

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented May 26, 2015

Hear ye, hear ye. This RFC is now in final comment period until June 2nd.

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented May 26, 2015

🎊

@ssokolow

This comment has been minimized.

Copy link

ssokolow commented May 27, 2015

@SimonSapin Unless I missed an announcement that compiler plugins will work in stable channel, I'd hardly call it zero reason.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented May 27, 2015

@ssokolow NB. #[static_assert] also doesn't work in the stable channel, so there's not much loss.

@ssokolow

This comment has been minimized.

Copy link

ssokolow commented May 27, 2015

Ahh. That makes sense. It can get a bit difficult to keep track of which features I'm not currently using are stable.

@SimonSapin

This comment has been minimized.

Copy link
Contributor

SimonSapin commented May 27, 2015

@ssokolow I don’t see how my comment is related to plugins. Or did you mean @oli-obk ?

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 1, 2015

I think I agree with @huonw that, in practice, adding a #[test] (or a plugin, if you're so inclined) is better than keeping this feature around. I'm nervous about keeping random features around in unstable fashion for an indefinite period of time without strong motivation -- I don't want to allow de facto dependencies to form that prove troublesome to change. cough macro rules cough

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Jun 2, 2015

I'm fine with removing #[static_assert]; when we do, we should probably open a corresponding RFC issue at the same time stating that we'd like to see this functionality provided in some manner in the future (in the abstract; its just an issue, not an RFC, after all). At least, that's my take-away from the comment thread.

@nikomatsakis nikomatsakis referenced this pull request Jun 2, 2015

Closed

Static asserts #1146

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Jun 2, 2015

Based on the comment thread, we've decided to accept this RFC to remove static_assert. The major arguments in favor of removing it are that it complicates many parts of the compiler tool-chain and we know we would never stabilize the current design. Existing uses can be converted into tests or via a plugin. Per @pnkfelix's comment, we've created issue #1146.

--- Language Subteam

@nikomatsakis nikomatsakis merged commit c385c9b into rust-lang:master Jun 2, 2015

@steveklabnik

This comment has been minimized.

Copy link
Member Author

steveklabnik commented Jun 3, 2015

@chriskrycho chriskrycho referenced this pull request Feb 8, 2017

Closed

Document all features in the reference #38643

0 of 17 tasks complete

@chriskrycho chriskrycho referenced this pull request Mar 11, 2017

Closed

Document all features #9

18 of 48 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.