const_check: trans: added support for trivial casts

Part of #1215
This commit is contained in:
Stefan Plantikow 2011-12-02 13:57:34 +01:00 committed by Graydon Hoare
parent 3ee2eb656e
commit 68a82e4468
3 changed files with 26 additions and 0 deletions

View file

@ -43,6 +43,7 @@ fn check_expr(sess: session, e: @expr, &&is_const: bool, v: visit::vt<bool>) {
"disallowed operator in constant expression");
ret;
}
expr_cast(_, _) { }
expr_lit(@{node: lit_str(_), _}) {
sess.span_err(e.span,
"string constants are not supported");

View file

@ -5184,6 +5184,16 @@ fn trans_tag_variant(cx: @local_ctxt, tag_id: ast::node_id,
// that does so later on?
fn trans_const_expr(cx: @crate_ctxt, e: @ast::expr) -> ValueRef {
alt e.node {
ast::expr_cast(e1, _) {
alt ccx_tcx(cx).cast_map.find(e.id) {
some(ty::triv_cast.) { trans_const_expr(cx, e1) }
_ {
cx.sess.span_err(e.span,
"non-trivial cast in constant expression");
fail;
}
}
}
ast::expr_lit(lit) { ret trans_crate_lit(cx, *lit); }
ast::expr_binary(b, e1, e2) {
let te1 = trans_const_expr(cx, e1);

View file

@ -0,0 +1,15 @@
use std;
import std::ctypes::*;
// This will be more interesting once there is support
// for consts that refer to other consts, i.e. math_f64::consts::pi as m_float
#[cfg(target_arch="x86")]
const foo: m_int = 0i32 as m_int;
#[cfg(target_arch="x86_64")]
const foo: m_int = 0i64 as m_int;
fn main() {
assert foo == 0 as m_int;
}