From 73e6a4f9e512b8e7fbec1c645311fa4382558aff Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Tue, 12 Jul 2011 16:46:42 -0700 Subject: [PATCH] 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. --- src/comp/front/test.rs | 25 ++++++++++++++++++++++++- src/comp/syntax/fold.rs | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/comp/front/test.rs b/src/comp/front/test.rs index ca7f3b69d7a..4c5f53d4bc1 100644 --- a/src/comp/front/test.rs +++ b/src/comp/front/test.rs @@ -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); diff --git a/src/comp/syntax/fold.rs b/src/comp/syntax/fold.rs index 929f0bc3f10..93c51b9baf8 100644 --- a/src/comp/syntax/fold.rs +++ b/src/comp/syntax/fold.rs @@ -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;