Skip to content

Commit e674a7e

Browse files
jwisebrson
authored andcommitted
middle: Add a pass to reject bad const expressions earlier. Currently just rejects unimplemented const expressions, but will be needed later.
1 parent ff5b319 commit e674a7e

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/comp/driver/rustc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ fn compile_input(sess: session::session, cfg: ast::crate_cfg, input: str,
151151
time(time_passes, "alias checking",
152152
bind middle::alias::check_crate(ty_cx, crate));
153153
time(time_passes, "kind checking", bind kind::check_crate(ty_cx, crate));
154+
time(time_passes, "const checking",
155+
bind middle::check_const::check_crate(ty_cx, crate));
154156
if sess.get_opts().no_trans { ret; }
155157
let llmod =
156158
time(time_passes, "translation",

src/comp/middle/check_const.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import syntax::ast::*;
2+
import syntax::ast_util::{variant_def_ids, dummy_sp};
3+
import syntax::visit;
4+
5+
fn check_crate(tcx: ty::ctxt, crate: @crate) {
6+
let v =
7+
@{visit_item: bind check_item(tcx, _, _, _)
8+
with *visit::default_visitor::<()>()};
9+
visit::visit_crate(*crate, (), visit::mk_vt(v));
10+
tcx.sess.abort_if_errors();
11+
}
12+
13+
fn check_item(tcx: ty::ctxt, it: @item, &&s: (), v: visit::vt<()>) {
14+
visit::visit_item(it, s, v);
15+
alt it.node {
16+
item_const(_ /* ty */, ex) {
17+
let v =
18+
@{visit_expr: bind check_const_expr(tcx, _, _, _)
19+
with *visit::default_visitor::<()>()};
20+
check_const_expr(tcx, ex, (), visit::mk_vt(v));
21+
}
22+
_ { }
23+
}
24+
}
25+
26+
fn check_const_expr(tcx: ty::ctxt, ex: @expr, &&s: (), v: visit::vt<()>) {
27+
visit::visit_expr(ex, s, v);
28+
alt ex.node {
29+
expr_lit(_) { }
30+
_ { tcx.sess.span_err(ex.span,
31+
"constant contains unimplemented expression type"); }
32+
}
33+
}
34+
35+
// Local Variables:
36+
// mode: rust
37+
// fill-column: 78;
38+
// indent-tabs-mode: nil
39+
// c-basic-offset: 4
40+
// buffer-file-coding-system: utf-8-unix
41+
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
42+
// End:

src/comp/rustc.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod middle {
2727
mod typeck;
2828
mod fn_usage;
2929
mod check_alt;
30+
mod check_const;
3031
mod mut;
3132
mod alias;
3233
mod kind;

0 commit comments

Comments
 (0)