Elide existing main function when building a test runner. Issue #428

This prevents any defined main function from colliding with the one
synthesized for the test runner. This is not the best solution since it
doesn't compile a function the user defined, but I don't think it's likely to
be a problem in the near term.
This commit is contained in:
Brian Anderson 2011-07-12 16:46:42 -07:00
parent 41a3888da7
commit 73e6a4f9e5
2 changed files with 25 additions and 1 deletions

View File

@ -35,7 +35,8 @@ fn modify_for_testing(@ast::crate crate) -> @ast::crate {
mutable testfns = ~[]);
auto precursor = rec(fold_crate = bind fold_crate(cx, _, _),
fold_item = bind fold_item(cx, _, _)
fold_item = bind fold_item(cx, _, _),
fold_mod = bind fold_mod(cx, _, _)
with *fold::default_ast_fold());
auto fold = fold::make_fold(precursor);
@ -45,6 +46,28 @@ fn modify_for_testing(@ast::crate crate) -> @ast::crate {
ret res;
}
fn fold_mod(&test_ctxt cx, &ast::_mod m,
fold::ast_fold fld) -> ast::_mod {
// Remove any defined main function from the AST so it doesn't clash with
// the one we're going to add. FIXME: This is sloppy. Instead we should
// have some mechanism to indicate to the translation pass which function
// we want to be main.
fn nomain(&@ast::item item) -> option::t[@ast::item] {
alt (item.node) {
ast::item_fn(?f, _) {
if (item.ident == "main") { option::none }
else { option::some(item) }
}
_ { option::some(item) }
}
}
auto mod_nomain = rec(view_items=m.view_items,
items=ivec::filter_map(nomain, m.items));
ret fold::noop_fold_mod(mod_nomain, fld);
}
fn fold_crate(&test_ctxt cx, &ast::crate_ c,
fold::ast_fold fld) -> ast::crate_ {
auto folded = fold::noop_fold_crate(c, fld);

View File

@ -12,6 +12,7 @@ export dummy_out;
export noop_fold_crate;
export noop_fold_item;
export noop_fold_expr;
export noop_fold_mod;
type ast_fold = @mutable a_f;