Skip to content

Commit

Permalink
middle: Add a pass to reject bad const expressions earlier. Currently…
Browse files Browse the repository at this point in the history
… just rejects unimplemented const expressions, but will be needed later.
  • Loading branch information
jwise authored and brson committed Nov 9, 2011
1 parent ff5b319 commit e674a7e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/comp/driver/rustc.rs
Expand Up @@ -151,6 +151,8 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
time(time_passes, "alias checking",
bind middle::alias::check_crate(ty_cx, crate));
time(time_passes, "kind checking", bind kind::check_crate(ty_cx, crate));
time(time_passes, "const checking",
bind middle::check_const::check_crate(ty_cx, crate));
if sess.get_opts().no_trans { ret; }
let llmod =
time(time_passes, "translation",
Expand Down
42 changes: 42 additions & 0 deletions src/comp/middle/check_const.rs
@@ -0,0 +1,42 @@
import syntax::ast::*;
import syntax::ast_util::{variant_def_ids, dummy_sp};
import syntax::visit;

fn check_crate(tcx: ty::ctxt, crate: @crate) {
let v =
@{visit_item: bind check_item(tcx, _, _, _)
with *visit::default_visitor::<()>()};
visit::visit_crate(*crate, (), visit::mk_vt(v));
tcx.sess.abort_if_errors();
}

fn check_item(tcx: ty::ctxt, it: @item, &&s: (), v: visit::vt<()>) {
visit::visit_item(it, s, v);
alt it.node {
item_const(_ /* ty */, ex) {
let v =
@{visit_expr: bind check_const_expr(tcx, _, _, _)
with *visit::default_visitor::<()>()};
check_const_expr(tcx, ex, (), visit::mk_vt(v));
}
_ { }
}
}

fn check_const_expr(tcx: ty::ctxt, ex: @expr, &&s: (), v: visit::vt<()>) {
visit::visit_expr(ex, s, v);
alt ex.node {
expr_lit(_) { }
_ { tcx.sess.span_err(ex.span,
"constant contains unimplemented expression type"); }
}
}

// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
1 change: 1 addition & 0 deletions src/comp/rustc.rc
Expand Up @@ -27,6 +27,7 @@ mod middle {
mod typeck;
mod fn_usage;
mod check_alt;
mod check_const;
mod mut;
mod alias;
mod kind;
Expand Down

0 comments on commit e674a7e

Please sign in to comment.