diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-07-29 17:01:14 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-08-03 17:23:01 -0700 |
| commit | 5cccf3cd256420d9f32c265e83036dea1d5f94d8 (patch) | |
| tree | 22904c7bb3df0872afa227638aa5e1e4ccb99fbc /src/libsyntax/std_inject.rs | |
| parent | ceded6adb3a4e172eabef09e1c78717a99c16b14 (diff) | |
| download | rust-5cccf3cd256420d9f32c265e83036dea1d5f94d8.tar.gz rust-5cccf3cd256420d9f32c265e83036dea1d5f94d8.zip | |
syntax: Implement #![no_core]
This commit is an implementation of [RFC 1184][rfc] which tweaks the behavior of the `#![no_std]` attribute and adds a new `#![no_core]` attribute. The `#![no_std]` attribute now injects `extern crate core` at the top of the crate as well as the libcore prelude into all modules (in the same manner as the standard library's prelude). The `#![no_core]` attribute disables both std and core injection. [rfc]: https://github.com/rust-lang/rfcs/pull/1184
Diffstat (limited to 'src/libsyntax/std_inject.rs')
| -rw-r--r-- | src/libsyntax/std_inject.rs | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index 9787f2537c6..d41a8ff140c 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -41,52 +41,57 @@ fn ignored_span(sess: &ParseSess, sp: Span) -> Span { pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate { - if use_std(&krate) { - inject_crates_ref(krate, alt_std_name) - } else { + if no_core(&krate) { krate + } else { + let name = if no_std(&krate) {"core"} else {"std"}; + let mut fold = CrateInjector { + item_name: token::str_to_ident(name), + crate_name: token::intern(&alt_std_name.unwrap_or(name.to_string())), + }; + fold.fold_crate(krate) } } pub fn maybe_inject_prelude(sess: &ParseSess, krate: ast::Crate) -> ast::Crate { - if use_std(&krate) { + if no_core(&krate) { + krate + } else { + let name = if no_std(&krate) {"core"} else {"std"}; let mut fold = PreludeInjector { - span: ignored_span(sess, DUMMY_SP) + span: ignored_span(sess, DUMMY_SP), + crate_identifier: token::str_to_ident(name), }; fold.fold_crate(krate) - } else { - krate } } -pub fn use_std(krate: &ast::Crate) -> bool { - !attr::contains_name(&krate.attrs, "no_std") +pub fn no_core(krate: &ast::Crate) -> bool { + attr::contains_name(&krate.attrs, "no_core") +} + +pub fn no_std(krate: &ast::Crate) -> bool { + attr::contains_name(&krate.attrs, "no_std") || no_core(krate) } fn no_prelude(attrs: &[ast::Attribute]) -> bool { attr::contains_name(attrs, "no_implicit_prelude") } -struct StandardLibraryInjector { - alt_std_name: Option<String>, +struct CrateInjector { + item_name: ast::Ident, + crate_name: ast::Name, } -impl fold::Folder for StandardLibraryInjector { +impl fold::Folder for CrateInjector { fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { - - // The name to use in `extern crate name as std;` - let actual_crate_name = match self.alt_std_name { - Some(ref s) => token::intern(&s), - None => token::intern("std"), - }; - krate.module.items.insert(0, P(ast::Item { id: ast::DUMMY_NODE_ID, - ident: token::str_to_ident("std"), + ident: self.item_name, attrs: vec!( attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item( InternedString::new("macro_use")))), - node: ast::ItemExternCrate(Some(actual_crate_name)), + node: ast::ItemExternCrate(Some(self.crate_name)), vis: ast::Inherited, span: DUMMY_SP })); @@ -95,15 +100,9 @@ impl fold::Folder for StandardLibraryInjector { } } -fn inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) -> ast::Crate { - let mut fold = StandardLibraryInjector { - alt_std_name: alt_std_name - }; - fold.fold_crate(krate) -} - struct PreludeInjector { - span: Span + span: Span, + crate_identifier: ast::Ident, } impl fold::Folder for PreludeInjector { @@ -134,7 +133,7 @@ impl fold::Folder for PreludeInjector { global: false, segments: vec