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.
14 use ext::hygiene::{Mark, SyntaxContext};
15 use symbol::{Symbol, keywords};
16 use syntax_pos::{DUMMY_SP, Span};
17 use codemap::{ExpnInfo, NameAndSpan, MacroAttribute, dummy_spanned, respan};
19 use tokenstream::TokenStream;
21 /// Craft a span that will be ignored by the stability lint's
22 /// call to codemap's `is_internal` check.
23 /// The expanded code uses the unstable `#[prelude_import]` attribute.
24 fn ignored_span(sp: Span) -> Span {
25 let mark = Mark::fresh(Mark::root());
26 mark.set_expn_info(ExpnInfo {
29 format: MacroAttribute(Symbol::intern("std_inject")),
31 allow_internal_unstable: true,
32 allow_internal_unsafe: false,
35 sp.with_ctxt(SyntaxContext::empty().apply_mark(mark))
38 pub fn injected_crate_name() -> Option<&'static str> {
39 INJECTED_CRATE_NAME.with(|name| name.get())
43 static INJECTED_CRATE_NAME: Cell<Option<&'static str>> = Cell::new(None);
46 pub fn maybe_inject_crates_ref(mut krate: ast::Crate, alt_std_name: Option<&str>) -> ast::Crate {
47 let name = if attr::contains_name(&krate.attrs, "no_core") {
49 } else if attr::contains_name(&krate.attrs, "no_std") {
55 INJECTED_CRATE_NAME.with(|opt_name| opt_name.set(Some(name)));
57 krate.module.items.insert(0, P(ast::Item {
58 attrs: vec![attr::mk_attr_outer(DUMMY_SP,
60 attr::mk_word_item(Symbol::intern("macro_use")))],
61 vis: dummy_spanned(ast::VisibilityKind::Inherited),
62 node: ast::ItemKind::ExternCrate(alt_std_name.map(Symbol::intern)),
63 ident: ast::Ident::from_str(name),
64 id: ast::DUMMY_NODE_ID,
69 let span = ignored_span(DUMMY_SP);
70 krate.module.items.insert(0, P(ast::Item {
71 attrs: vec![ast::Attribute {
72 style: ast::AttrStyle::Outer,
73 path: ast::Path::from_ident(ast::Ident::new(Symbol::intern("prelude_import"), span)),
74 tokens: TokenStream::empty(),
75 id: attr::mk_attr_id(),
76 is_sugared_doc: false,
79 vis: respan(span.shrink_to_lo(), ast::VisibilityKind::Inherited),
80 node: ast::ItemKind::Use(P(ast::UseTree {
82 segments: [name, "prelude", "v1"].into_iter().map(|name| {
83 ast::PathSegment::from_ident(ast::Ident::from_str(name))
87 kind: ast::UseTreeKind::Glob,
90 id: ast::DUMMY_NODE_ID,
91 ident: keywords::Invalid.ident(),