1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use symbol::{Symbol, keywords};
14 use syntax_pos::{DUMMY_SP, Span};
15 use codemap::{self, ExpnInfo, NameAndSpan, MacroAttribute};
18 use tokenstream::TokenStream;
20 /// Craft a span that will be ignored by the stability lint's
21 /// call to codemap's is_internal check.
22 /// The expanded code uses the unstable `#[prelude_import]` attribute.
23 fn ignored_span(sess: &ParseSess, sp: Span) -> Span {
27 format: MacroAttribute(Symbol::intern("std_inject")),
29 allow_internal_unstable: true,
32 let expn_id = sess.codemap().record_expansion(info);
38 pub fn injected_crate_name(krate: &ast::Crate) -> Option<&'static str> {
39 if attr::contains_name(&krate.attrs, "no_core") {
41 } else if attr::contains_name(&krate.attrs, "no_std") {
48 pub fn maybe_inject_crates_ref(sess: &ParseSess,
49 mut krate: ast::Crate,
50 alt_std_name: Option<String>)
52 let name = match injected_crate_name(&krate) {
57 let crate_name = Symbol::intern(&alt_std_name.unwrap_or(name.to_string()));
59 krate.module.items.insert(0, P(ast::Item {
60 attrs: vec![attr::mk_attr_outer(DUMMY_SP,
62 attr::mk_word_item(Symbol::intern("macro_use")))],
63 vis: ast::Visibility::Inherited,
64 node: ast::ItemKind::ExternCrate(Some(crate_name)),
65 ident: ast::Ident::from_str(name),
66 id: ast::DUMMY_NODE_ID,
70 let span = ignored_span(sess, DUMMY_SP);
71 krate.module.items.insert(0, P(ast::Item {
72 attrs: vec![ast::Attribute {
73 style: ast::AttrStyle::Outer,
74 path: ast::Path::from_ident(span, ast::Ident::from_str("prelude_import")),
75 tokens: TokenStream::empty(),
76 id: attr::mk_attr_id(),
77 is_sugared_doc: false,
80 vis: ast::Visibility::Inherited,
81 node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path {
82 segments: ["{{root}}", name, "prelude", "v1"].into_iter().map(|name| {
83 ast::PathSegment::from_ident(ast::Ident::from_str(name), DUMMY_SP)
87 id: ast::DUMMY_NODE_ID,
88 ident: keywords::Invalid.ident(),