Skip to content

Commit

Permalink
const_check: trans: added support for trivial casts
Browse files Browse the repository at this point in the history
Part of #1215
  • Loading branch information
boggle authored and graydon committed Dec 2, 2011
1 parent 3ee2eb6 commit 68a82e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/comp/middle/check_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 10 additions & 0 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/test/run-pass/triv-cast-const.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 68a82e4

Please sign in to comment.