Skip to content

Commit d4d6592

Browse files
committed
---
yaml --- r: 65206 b: refs/heads/master c: f517ed0 h: refs/heads/master v: v3
1 parent a71438d commit d4d6592

File tree

9 files changed

+104
-6
lines changed

9 files changed

+104
-6
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 5118d2f84a4c4a8bf00d7fb9e06609877bf821e7
2+
refs/heads/master: f517ed0b08f5a1cc9b65ac0e57dbfaaefcbf002a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/doc/rust.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,7 @@ names are effectively reserved. Some significant attributes include:
14261426
by the compiler can be found via `rustc -W help`.
14271427
* The `deriving` attribute, for automatically generating
14281428
implementations of certain traits.
1429+
* The `static_assert` attribute, for asserting that a static bool is true at compiletime
14291430

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

trunk/src/librustc/middle/trans/base.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2147,7 +2147,26 @@ pub fn trans_item(ccx: @CrateContext, item: &ast::item) {
21472147
trans_enum_def(ccx, enum_definition, item.id, vi, &mut i);
21482148
}
21492149
}
2150-
ast::item_const(_, expr) => consts::trans_const(ccx, expr, item.id),
2150+
ast::item_const(_, expr) => {
2151+
consts::trans_const(ccx, expr, item.id);
2152+
// Do static_assert checking. It can't really be done much earlier because we need to get
2153+
// the value of the bool out of LLVM
2154+
for item.attrs.each |attr| {
2155+
match attr.node.value.node {
2156+
ast::meta_word(x) => {
2157+
if x.slice(0, x.len()) == "static_assert" {
2158+
let v = ccx.const_values.get_copy(&item.id);
2159+
unsafe {
2160+
if !(llvm::LLVMConstIntGetZExtValue(v) as bool) {
2161+
ccx.sess.span_fatal(expr.span, "static assertion failed");
2162+
}
2163+
}
2164+
}
2165+
},
2166+
_ => ()
2167+
}
2168+
}
2169+
},
21512170
ast::item_foreign_mod(ref foreign_mod) => {
21522171
foreign::trans_foreign_mod(ccx, path, foreign_mod);
21532172
}

trunk/src/libsyntax/parse/parser.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4186,10 +4186,10 @@ pub impl Parser {
41864186
return iovi_foreign_item(item);
41874187
}
41884188
if (self.is_keyword("fn") || self.is_keyword("pure") ||
4189-
self.is_keyword("unsafe")) {
4189+
self.is_keyword("unsafe")) {
41904190
// FOREIGN FUNCTION ITEM
4191-
let item = self.parse_item_foreign_fn(attrs);
4192-
return iovi_foreign_item(item);
4191+
let item = self.parse_item_foreign_fn(attrs);
4192+
return iovi_foreign_item(item);
41934193
}
41944194
self.parse_macro_use_or_failure(attrs,macros_allowed,lo,visibility)
41954195
}
@@ -4504,7 +4504,12 @@ pub impl Parser {
45044504
let mut foreign_items = ~[];
45054505
loop {
45064506
match self.parse_foreign_item(/*bad*/ copy attrs, macros_allowed) {
4507-
iovi_none => break,
4507+
iovi_none => {
4508+
if *self.token == token::RBRACE {
4509+
break
4510+
}
4511+
self.unexpected();
4512+
},
45084513
iovi_view_item(view_item) => {
45094514
// I think this can't occur:
45104515
self.span_err(view_item.span,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern:unexpected token
12+
extern {
13+
f();
14+
}
15+
16+
fn main() {
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[static_assert]
2+
static a: bool = false; //~ ERROR static assertion failed
3+
4+
fn main() {
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[static_assert]
2+
static e: bool = 1 == 2; //~ ERROR static assertion failed
3+
4+
fn main() {}

trunk/src/test/run-pass/issue-4107.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub fn main() {
12+
let id: &Mat2<float> = &Matrix::identity();
13+
}
14+
15+
pub trait Index<Index,Result> { }
16+
pub trait Dimensional<T>: Index<uint, T> { }
17+
18+
pub struct Mat2<T> { x: () }
19+
pub struct Vec2<T> { x: () }
20+
21+
impl<T> Dimensional<Vec2<T>> for Mat2<T> { }
22+
impl<T> Index<uint, Vec2<T>> for Mat2<T> { }
23+
24+
impl<T> Dimensional<T> for Vec2<T> { }
25+
impl<T> Index<uint, T> for Vec2<T> { }
26+
27+
pub trait Matrix<T,V>: Dimensional<V> {
28+
fn identity() -> Self;
29+
}
30+
31+
impl<T> Matrix<T, Vec2<T>> for Mat2<T> {
32+
fn identity() -> Mat2<T> { Mat2{ x: () } }
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[static_assert]
2+
static b: bool = true;
3+
4+
#[static_assert]
5+
static c: bool = 1 == 1;
6+
7+
#[static_assert]
8+
static d: bool = 1 != 2;
9+
10+
#[static_assert]
11+
static f: bool = (4/2) == 2;
12+
13+
fn main() {
14+
}

0 commit comments

Comments
 (0)