From 5610205363eb56b2fd7ce6daeaa3b20a659398a5 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 26 Jan 2012 15:20:29 -0800 Subject: [PATCH] rustc: Move core injection to its own module --- src/comp/driver/driver.rs | 26 ++-------------- src/comp/front/core_inject.rs | 35 ++++++++++++++++++++++ src/comp/rustc.rc | 1 + src/test/compile-fail/no-core-attribute.rs | 6 ++++ 4 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 src/comp/front/core_inject.rs create mode 100644 src/test/compile-fail/no-core-attribute.rs diff --git a/src/comp/driver/driver.rs b/src/comp/driver/driver.rs index 87b00a9e25b..c75e9db8a7a 100644 --- a/src/comp/driver/driver.rs +++ b/src/comp/driver/driver.rs @@ -95,27 +95,6 @@ fn time(do_it: bool, what: str, thunk: fn@() -> T) -> T { ret rv; } -fn inject_libcore_reference(sess: session, - crate: @ast::crate) -> @ast::crate { - - fn spanned(x: T) -> @ast::spanned { - ret @{node: x, - span: {lo: 0u, hi: 0u, - expanded_from: codemap::os_none}}; - } - - let n1 = sess.next_node_id(); - let n2 = sess.next_node_id(); - - let vi1 = spanned(ast::view_item_use("core", [], n1)); - let vi2 = spanned(ast::view_item_import_glob(@["core"], n2)); - - let vis = [vi1, vi2] + crate.node.module.view_items; - - ret @{node: {module: { view_items: vis with crate.node.module } - with crate.node} with *crate } -} - enum compile_upto { cu_parse, cu_expand, @@ -147,9 +126,8 @@ fn compile_upto(sess: session, cfg: ast::crate_cfg, bind syntax::ext::expand::expand_crate(sess, crate)); if upto == cu_expand { ret {crate: crate, tcx: none}; } - if sess.opts.libcore { - crate = inject_libcore_reference(sess, crate); - } + + crate = front::core_inject::maybe_inject_libcore_ref(sess, crate); let ast_map = time(time_passes, "ast indexing", diff --git a/src/comp/front/core_inject.rs b/src/comp/front/core_inject.rs new file mode 100644 index 00000000000..8e88bd50068 --- /dev/null +++ b/src/comp/front/core_inject.rs @@ -0,0 +1,35 @@ +import driver::session::session; +import syntax::ast; +import syntax::codemap; + +export maybe_inject_libcore_ref; + +fn maybe_inject_libcore_ref(sess: session, + crate: @ast::crate) -> @ast::crate { + if sess.opts.libcore { + inject_libcore_ref(sess, crate) + } else { + crate + } +} + +fn inject_libcore_ref(sess: session, + crate: @ast::crate) -> @ast::crate { + + fn spanned(x: T) -> @ast::spanned { + ret @{node: x, + span: {lo: 0u, hi: 0u, + expanded_from: codemap::os_none}}; + } + + let n1 = sess.next_node_id(); + let n2 = sess.next_node_id(); + + let vi1 = spanned(ast::view_item_use("core", [], n1)); + let vi2 = spanned(ast::view_item_import_glob(@["core"], n2)); + + let vis = [vi1, vi2] + crate.node.module.view_items; + + ret @{node: {module: { view_items: vis with crate.node.module } + with crate.node} with *crate } +} diff --git a/src/comp/rustc.rc b/src/comp/rustc.rc index d63916d4d31..6a11dcce79d 100644 --- a/src/comp/rustc.rc +++ b/src/comp/rustc.rc @@ -94,6 +94,7 @@ mod front { mod attr; mod config; mod test; + mod core_inject; } mod back { diff --git a/src/test/compile-fail/no-core-attribute.rs b/src/test/compile-fail/no-core-attribute.rs new file mode 100644 index 00000000000..f39aa2eff2d --- /dev/null +++ b/src/test/compile-fail/no-core-attribute.rs @@ -0,0 +1,6 @@ +// error-pattern: whatever +#[no_core]; + +fn main() { + log(debug, core::int::max_value); +} \ No newline at end of file