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

Implement static_assert attribute #6670

Merged
merged 2 commits into from
May 22, 2013
Merged

Implement static_assert attribute #6670

merged 2 commits into from
May 22, 2013

Conversation

emberian
Copy link
Member

This verifies that a static item evaluates to true, at compile time.

@kud1ing
Copy link

kud1ing commented May 22, 2013

Nice. Could this be implemented as a macro à la Boost's StaticAssert?
I think macros make the point a bit more clear that something is tested and not just stated.

@emberian
Copy link
Member Author

I don't believe so, but I'm not sure. Macros run very early in the compilation process, before there's enough information to actually do the assertion. I'm also not sure if macros can expand into items with attributes, which is the only non-invasive way to implement this.

Ideally it wouldn't be an attribute though. It's really nasty to have bools floating around in your statics just for compiletime assertions. But it's the only way to implement it on the existing language (afaik).

Although if static_assert!() were special cased, it might be possible...

@kud1ing
Copy link

kud1ing commented May 22, 2013

Not sure whether you meant this with your last sentence: how about a macro that generates the current solution?

static_assert!(<WHATEVER>)

expanding to

#[static_assert]
static ___x: bool = <WHATEVER>;

@emberian
Copy link
Member Author

Like I said, I'm not sure that it is possible to expand a macro into an item with attributes, and not sure if it's possible inside of functions. If so, great! Generating a non-conflicting name is going to be the hard part I think. I've opened #6676.

By my last sentence I mean if when static_assert!() is found its contents are converted into a const item (or maybe an expr, I'd have to look at the const code again) and stored in a special list in the crate context and at trans, evaluated. Not a normal macro expanded normally.

@huonw
Copy link
Member

huonw commented May 22, 2013

@cmr: expanding to mod foo { #[static_assert] static __x: bool = <WHATEVER>; } works in functions and as an item. Unfortunately, having two mod foo's in the same scope makes rustc unhappy, so (at this point) it would have to be implemented in Rust as a syntax extension (so that one can make sure each assertion gets a unique module), rather than a macro_rules!-style macro.

@emberian
Copy link
Member Author

At that point I'd much rather special case it than add stuff to the AST, it's bad enough as it is without phantom mods.

@kud1ing
Copy link

kud1ing commented May 22, 2013

@huonw: would it be undesireable or hard to make Rust support anonymous modules as in mod { }?
Admittedly they would only serve this purpose.

if x.slice(0, x.len()) == "static_assert" {
let v = ccx.const_values.get_copy(&item.id);
unsafe {
warn!("doin' it! %?", v);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this warn! call?

This verifies that a static item evaluates to true, at compile time.
bors added a commit that referenced this pull request May 22, 2013
This verifies that a static item evaluates to true, at compile time.
@bors bors closed this May 22, 2013
@bors bors merged commit acf920f into rust-lang:incoming May 22, 2013
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jan 13, 2022
…1995

Add `manual_bits` lint

Closes rust-lang#6670

---

changelog: new lint: [`manual_bits`]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants