Skip to content

Commit 24ece07

Browse files
Kimundialexcrichton
authored andcommitted
Allow blocks in const expressions
Only blocks with tail expressions that are const expressions and items are allowed.
1 parent cbc31df commit 24ece07

11 files changed

+290
-1
lines changed

src/librustc/middle/check_const.rs

+27
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,33 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &Expr, is_const: bool) {
148148
}
149149
}
150150
}
151+
ExprBlock(ref block) => {
152+
// Check all statements in the block
153+
for stmt in block.stmts.iter() {
154+
let block_span_err = |span|
155+
v.tcx.sess.span_err(span,
156+
"blocks in constants are limited to \
157+
items and tail expressions");
158+
match stmt.node {
159+
StmtDecl(ref span, _) => {
160+
match span.node {
161+
DeclLocal(_) => block_span_err(span.span),
162+
163+
// Item statements are allowed
164+
DeclItem(_) => {}
165+
}
166+
}
167+
StmtExpr(ref expr, _) => block_span_err(expr.span),
168+
StmtSemi(ref semi, _) => block_span_err(semi.span),
169+
StmtMac(..) => v.tcx.sess.span_bug(e.span,
170+
"unexpanded statement macro in const?!")
171+
}
172+
}
173+
match block.expr {
174+
Some(ref expr) => check_expr(v, &**expr, true),
175+
None => {}
176+
}
177+
}
151178
ExprVstore(_, ExprVstoreMutSlice) |
152179
ExprVstore(_, ExprVstoreSlice) |
153180
ExprVec(_) |

src/librustc/middle/const_eval.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use std::rc::Rc;
4747
// fixed-size vectors and strings: [] and ""/_
4848
// vector and string slices: &[] and &""
4949
// tuples: (,)
50-
// records: {...}
5150
// enums: foo(...)
5251
// floating point literals and operators
5352
// & and * pointers
@@ -241,6 +240,13 @@ impl<'a> ConstEvalVisitor<'a> {
241240

242241
ast::ExprRepeat(..) => general_const,
243242

243+
ast::ExprBlock(ref block) => {
244+
match block.expr {
245+
Some(ref e) => self.classify(&**e),
246+
None => integral_const
247+
}
248+
}
249+
244250
_ => non_const
245251
};
246252
self.ccache.insert(did, cn);
@@ -479,6 +485,12 @@ pub fn eval_const_expr_partial<T: ty::ExprTyProvider>(tcx: &T, e: &Expr)
479485
// If we have a vstore, just keep going; it has to be a string
480486
ExprVstore(e, _) => eval_const_expr_partial(tcx, e),
481487
ExprParen(e) => eval_const_expr_partial(tcx, e),
488+
ExprBlock(ref block) => {
489+
match block.expr {
490+
Some(ref expr) => eval_const_expr_partial(tcx, &**expr),
491+
None => Ok(const_int(0i64))
492+
}
493+
}
482494
_ => Err("unsupported constant expr".to_strbuf())
483495
}
484496
}

src/librustc/middle/trans/base.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,9 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) {
16111611
}
16121612
}
16131613
ast::ItemStatic(_, m, expr) => {
1614+
// Recurse on the expression to catch items in blocks
1615+
let mut v = TransItemVisitor{ ccx: ccx };
1616+
v.visit_expr(expr, ());
16141617
consts::trans_const(ccx, m, item.id);
16151618
// Do static_assert checking. It can't really be done much earlier
16161619
// because we need to get the value of the bool out of LLVM

src/librustc/middle/trans/consts.rs

+6
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,12 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr,
672672
}
673673
}
674674
ast::ExprParen(e) => { const_expr(cx, e, is_local) }
675+
ast::ExprBlock(ref block) => {
676+
match block.expr {
677+
Some(ref expr) => const_expr(cx, &**expr, is_local),
678+
None => (C_nil(cx), true)
679+
}
680+
}
675681
_ => cx.sess().span_bug(e.span,
676682
"bad constant expression type in consts::const_expr")
677683
};

src/librustc/middle/typeck/check/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -3554,6 +3554,12 @@ pub fn check_const_with_ty(fcx: &FnCtxt,
35543554
_: Span,
35553555
e: &ast::Expr,
35563556
declty: ty::t) {
3557+
// Gather locals in statics (because of block expressions).
3558+
// This is technically uneccessary because locals in static items are forbidden,
3559+
// but prevents type checking from blowing up before const checking can properly
3560+
// emit a error.
3561+
GatherLocalsVisitor { fcx: fcx }.visit_expr(e, ());
3562+
35573563
check_expr(fcx, e);
35583564
let cty = fcx.expr_ty(e);
35593565
demand::suptype(fcx, e.span, declty, cty);

src/test/auxiliary/cci_const_block.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 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 static BLOCK_FN_DEF: fn(uint) -> uint = {
12+
fn foo(a: uint) -> uint {
13+
a + 10
14+
}
15+
foo
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2014 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+
#![feature(macro_rules)]
12+
13+
static A: uint = { 1; 2 };
14+
//~^ ERROR: blocks in constants are limited to items and tail expressions
15+
16+
static B: uint = { { } 2 };
17+
//~^ ERROR: blocks in constants are limited to items and tail expressions
18+
19+
macro_rules! foo {
20+
() => (()) //~ ERROR: blocks in constants are limited to items and tail expressions
21+
}
22+
static C: uint = { foo!() 2 };
23+
24+
static D: uint = { let x = 4; 2 };
25+
//~^ ERROR: blocks in constants are limited to items and tail expressions
26+
27+
pub fn main() {
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 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+
// aux-build:cci_const_block.rs
12+
13+
extern crate cci_const_block;
14+
15+
pub fn main() {
16+
assert_eq!(cci_const_block::BLOCK_FN_DEF(390), 400);
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2014 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+
// General test that function items in static blocks
12+
// can be generated with a macro.
13+
14+
#![feature(macro_rules)]
15+
16+
struct MyType {
17+
desc: &'static str,
18+
data: uint,
19+
code: fn(uint, uint) -> uint
20+
}
21+
22+
impl MyType {
23+
fn eval(&self, a: uint) -> uint {
24+
(self.code)(self.data, a)
25+
}
26+
}
27+
28+
macro_rules! codegen {
29+
($e:expr, $v:expr) => {
30+
{
31+
fn generated(a: uint, b: uint) -> uint {
32+
a - ($e * b)
33+
}
34+
MyType {
35+
desc: "test",
36+
data: $v,
37+
code: generated
38+
}
39+
}
40+
}
41+
}
42+
43+
static GENERATED_CODE_1: MyType = codegen!(2, 100);
44+
static GENERATED_CODE_2: MyType = codegen!(5, 1000);
45+
46+
pub fn main() {
47+
assert_eq!(GENERATED_CODE_1.eval(10), 80);
48+
assert_eq!(GENERATED_CODE_2.eval(100), 500);
49+
}

src/test/run-pass/const-block-item.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2014 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+
#![feature(macro_rules)]
12+
13+
mod foo {
14+
pub trait Value {
15+
fn value(&self) -> uint;
16+
}
17+
}
18+
19+
static BLOCK_USE: uint = {
20+
use foo::Value;
21+
100
22+
};
23+
24+
static BLOCK_PUB_USE: uint = {
25+
pub use foo::Value;
26+
200
27+
};
28+
29+
static BLOCK_STRUCT_DEF: uint = {
30+
struct Foo {
31+
a: uint
32+
}
33+
Foo{ a: 300 }.a
34+
};
35+
36+
static BLOCK_FN_DEF: fn(uint) -> uint = {
37+
fn foo(a: uint) -> uint {
38+
a + 10
39+
}
40+
foo
41+
};
42+
43+
static BLOCK_MACRO_RULES: uint = {
44+
macro_rules! baz {
45+
() => (412)
46+
}
47+
baz!()
48+
};
49+
50+
pub fn main() {
51+
assert_eq!(BLOCK_USE, 100);
52+
assert_eq!(BLOCK_PUB_USE, 200);
53+
assert_eq!(BLOCK_STRUCT_DEF, 300);
54+
assert_eq!(BLOCK_FN_DEF(390), 400);
55+
assert_eq!(BLOCK_MACRO_RULES, 412);
56+
}

src/test/run-pass/const-block.rs

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2014 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+
#![allow(dead_code)]
12+
#![allow(unused_unsafe)]
13+
14+
struct Foo {
15+
a: uint,
16+
b: *()
17+
}
18+
19+
fn foo<T>(a: T) -> T {
20+
a
21+
}
22+
23+
static BLOCK_INTEGRAL: uint = { 1 };
24+
static BLOCK_EXPLICIT_UNIT: () = { () };
25+
static BLOCK_IMPLICIT_UNIT: () = { };
26+
static BLOCK_FLOAT: f64 = { 1.0 };
27+
static BLOCK_ENUM: Option<uint> = { Some(100) };
28+
static BLOCK_STRUCT: Foo = { Foo { a: 12, b: 0 as *() } };
29+
static BLOCK_UNSAFE: uint = unsafe { 1000 };
30+
31+
// FIXME: #13970
32+
// static BLOCK_FN_INFERRED: fn(uint) -> uint = { foo };
33+
34+
// FIXME: #13971
35+
// static BLOCK_FN: fn(uint) -> uint = { foo::<uint> };
36+
37+
// FIXME: #13972
38+
// static BLOCK_ENUM_CONSTRUCTOR: fn(uint) -> Option<uint> = { Some };
39+
40+
// FIXME: #13973
41+
// static BLOCK_UNSAFE_SAFE_PTR: &'static int = unsafe { &*(0xdeadbeef as *int) };
42+
// static BLOCK_UNSAFE_SAFE_PTR_2: &'static int = unsafe {
43+
// static X: *int = 0xdeadbeef as *int;
44+
// &*X
45+
// };
46+
47+
pub fn main() {
48+
assert_eq!(BLOCK_INTEGRAL, 1);
49+
assert_eq!(BLOCK_EXPLICIT_UNIT, ());
50+
assert_eq!(BLOCK_IMPLICIT_UNIT, ());
51+
assert_eq!(BLOCK_FLOAT, 1.0_f64);
52+
assert_eq!(BLOCK_STRUCT.a, 12);
53+
assert_eq!(BLOCK_STRUCT.b, 0 as *());
54+
assert_eq!(BLOCK_ENUM, Some(100));
55+
assert_eq!(BLOCK_UNSAFE, 1000);
56+
57+
// FIXME: #13970
58+
// assert_eq!(BLOCK_FN_INFERRED(300), 300);
59+
60+
// FIXME: #13971
61+
// assert_eq!(BLOCK_FN(300), 300);
62+
63+
// FIXME: #13972
64+
// assert_eq!(BLOCK_ENUM_CONSTRUCTOR(200), Some(200));
65+
66+
// FIXME: #13973
67+
// assert_eq!(BLOCK_UNSAFE_SAFE_PTR as *int as uint, 0xdeadbeef_u);
68+
// assert_eq!(BLOCK_UNSAFE_SAFE_PTR_2 as *int as uint, 0xdeadbeef_u);
69+
}

0 commit comments

Comments
 (0)