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
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,7 @@ names are effectively reserved. Some significant attributes include:
by the compiler can be found via `rustc -W help`.
* The `deriving` attribute, for automatically generating
implementations of certain traits.
* The `static_assert` attribute, for asserting that a static bool is true at compiletime

Other attributes may be added or removed during development of the language.

Expand Down
21 changes: 20 additions & 1 deletion src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2147,7 +2147,26 @@ pub fn trans_item(ccx: @CrateContext, item: &ast::item) {
trans_enum_def(ccx, enum_definition, item.id, vi, &mut i);
}
}
ast::item_const(_, expr) => consts::trans_const(ccx, expr, item.id),
ast::item_const(_, expr) => {
consts::trans_const(ccx, expr, item.id);
// Do static_assert checking. It can't really be done much earlier because we need to get
// the value of the bool out of LLVM
for item.attrs.each |attr| {
match attr.node.value.node {
ast::meta_word(x) => {
if x.slice(0, x.len()) == "static_assert" {
let v = ccx.const_values.get_copy(&item.id);
unsafe {
if !(llvm::LLVMConstIntGetZExtValue(v) as bool) {
ccx.sess.span_fatal(expr.span, "static assertion failed");
}
}
}
},
_ => ()
}
}
},
ast::item_foreign_mod(ref foreign_mod) => {
foreign::trans_foreign_mod(ccx, path, foreign_mod);
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/compile-fail/static-assert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[static_assert]
static a: bool = false; //~ ERROR static assertion failed

fn main() {
}
4 changes: 4 additions & 0 deletions src/test/compile-fail/static-assert2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[static_assert]
static e: bool = 1 == 2; //~ ERROR static assertion failed

fn main() {}
14 changes: 14 additions & 0 deletions src/test/run-pass/static-assert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[static_assert]
static b: bool = true;

#[static_assert]
static c: bool = 1 == 1;

#[static_assert]
static d: bool = 1 != 2;

#[static_assert]
static f: bool = (4/2) == 2;

fn main() {
}