Skip to content

Commit 68a82e4

Browse files
bogglegraydon
authored andcommitted
const_check: trans: added support for trivial casts
Part of #1215
1 parent 3ee2eb6 commit 68a82e4

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/comp/middle/check_const.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn check_expr(sess: session, e: @expr, &&is_const: bool, v: visit::vt<bool>) {
4343
"disallowed operator in constant expression");
4444
ret;
4545
}
46+
expr_cast(_, _) { }
4647
expr_lit(@{node: lit_str(_), _}) {
4748
sess.span_err(e.span,
4849
"string constants are not supported");

src/comp/middle/trans.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5184,6 +5184,16 @@ fn trans_tag_variant(cx: @local_ctxt, tag_id: ast::node_id,
51845184
// that does so later on?
51855185
fn trans_const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
51865186
alt e.node {
5187+
ast::expr_cast(e1, _) {
5188+
alt ccx_tcx(cx).cast_map.find(e.id) {
5189+
some(ty::triv_cast.) { trans_const_expr(cx, e1) }
5190+
_ {
5191+
cx.sess.span_err(e.span,
5192+
"non-trivial cast in constant expression");
5193+
fail;
5194+
}
5195+
}
5196+
}
51875197
ast::expr_lit(lit) { ret trans_crate_lit(cx, *lit); }
51885198
ast::expr_binary(b, e1, e2) {
51895199
let te1 = trans_const_expr(cx, e1);

src/test/run-pass/triv-cast-const.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std;
2+
3+
import std::ctypes::*;
4+
5+
// This will be more interesting once there is support
6+
// for consts that refer to other consts, i.e. math_f64::consts::pi as m_float
7+
#[cfg(target_arch="x86")]
8+
const foo: m_int = 0i32 as m_int;
9+
10+
#[cfg(target_arch="x86_64")]
11+
const foo: m_int = 0i64 as m_int;
12+
13+
fn main() {
14+
assert foo == 0 as m_int;
15+
}

0 commit comments

Comments
 (0)